You need to sign in to do that
Don't have an account?
Query if Lead is in Campaign by matching Custom Lead Field
Hi All.
I'm hoping someone can help with this simple fix.
I have the following code below that looks at a lead field called custom field hubspot_campaign_c (this has a campaign ID in) and if it is not null it adds the lead to the specific campaign listed in hubspot_campaign_c.
trigger LeadToCampaign on Lead (after update, after insert) { for (Lead l: trigger.new) { if (l.HubSpot_Campaign__c != null ) { CampaignMember cm = new CampaignMember(); cm = new CampaignMember(); cm.LeadId = l.Id; cm.CampaignId = l.HubSpot_Campaign__c; //You will have to know the ID or query for it or something cm.Status = 'Responded'; //Or whatever default status you want on the member insert cm; } } }
This works perfectly fine but it generates an error if the lead record is updated because the lead will already exist in the campaign.
So what I need help on is to add a line which queries the campaignmembers and checks to see if the lead already exists for the campaignID listed in the custom field hubspot_campaign_c.
So what I was trying to do was create a list of campaign members where the leadID matches in the campaign member list but this is where I got stuck. This will generate a list of campaigns the lead is in and I then need to filter it by campaignid = hubspot_campaign_c and then if campaignmember list is null add the campaign member.
trigger LeadToCampaign on Lead (after update, after insert) { List<Id> leads=new List<Id>(); for (Lead l: trigger.new) { List<CampaignMember> cms=[Select LeadID, CampaignID FROM CampaignMember WHERE LeadID IN: leads]; if (l.HubSpot_Campaign__c != null) { CampaignMember cm = new CampaignMember(); cm = new CampaignMember(); cm.LeadId = l.Id; cm.CampaignId = l.HubSpot_Campaign__c; //You will have to know the ID or query for it or something cm.Status = 'Responded'; //Or whatever default status you want on the member insert cm; } } }
Any help would be much appreciated.
Kev
insert cMembers;
to
database.insert( cMembers, false);
All Answers
Thsi can be something list this
trigger {
//Create a Set with LeadId - Campaign Id
for(CampignMamber cM: [Select data from CampaignMember where LeadId IN: Trigger.newMap.keySet()]) {
setVar(cM.LeadId + '-' + cM.CampaignId);
}
Now loop through the trigger new records and check if LeadId-CampignId already exists in set, do nothing elase create a new record
I'm still struggling with this one.
I understand the concept of creating a list of campaigns the Member is associated to but I need to filter this list to where the campaign id = hupspot_campaign field. Then if the list is empty do the follow else do nothing.
Any further help would really be appreciated.
Thanks
Kevin
Ok made a step forward by doing the below:
If someone could have look over and see if there is anything functionally wrong it would be really helpful.
Thanks
Kevin
I would suggest to re-read my earlier post. What all you need to do is, Create a set of all the existing leads with CampaignId. Set helps in maintain the uniquess.
Now when your are looping through your Trigger.New, fist check if that record already exist in Set. If yes, that means Lead is already a campaign member. elase you can create a new record
Hi Bhawani, thanks for your response. I'm pretty new to this so still trying to learn. I'll have a look and see if I can figure out what you're saying.
Kevin
Hi Kevin, I tried to comment as much as possible. Hopefully this will be helpful for you:
I seem to be getting the error with this:
Awesome thankyou. I'll have a look now and see if it makes sense.
Just tried it now and get the error from this line of code:
Error: Compile Error: unexpected token: if at line 44 column 4
Ok fixs this as it was just in the wrong position but now i get the error from cm = new CampaignMember(); in this line of code:
Sorry should say error is:
Variable does not exist: cm at line 32 column 17
I tweaked the code as there was a { missing after an if statement:
While it saves if I try to add the campaignid to l.hubspot_campaign to a lead who is already in the campaign I get the following error:
Error:Apex trigger LeadToCampaign caused an unexpected exception, contact your administrator: LeadToCampaign: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This entity is already a member of this campaign: []: Trigger.LeadToCampaign: line 46, column 1
insert cMembers;
to
database.insert( cMembers, false);
Excellent that fixed the issue!
Here's the full code for anyone else:
Bhawani - thank you for your help and description! Has really helped with understanding how to generate lists and use them effectively. I marked your last post as the accepted solution to show your help and gave you some kudos!