You need to sign in to do that
Don't have an account?
Bharat.Sharma
Automate the Process of add to Campaign- Salesforce
hello Everyone,
actually i want to automate the process of 'add to campaign member' but i am getting some error.
Here are the scenario details :
Whenever any lead enters in the database with LeadSource = Linkedin (PicklistValue) it should automatically get added to the particular campaign which will be having a specific campaign id.
I think this secnario can handle with Trigger , Flows and Process Builder
I have tried Everything to implement this secnario but i am getting Some errors. please help me to complete this automation.
Any help will be appreciable :)
here is the Trigger
Flow
And Process builder also showing some Error
please help me
Thanks
Bharat Sharma
actually i want to automate the process of 'add to campaign member' but i am getting some error.
Here are the scenario details :
Whenever any lead enters in the database with LeadSource = Linkedin (PicklistValue) it should automatically get added to the particular campaign which will be having a specific campaign id.
I think this secnario can handle with Trigger , Flows and Process Builder
I have tried Everything to implement this secnario but i am getting Some errors. please help me to complete this automation.
Any help will be appreciable :)
here is the Trigger
trigger add_member_to_camp on Lead (before insert,after insert) { List<CampaignMember> members = new List<CampaignMember>(); for(lead l : Trigger.new){ if(l.LeadSource == 'linkedin'){ CampaignMember cm = new CampaignMember(CampaignId = '70128000000Btjj'); } try{ insert members; } catch(DmlException e) { System.debug('An unexpected error has occured: ' + e.getMessage()); } } }
Flow
And Process builder also showing some Error
please help me
Thanks
Bharat Sharma
I checked your trigger. the important points are
- In before trigger context, DML statements cannot be operated.
- In your case, trigger should be called only after context. (Now, your trigger is called twice!)
- Campaign members should be added to list.
- LeadId is required for campaign member.
Try below code! Be careful if you develop this trigger in a sandbox org because campaign id will be changed in the production org. And I will check your flow later!
All Answers
I checked your trigger. the important points are
- In before trigger context, DML statements cannot be operated.
- In your case, trigger should be called only after context. (Now, your trigger is called twice!)
- Campaign members should be added to list.
- LeadId is required for campaign member.
Try below code! Be careful if you develop this trigger in a sandbox org because campaign id will be changed in the production org. And I will check your flow later!
Shun Kosaka !
Is there any other option to Specify the Campaign Without id ??
by the way Its working now :)
That's good!
I guess there are some options to specify the record.
- Create unique and required custom field.
- Use custom metadata types/custom settings that have a mapping of sandbox and production record id
etc...
I also looked over the flow. I think a governor limitation error occurs due to the record creation in the loop.
You can use collection variables same as the trigger.
https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_designer_resources_coll_var_pop.htm
one more thing,
I have created a custom field on lead page named as "First_touch_Campaign__c". As name suggest this field will be filled up automatically with the same campaign name in which lead is added as a campaignMember for the very first time. this field should get locked or no further editing or any updation could takes place. Basically i want this field to get the data once only and that too at the time of lead creation only when the lead will get associated to the campaign in the very beginning.
Can we add this feature in same trigger that we have talked about earlier.
Hi Bharat,
Set field level security of 'First_touch_Campaign__c' as read only and check the result with the profile other than system admin. And tell me if you don't get expected result!It can be realized like below. In before trigger context, triggered records can be modified directly.
for such a fast Reply !!
When i am Passing Query
'l.First_touch_Campaign__c = SELECT Name FROM Campaign WHERE Id = '70128000000Btjj'' insted of String. getting error like Illegal assignment from List<Campaign> to String.
Type of query results is sObject or List of sObject. The following is an another example. See also,
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_working_with_results.htm
Good luck!
Everything is working fine :)
One last thing,
Could you please help me to develope test class for the same trigger.
Thanks for your Support.