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
Adeline MooreAdeline Moore 

I want to Update a campaign member status when a lead gets converted.

When a lead gets converted to a contact I want to update the member's Statuses In two different campaigns.
I have been messing with Workflow and Process builder but I cant seem to get all the fields I need on either method

1. Canis Minor Lead Campaign = Status need to change to "Enrolled in canis Minor Program"
2. Canis inor Canis MAjor Campaign = Status needs to change to "Application Received"

Can someone pooint me in the right direction?

Process Builder seems to only let me work with fields from one object at a time.
If: 
Lead Status = Converted

then:
Campaign ID = 701i00000010euu
Member Status = "Enrolled in Canis Minor"

Campaign ID = 701i0000000VkZS
Member Status = "application Received"

 
Saurabh BSaurabh B
Hi Adeline, lead you are referring to is already a member of these two campaigns correct? So when a lead status field changes to 'Converted', member status should get updated? 
 
Adeline MooreAdeline Moore
The member is already a member of the 
Canis Minor Lead Campaign with status of "Inquiry"
I want the above campaign status to change to "Enrolled in Canis Minor Program" when The lead gets converted to a contact.

Canis Minor Canis Major Campaign  Member needs to be added to this campaign with status of "Enrolled in Canis Minor Canis Major Program" when the above Camapign Status Canis Minor Lead Campaign changes to "Enrolled in Canis Minor Program"
Saurabh BSaurabh B
Hi Adeline, Process builder offers limited funtionlaity when it comes to building logic with cross objects. Below is the trigger that I have written for you which will allow you to achieve your requirements. Please test this in your Sandbox and let me know if you need any help -
 
trigger UpdateCampMembersCanis on Lead (after update) {
    
        Set <id> leadConids = New Set <Id> ();

 	  for (Lead Ld : Trigger.new ){
        if (Ld.IsConverted == True) 
        leadConids.add(Ld.ConvertedContactId);
       system.debug('ConvertedContactId@@' + leadConids); 
    }
    
    List <CampaignMember> CamMemList = [SELECT ID,Status,ContactId,TYPE FROM CampaignMember WHERE ContactID IN :leadConids AND TYPE = 'Contact' AND CampaignId='701i00000010euu'];
           system.debug(CamMemList); 
    
List <CampaignMember> updateCamList = New List <CampaignMember> ();
List <CampaignMember> insertCamList = New List <CampaignMember> ();
   
    For (CampaignMember c : CamMemList) {
		c.status = 'Inquiry';
        updateCamList.add(c); //This will update status to "Enrolled in Canis Minor Program"
        
        //This will create status to "Enrolled in Canis Minor Canis Major Program"
     CampaignMember Camp = New CampaignMember ();
	Camp.Contactid = c.ContactId ;
    camp.CampaignId = '701i0000000VkZS';
    camp.Status = 'Enrolled in Canis Minor Canis Major Prog'; 
     insertCamList.add(camp);   
    }
    Update updateCamList;
    Insert insertCamList;
}

Please mark this as Best Answer if it helps you!
 
Saurabh BSaurabh B
Adeline, did it help you? It appears to work fine in my Dev Org.

