0

Below i have my apex class which queries all the contacts for an Account. After querying it is passed to a batch class to update one checkbox (Updated__C) to true which is on contact object .

after running and firing the batch i am getting below in debug

After this how to retrive all the contacts and update MyThunder__Updated__c to true.

Class -

  public class updateflag {

    private Id parentId;

    public updateflag(ApexPages.StandardController stdController) {
       // parentId = stdController.getId();
        parentId = ApexPages.currentPage().getParameters().get('id');
        callBatch();
    }

    public void callBatch() {
        updateflabatch batch = new updateflabatch(parentId);
        Database.executeBatch(batch);
    }
}

batch class -

    public class updateflabatch implements Database.Batchable<sObject>{

    private Id parentId;

    public updateflabatch(Id parentId) {
        this.parentId = parentId;
    }

    public Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([
            select Id
            from Contact
            where AccountId = :parentId
            and MyThunder__Updated__c = false
        ]);
    }

    public void execute(Database.BatchableContext bc, List<Contact> scope){
        for (Contact c : scope) {
            c.MyThunder__Updated__c = true;
        }
        update scope;
    }

    public void finish(Database.BatchableContext bc){
    }
}
  • I strongly suggest you write the batchable from scratch. Start by writing down exactly what you are trying to accomplish and then work out the code for that doing the query directly in the start method so you can process a very large number of rows; – Keith C Jan 6 '18 at 10:32
  • So Keith according to you.. what i wrote is totally incorrect...if i am getting parent and child in a Map and passing it to a batch , it is not possible to process the map and update it... – learnjqueery Jan 6 '18 at 10:36
  • Its possible if you return an iterator from start but then you lose the ability to process up to 50 million rows and have the normal query governor limit of 50,000 rows. I've added an answer that shows how I think the work should be divided. – Keith C Jan 6 '18 at 11:37
2

Not sure I fully understand what you want to do, but I would divide the work between the two classes more like below.

Controller:

public class updateflag {

    private Id parentId;

    public updateflag(ApexPages.StandardController stdController) {
        parentId = stdController.getId();
    }

    public void callBatch() {
        updateflabatch batch = new updateflabatch(parentId);
        Database.executeBatch(batch);
    }
}

Batchable:

public class updateflabatch implements Database.Batchable<sObject>{

    private Id parentId;

    public updateflabatch(Id parentId) {
        this.parentId = parentId;
    }

    public Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator([
            select Id
            from Contact
            where AccountId = :parentId
            and MyThunder__Updated__c = false
        ]);
    }

    public void execute(Database.BatchableContext bc, List<Contact> scope){
        for (Contact c : scope) {
            c.MyThunder__Updated__c = true;
        }
        update scope;
    }

    public void finish(Database.BatchableContext bc){
    }
}
| improve this answer | |
  • Hi Keith... Thanks for the help... I have updated my code...Now the problem is i am unable to make the checkbox to true...I am not sure where it is failing... – learnjqueery Jan 6 '18 at 12:19
-2

you are not included ",MyThunder__Updated__c " field in the query

select Id,MyThunder__Updated__c from Contact where AccountId = :parentId and MyThunder__Updated__c = false

| improve this answer | |
  • You don't have to query a field to update it or to reference it in a where clause. – Keith C Jan 6 '18 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.