You need to sign in to do that
Don't have an account?

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.
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.
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;
}
}
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;
}
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.