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
force shahidforce shahid 

Trigger to update MQL custom field based on campaign member on "Responded" campaign member field?

Hi Friends,

I am new to salesforce developing . I  have a custom MQL date / time field in CONTACT and LEAD object . 

I need to create a trigger on campaign member when a new contact / lead assign as a campaign member and check the MQL field in CONTACT / LEAD object is null or not and campaign member status is RESPONDED or not ? If Status is Responded we need to update the MQL field with current date time . 
Please Help me to write the code. I don't know how to write the code. Please provide the logic. 

Thanks for Advance.
kavya rao neelagirikavya rao neelagiri
Hi,

The code should be something like this. Please let me know if you have any questions

trigger HelpTrigger on CampaignMember (after insert) {
    List<CampaignMember> campaignMemberUpdate = new List<CampaignMember>();
        for (CampaignMember a : Trigger.new) 
        {
            if(a.contact.MQL__c != null && a.Lead.MQL__c != null && a.status ==  'Responded')
            {
                a.contact.MQL__c = system.today();
                a.Lead.MQL__c = system.today();
                campaignMemberUpdate.add(a);
            }
        }
    if(!campaignMemberUpdate.isEmpty())
    {
        update campaignMemberUpdate;
    }
}
                
force shahidforce shahid
Hi Kavya ,

I wrote the trigger . But I can't write test class for this trigger. Can you help me ?

My Trigger :

trigger CampaignMenmberStatus on CampaignMember (after insert,after Update)
  {
   list<contact> con=[select LastName,Email, otherphone, phone, Became_an_MQL_Lead_Date__c from Contact];
   List <Lead> leads = [select lastname, MobilePhone, Email, Company,phone, Became_an_MQL_Lead_Date__c ,Status from Lead];
      list<contact> upcon = new list<contact>();
      list<lead> uplead = new list<lead>();
    for(CampaignMember Member : trigger.new)
    {
          for(contact c:con)
        {
           if(Member.HasResponded==True && c.Became_an_MQL_Lead_Date__c==null )
           {
               c.Became_an_MQL_Lead_Date__c=System.now();
               upcon.add(c);
           }
        }
          for(Lead l:leads)
        {
           if(Member.HasResponded==True && l.Became_an_MQL_Lead_Date__c==null)
            {
                l.Became_an_MQL_Lead_Date__c=system.now();
                uplead.add(l);
            }  
        }       
        
    }
      if(upcon.size()>0)
          update upcon;
      if(uplead.size()>0)
          update uplead;
}
kavya rao neelagirikavya rao neelagiri
Hi,

For writing test class for above code:
1) Insert a Lead,Contact,campaignMember and use system.assert to check the update functionality.
2) update campaignMember and use system.assert to check the update functionality.

Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.