Author - StudySection Post Views - 810 views
Salesforce Script

Writing a script to be executed in a Salesforce Org

For updating fields on Custom Object records, make a new Salesforce apex class that implements a Database.Batchable interface and then update the fields on the existing records.

In the following scenario, we will take a Custom_Object__c and its field would be updated
global class UpdateCustomObjectField implements Database.Batchable, Database.Stateful{
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc){
system.debug('Start Called');
return Database.getQueryLocator('SELECT ID, FieldA, FieldB FROM Custom_Object__c' +
'WHERE FieldA = \'anyValue\'');
}
global void execute(Database.BatchableContext bc, List<Custom_Object__c> scope){
system.debug('Execute Called');
// process each batch of records
List objList = new List<Custom_Object__c>();
for (Custom_Object__c objVariable : scope){
objVariable.FieldA = 'new value for FIeldA';
objVariable.FieldB = 'new value for FIeldB';
// add objVariable to list to be updated
objList.add(objVariable);
// increment the instance member counter
recordsProcessed = recordsProcessed + 1;
}
update objList;
}
global void finish(Database.BatchableContext bc){
system.debug('Finish Called');
System.debug('The number of records processed are : ' + recordsProcessed);
AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems
FROM AsyncApexJob
WHERE Id = :bc.getJobId()];
}
}

To execute this class when deploying a managed package into a Salesforce Org Open Anonymous window from Developer Console and call the above class from there by instantiating its object as :
UpdateCustomObjectField obj = new UpdateCustomObjectField();
Id batchId = Database.executeBatch(obj, 100);

Microsoft Windows 10 is a widely used operating system in computers all over the world. If you have skills in Microsoft Windows 10 then you can get a Windows 10 Certification Exam from StudySection which can help you in getting hired. A beginner level certification exam for newbies and an advanced level certification exam for experts is available on StudySection.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri