+ Start a Discussion
Edward Scott 17Edward Scott 17 

Trigger or flow to fire from campaign member to contact on contact edit

Hi,

I am writing to ask if anyone knows a way to create a flow that will fire from campaign to the source campaign field on the contact object.

A little background I just wrote a trigger that sorts the names of the campaign in descending order based on date and fires the name of the most recent campaign to the source campaign field on the contact object. This will work moving forward but I am hoping there is something I can do to clean up the contacts that are already apart of campaigns.

I am thinking the only way is to create a flow and call that from a process builder on edit and do an upload but I am not sure how to create the flow.

Thanks in advance for your help,
Ed
Raj VakatiRaj Vakati
Can u share the your code ?that will help us to help you where we can improve 

You can do it even with the process builder .. did you tried 
Edward Scott 17Edward Scott 17
Hi Raj. I am pasting the code below. Right now it works when a new campaign is added to a lead or contact it updates the field source campaign. It also takes the most recent campaign when a lead is converted to a contact. What I am trying to do is update all the old contacts that have their first campaign as their campaign source and update them without adding in a new campaign. 

I haven't tried a process builder but if I can do it there that works as well. Thanks 
 
trigger SetSourceCampaign on CampaignMember (after insert, after update)
{
    List<Id> cmIds = new List<Id>();
    List<Lead> leadsToUpdate = new List<Lead>();
    List<Contact> contactsToUpdate = new List<Contact>();
    List<Id> cmLeadIds = new List<Id>();
    List<Id> cmContactIds = new List<Id>();

    for(CampaignMember cm: Trigger.New)
    {
        cmIds.add(cm.Id);
    }
    
    for(Lead l: [ SELECT Id, Source_Campaign__c, (SELECT CampaignId FROM CampaignMembers WHERE Campaign.Type != 'Lead Nurturing' AND Campaign.Type != 'Email Marketing' AND Campaign.Type != 'Other' AND Campaign.Type != 'Sales' AND Campaign.Type != 'Purchased' ORDER BY CreatedDate DESC LIMIT 1) FROM Lead WHERE Id IN (SELECT LeadId FROM CampaignMember WHERE Id IN :cmIds AND LeadId != NULL) AND IsConverted = FALSE ] )
    {
        List<CampaignMember> cmListL = l.CampaignMembers;
        if(cmListL != null && cmListL.size() > 0) {
            l.Source_Campaign__c = cmListL[0].CampaignId;
            leadsToUpdate.add(l);
        }
    }

    for(Contact c: [ SELECT Id, Source_Campaign__c, (SELECT CampaignId FROM CampaignMembers WHERE Campaign.Type != 'Lead Nurturing' AND Campaign.Type != 'Email Marketing' AND Campaign.Type != 'Other' AND Campaign.Type != 'Sales' AND Campaign.Type != 'Purchased' ORDER BY CreatedDate DESC LIMIT 1) FROM Contact WHERE Id IN (SELECT ContactId FROM CampaignMember WHERE Id IN :cmIds) AND ContactId != NULL] )
    {
        List<CampaignMember> cmListC = c.CampaignMembers;
        if(cmListC != null && cmListC.size() > 0) {
            c.Source_Campaign__c = cmListC[0].CampaignId;
            contactsToUpdate.add(c);
        }
    }
    
    update leadsToUpdate;
    update contactsToUpdate;
}

 
Raj VakatiRaj Vakati
You code looks good for me . .i think you can able to do it with process builder .. pls try and let me know 
Edward Scott 17Edward Scott 17
Thanks. Do you have any idea on how to do it from process builder?