You need to sign in to do that
Don't have an account?

Campaign Trigger-Help needed!
Hi,
I am thinking if my idea is possible or not.
Can we write trigger on a campaign that processes only Campaigns that are marked "Special".
I have created a custom field called "Special Campaign" which is a checkbox.
Now when ever that checbox is checked and whenever users add Contacts or Leads to that Special campaign i need to create a task.Also i need to make sure i do not add duplicate contacts/leads.
I am guessing that we need to write a trigger to achieve this, but a trigger works on all campaigns. How do i make it work only for campaigns marked "Special"?
Or should i write a trigger and at first check if the checkbox is checked and then do processing as per business logic or any other way.
Any idea/start up code is highly appreciated.
Thanks,
Sales4ce
Sorry for the late reply! didnt realize you replied, hope you check back here.
Write it on campaign member and through the relationship check that the campaigns has "special" field, if so when a contact/lead is added do your magic. if not , do nothing.
All Answers
Your approach of checking if the "Special Campaign" checkbox is checked is valid.
To get started on Triggers :
Thats a very basic trigger structure. To learn more about triggers consult the documentation:
http://www.salesforce.com/us/developer/docs/apexcode/index.htm
Under "Invoking Apex" -> "Triggers" you will find a lot of info.
If you have any specific doubts on how to code or what to do, let me know and i'll help you out :)
Gaston.
Hi Gaston,
Thanks for your quick reply and letting me know that i am heading in right direction.
i am stuck at the very beginning in the trigger.I already posted that as a new topic
http://community.salesforce.com/t5/Apex-Code-Development/Trigger-Not-compiling/td-p/179795
I dont know for some reason my trigger is not compiling. what could be the problem?
Thanks,
Sales4ce
I already replied to your other post :)
Thanks Gaston for your help!
But i have a question : should i write the trigger on CampaignMember or Campaign?
What i am looking is , whenever Contact/Lead is added to a campaign, i need to check if that campaign has an Checkbox ("Special") checked or not.
If checked, i need to create tasks for those contacts/leads.Also i need to make sure i do not have duplicates added.
if not checked, do nothing.
where should i write my trigger?
Thanks,
Sales4ce
Sorry for the late reply! didnt realize you replied, hope you check back here.
Write it on campaign member and through the relationship check that the campaigns has "special" field, if so when a contact/lead is added do your magic. if not , do nothing.
Hi Gaston,
Thanks for your reply!
Going through some of the others post, i came to know that i need to write it on Campaignmember.
Eventually i ended up writing on it and it compiles and runs successfully. I also tried to bulkify. Now i need to write a unit test method and see that apex trigger gets covered.
For this i am assuming that i need to insert a new campaign member object into a campaign that has that "Special" checked.
But for some reason i am stuck with how i can insert campaign member, so that it evetually fires off the trigger.
Can you help me in writing the test method.
Below is the trigger and a Test class to cover the trigger.
Also, can you let me know if my trigger works for Bulk uploads.
Here is the trigger:
Here is the Test Class to cover the trigger. This is where i am stuck!
Many Thanks for your help,
Kumar
Hey Kumar, im glad you got this working!
You need to add a Lead or Contact to the Campaign member you are creating , thats the only thing i see missing.
When you do :
CampaignMember Cl=New CampaignMember(C ampaignId=camp.Id);
Just add ContactId or LeadId to the Campaign member with the value from the Leads/Contacts you just inserted before.
Let me know how it goes!
Gaston.
Hi Gaston,
Thanks for all your help on this and pointing me in right direction.
I was able to develop the trigger and associated test method.
Here is the Test Method:(Might help others)
Also i might need your help for one of my other task.It is a webservice written in Java which connects to salesforce and does some processing on Leads.What i need to do is convert this webservice and implement using the functionality provided by salesforce itself, may be Batch Apex.
Let me know on this, then i can create a new post and explain all details.
Thanks,
Kumar
Happy to hear you got it working!
Sure go ahead, ill try helping you out. Im not too good at Java but ill give it a shot, if i can't im sure some other community member will be glad to help you out.
Also if you dont find any help here, try posting in the JAVA subforum. But lets try and see if we can do that.
Gaston.
Hi Gaston,
I feel really comfortable in this community for having helping folks like you!. It gives me more interest in learning and getting things done.
Before i go and post, i want to give you a background of what i want to accomplish.I sent you a private message on this.
Let me know.
Thanks,
Kumar