0

I'm really struggling to learn this batchable process so i apologize ahead of time!!! Having said that....

I am trying to kick off a limited batch record update process. I think I have this part right now. I've built a scheduled class that calls my batch process:

//  In the scheduled class I'm going to update many records, 10 at a time.

OpportunityMajorGiftProspectBatch batch = new OpportunityMajorGiftProspectBatch(); 
Database.executeBatch(batch, 10);

The Issue

In my OpportunityMajorGiftProspectBatch class I don't know how to pass the getQueryLocator records, (OpportunityContactRole.Contact.Id values) into the Execute statement's List 'Contact' context. Do you know what I mean? I'm looking up Contact Id's in the OpportunityContactRole so I can update Contact records in the execute section.

Thanks in advance for help on this.

Kevin

global class OpportunityMajorGiftProspectBatch implements Database.Batchable<sObject> {
// This class sets a Contact's Major_Gift_Prospect_Contact__c field to TRUE
// under the correct circumstances.

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'Select contact.id, contact.Name from OpportunityContactRole WHERE Opportunity.DEPARTMENT_TYPE__C = \'Advancement\' AND Opportunity.DEPARTMENT_Sub_Type__C = \'Major Gifts\' AND Contact.Major_Gift_Prospect_Contact__c = false';
        return Database.getQueryLocator(query);
    }


    global void execute(Database.BatchableContext BC, List<Contact> scope) {
         for(Contact c : scope)
         {
             c.Major_Gift_Prospect_Contact__c = true;
         }
         update scope;
    }   

    global void finish(Database.BatchableContext BC) {
    }
}
3

you mix several things : the scope in the execute method is the records from the query define in the start. So in your case, OpportunityContactRole. You need to create a Map of contact while in the for loop :

global void execute(Database.BatchableContext BC, List<sObject> scope) {
     Map<Id,Contact> contactMap = new Map<Id,Contact>();
     for(sObject s : scope)
     {
        OpportunityContactRole ocr = (OpportunityContactRole)s;
        if(!contactMap.containsKey(ocr.Contact.Id))
        {
            Contact con = new Contact(Id=ocr.Contact.Id,Major_Gift_Prospect_Contact__c = true);
            contactMap.put(ocr.Contact.Id,con);
        }
     }
     update contactMap.values();
}   
0

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.