Please mark this as Best Answer so that it can be helpful to other!
Adeline MooreAdeline Moore
Saurabh, I have never done a trigger before. I copied the above code and pasted it in a lead Trigger. I tested by creating a test lead and converted it. But I dont see that anythiong happened:(

 User-added image
User-added image
User-added image
User-added image

There is no campaign history at all.

Adeline
Saurabh BSaurabh B
I have tested this in my Dev Org and it worked fine. Can you please confirm if Id`s of campaigns below are correct? .
Canis Minor Lead Campaign - id is 701i00000010euu
Canis Minor Canis Major Campaign  - id is 701i0000000VkZS

Also, please see results below for my Dev org after Lead for Jack Rogers was converted.

Look at the contact for Jack Rogers. For Canis Minor Lead Campaign, Member status changed to 'Inquiry'

User-added image

Additionally,
For Jack Rogers was added to Canis Minor Canis Major Campaign, with Member status as 'Enrolled in Canis Minor Canis Major Prog'

User-added image
Adeline MooreAdeline Moore
I think the problem is my sandbox doesnt have the same campaigns. Let me go into Production and add the trigger and test there
 
Saurabh BSaurabh B
You will not be able to write trigger in a production directly. Please setup a similar test campaigns in a Sandbox and give me the ID`s. I will update the codes accordingly so that you can test it in a sandbox.
Adeline MooreAdeline Moore
I created the two campaigns in my sandbox and exchanged the URL's to the new Cmapaign URl's but when i try to convert I get this error:
User-added image

Qualified is the converted default Status and there is no other option on that field . 

Any ideas?

And When i try to build this in production it doesnt allow me to do that. If I am to deploy from Sandbox, My codes will need to be changed back to the actual Urls for production correct? 
Adeline MooreAdeline Moore
Gve them Test camapaign names too Maybe that is an issue 
Canis Minor Lead Campaign = Test Canis Minor Campaign = id is 7013B000000Rbo4
Canis Minor Canis Major Campaign = Test CM &CM =  id is 77013B000000Rbo9
Saurabh BSaurabh B
Here is the updated code, please try below and let me know if it works...
 
trigger UpdateCampMembersCanis on Lead (after update) {
    
        Set <id> leadConids = New Set <Id> ();

 	  for (Lead Ld : Trigger.new ){
        if (Ld.IsConverted == True) 
        leadConids.add(Ld.ConvertedContactId);
       system.debug('ConvertedContactId@@' + leadConids); 
    }
    
    List <CampaignMember> CamMemList = [SELECT ID,Status,ContactId,TYPE FROM CampaignMember WHERE ContactID IN :leadConids AND TYPE = 'Contact' AND CampaignId='7013B000000Rbo4'];
           system.debug(CamMemList); 
    
List <CampaignMember> updateCamList = New List <CampaignMember> ();
List <CampaignMember> insertCamList = New List <CampaignMember> ();
   
    For (CampaignMember c : CamMemList) {
		c.status = 'Inquiry';
        updateCamList.add(c); //This will update status to "Enrolled in Canis Minor Program"
        
        //This will create status to "Enrolled in Canis Minor Canis Major Program"
     CampaignMember Camp = New CampaignMember ();
	Camp.Contactid = c.ContactId ;
    camp.CampaignId = '77013B000000Rbo9';
    camp.Status = 'Enrolled in Canis Minor Canis Major Prog'; 
     insertCamList.add(camp);   
    }
    Update updateCamList;
    Insert insertCamList;
}

 
Saurabh BSaurabh B
Also, make sure that "Canis Minor Lead Campaign " has 'inquiry' status and "Canis Minor Canis Major Campaign " has 'Enrolled in Canis Minor Canis Major Prog' status.
Adeline MooreAdeline Moore
Could it be that I need to enter all possible Lead Statuses for line 18, other than Inquiry? We would like to Update the Campaign member status no matter what status the lead is at time of conversion.

User-added image
Saurabh BSaurabh B
Not required. You should only have 'Inquiry' in your campaign. That should do it. Line 18 is searching for your Lead that is being converted and updating it to status 'Inquiry'.
Adeline MooreAdeline Moore
Are you telling me to delete the other campaign statuses? Managment will not let me do that. Im  not understanding what I should be doing?
Hendrik VerbeekHendrik Verbeek
Appreciate that I'm coming very late to this party - and that this is the developer forum, however: just go declarative and make good use of the new and awesome Flow Builder features that were unleashed in 2020.  I have a similar though slightly simpler usecase where I want to update the Campaign Member Status to "Responded", when a Lead is converted.  I simply built a Record-Triggered Flow, that fires when the lead converts, then looks up the related Campaign Member record that is still "open" and update the Status to "Responded" thereafter. I'd reckon that you could also amend that slightly to meet your requirements here and update and or create additional Campaign Member records.