1

I'm trying to populate a custom lookup field on the CampaignMember object. For some reason ocr.OpportunityId seems to come back null based on the debug logs when I run the batch. Can anyone tell why that might be happening?

global class addOCRToCms implements Database.Batchable<sObject> {

    global Database.QueryLocator start(Database.BatchableContext BC) {

        String query = 'SELECT ContactId, OpportunityId FROM OpportunityContactRole';

        return Database.getQueryLocator(query);

    }

    global void execute(Database.BatchableContext BC, List<OpportunityContactRole> scope) {

        Map<Id,Id> ocrIdMap = new Map<Id,Id>();

        for( OpportunityContactRole ocr : (List<OpportunityContactRole>)scope )

        {

        ocrIdMap.put(ocr.ContactId,ocr.OpportunityId); 

        List<CampaignMember> membersToUpdate = new List<CampaignMember>();

        for(CampaignMember cm :[SELECT ContactId, Opportunity__c FROM CampaignMember WHERE Opportunity__c = null AND ContactId IN :ocrIdMap.keyset()])

        {

        cm.Opportunity__c = ocrIdMap.get(ocr.OpportunityId);
        membersToUpdate.add(cm);

        }

        //save the records
        update membersToUpdate;

        }

    }

        global void finish(Database.BatchableContext BC) {

    }
 }
4
  • Can you post the logs as well as where in your code you see it null. Afaik opportunityid on ocr is required so it cannot be null or made null – Eric Oct 31 '16 at 7:38
  • Where are you getting the map blank? put system debug to inspect and like Eric said, share the logs here. – Mahmood Oct 31 '16 at 7:45
  • Have you made a test Class for this batch? Does that also return a null for ocr.OpportunityId? – John Thompson Oct 31 '16 at 13:24
  • naming conventions can help here; ocrIdMap doesn't tell you what the key is nor the value. Better name is ocrContactIdToOppoIdMap – cropredy Nov 1 '16 at 1:15
0

I think the problem is with this line:

cm.Opportunity__c = ocrIdMap.get(ocr.OpportunityId);

The ocrIdMap map is keyed by the Contact ID, not the Opportunity Id. It would never contain the Opportunity ID has a key, and would always return null.

Try the following, assuming it makes sense for what the code should be doing.

cm.Opportunity__c = ocrIdMap.get(ocr.ContactId);

Also consider what should happen if a contact has roles on more than one Opportunity. As the code is now, only one of the multiple Opportunity IDs would be in the Map.

1
  • Good catch, overlooked it. – Mahmood Oct 31 '16 at 8:02
0

Please get the contactid rather than using an opportunityid in the OcrIDMap as you are mapping contactid to opportunityid.Also please dont update inside for loop.Also,please dont use soql within for loop.PFB the code below,

global class addOCRToCms implements Database.Batchable<sObject> {

global Database.QueryLocator start(Database.BatchableContext BC) {

    String query = 'SELECT ContactId, OpportunityId FROM OpportunityContactRole';

    return Database.getQueryLocator(query);

}

global void execute(Database.BatchableContext BC, List<OpportunityContactRole> scope) {

    Map<Id,Id> ocrIdMap = new Map<Id,Id>();
    List<CampaignMember> membersToUpdate = new List<CampaignMember>();

    for( OpportunityContactRole ocr : (List<OpportunityContactRole>)scope )

    {   
        ocrIdMap.put(ocr.ContactId,ocr.OpportunityId);    
    }
    for(CampaignMember cm :[SELECT ContactId, Opportunity__c FROM CampaignMember WHERE Opportunity__c = null AND ContactId IN :ocrIdMap.keyset()])

    {
        //use cm.contactid to get the related opportunity as the map is with key of contactid
        cm.Opportunity__c = ocrIdMap.get(cm.contactid);
        membersToUpdate.add(cm);

    }
    if(!memberstoupdate.isempty())
        //save the records
        update membersToUpdate;

}

global void finish(Database.BatchableContext BC) {

}
1
  • This is right, definitely a newb mistake with the map – jdallas Nov 1 '16 at 1:07

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.