+ Start a Discussion
JesseRCJesseRC 

Insert CampaignMember object from lead before trigger

I have a before insert on the lead object. I am using "before" because I am changing lead owner based on campaign that cannot be done in an after trigger. If i'm wrong about that please let me know. 

 

My basic flow is:

 

  1. lead comes in, before trigger activated
  2. using lead information to find correct campaign
  3. use campaign information to update lead owner
  4. Need to associate lead to campaign by creating CampaignMember object

 

But I cannot insert any CampaignMember objects that I create because this is a before trigger and the leads dont have Id's yet. 

 

Any thoughts on how i can accomplish this all from the same trigger? Right now i'm working on storing the campaign Id in a dummy field on the lead so that an after trigger can find it and create and insert a campaignmember object. Doing this so that i don't have the same query (finding the campaign) in two different places.  

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

You can change owner in an After Insert too, only difference will be the number of queries you are  firing and the DML you are issusing.

In before insert you and just assigning the ownerid a value and it is set without any extra DML statement.

 

In After insert you have the ID of the Lead just insert , then you need to issue a select statement , fetch the lead record and then change the ownerid accordingly and then issue the update command on the lead record thus fetched and also since now you have the id of the lead you can also create Lead Menber.

So the naswer to your problem would be to write an after insert trigger purely or may be capture both before and after insert, in before insert change the owner id and in after insert you will have the id and you csn create the campaign members.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Ispita_NavatarIspita_Navatar

You can change owner in an After Insert too, only difference will be the number of queries you are  firing and the DML you are issusing.

In before insert you and just assigning the ownerid a value and it is set without any extra DML statement.

 

In After insert you have the ID of the Lead just insert , then you need to issue a select statement , fetch the lead record and then change the ownerid accordingly and then issue the update command on the lead record thus fetched and also since now you have the id of the lead you can also create Lead Menber.

So the naswer to your problem would be to write an after insert trigger purely or may be capture both before and after insert, in before insert change the owner id and in after insert you will have the id and you csn create the campaign members.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
JesseRCJesseRC

That makes sense. I ended up maintaining a short after insert trigger to create my campaignMember. In the future i might combine it all into one after trigger, would just have to keep an eye on the queries. Thanks.