Activity Forums Salesforce® Discussions How to update multiple sObjects field in a Salesforce Apex batch class?

  • Vikas Kumar

    Member
    January 16, 2017 at 7:19 am

    Hi sushant,

    you can do something like this

    global class TestBatch implements Database.Batchable<sobject>{
    global String query;
    global String field;
    global String value;
    global List<id>allObjIds ;
    global TestBatch(List<id>allObjectIds){
    allObjIds=allObjectIds;
    field='Name';
    value='ModSEARCHAccount';
    }
    global Database.QueryLocator Start(Database.BatchableContext BC){
    query='SELECT Id,Name FROM Account WHERE Id in:allObjIds';
    return Database.getQueryLocator(query);
    }
    global void execute (Database.BatchableContext BC,List<Sobject>scope){
    for(Account obj:scope){

    obj.put(field,value);

    }
    update scope;
    }
    global void finish (Database.BatchableContext BC){

    //call another batch from here to to update another object says lead or opportunity
    }
    }

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos