function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex LatitudeGTAlex LatitudeGT 

Campaign Member populate lookup trigger not working

Hey guys i have created a Account Lookup field on a Campaign Member object which i'm currently using a trigger to populate.

Does anyone know if this is possible?

 

At the moment i cant seem to populate any lookup fields even though the soql is getting the correct data. After the trigger runs the Account field in the Campaign Member remains empty.

Any help would be appreciated.

 

Here is the trigger below:

 

trigger CampaignMemberLookup on CampaignMember (before update) {

   List<Id> contactID = new List<Id>();

 

   for (CampaignMember cm : trigger.new) { 

      CampaignMember old_cm = Trigger.oldMap.get(cm.Id);

      if (cm.ContactId != old_cm.ContactId) {

         contactID.add(cm.ContactId);

      }

   }

 

   List<Contact> myList= [Select Id, Account__c FROM Contact WHERE Id IN : contactID];

 

Map<Id, Account__c > myMap= new Map<Id, Account__c >();

for (Contact : myList)  {

tradingMap.put(c.Id, c.Account__c );

   }

 

for (CampaignMember cm : trigger.new) {

      if (myMap.containsKey(cm.ContactId)) {

         cm.Account__c = myMap.get(cm.ContactId);

      }

}

Shashikant SharmaShashikant Sharma

try this trigger Account__c to AccountId in Contact SOQL and changed MAP<ID , Account__c> to Map<ID ,ID>

 

trigger CampaignMemberLookup on CampaignMember (before update) {
   List<Id> contactID = new List<Id>();
 
   for (CampaignMember cm : trigger.new) { 
      if (cm.ContactId != null) {
         contactID.add(cm.ContactId);
      }
   }
 
   List<Contact> myList= [Select Id, AccountId FROM Contact WHERE Id IN : contactID];
 
Map<Id, Id> myMap= new Map<Id, Id>();
for (Contact c : myList)  {
myMap.put(c.Id, c.AccountId);
   }
 
for (CampaignMember cm : trigger.new) {
      if (myMap.containsKey(cm.ContactId)) {
       cm.Account__c = myMap.get(cm.ContactId);
      }
}
}

 

Let me know with your resluts.

 

Alex LatitudeGTAlex LatitudeGT

Tried your version but i'm still getting the same result, Account lookup remains blank once a Lead is converted into a Contact.