0

I have a requirement to update Contact on MC Subscriber activity by matching email ids on both MC Subscriber Activity and Contact. Now, if both object's email ID matches, the contact name will be updated on MC SUbscriber activity which is a child of Contact object.

Below is the trigger:

    trigger MCActivityonContact on MC4SF__MC_Subscriber_Activity__c (before insert, before update) {

    Map<string,Contact> mapAccountCodeWithAccount = new Map<string,Contact>();

    for (Contact Con : [select id,email from Contact where Email != Null ]){

        mapAccountCodeWithAccount.put(Con.Email,Con);
    }
   // MC4SF__MC_Subscriber_Activity__c Mcc;
   list<string> lstSplittedFields = new list<string>();
    for(MC4SF__MC_Subscriber_Activity__c mc : trigger.new){
         lstSplittedFields =  mc.MC4SF__MC_Subscriber__r.Name.split('-');
        if(mc.Contact__r.id ==null){
            if(mapAccountCodeWithAccount.containsKey(lstSplittedFields[0].trim())){
                mc.Contact__r.id = mapAccountCodeWithAccount.get(lstSplittedFields[0].trim()).Id;
            }
        }
    }
}

Now when I tried to update the MC Subscriber activity record, I'm getting the error as

System.NullPointerException: Attempt to de-reference a null object

I'm getting this error on lstSplittedFields on line 12. But I can see the value on MC Subscriber Activity object and wondering why I'm getting that error.

Can anyone please help me out with this error.

2

The MC4SF__MC_Subscriber_Activity__c records that you receive in the trigger only have their own fields loaded. If you want to access related records fields (as mc.MC4SF__MC_Subscriber__r.Name), you have to load them.

Bear in mind to do it before the loop to respect bulkification, as follows, and to include any other fields that you need.

List<MC4SF__MC_Subscriber_Activity__c> activitiesWithSubscriber = [SELECT Contact__c, MC4SF__MC_Subscriber__r.Name FROM MC4SF__MC_Subscriber_Activity__c WHERE Id IN :Trigger.newMap.keySet()];

Note: you don't need to load mc.Contact__r.id, as that's equivalent to mc.Contact__c

| improve this answer | |

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.