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 ]){

   // 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){
                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.

1 Answer 1


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


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .