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
sales4cesales4ce 

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

Best Answer chosen by Admin (Salesforce Developers) 
incuGuSincuGuS

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

incuGuSincuGuS

Your approach of checking if the "Special Campaign" checkbox is checked is valid.

 

To get started on Triggers :

 

 

trigger NewTrigger on myCustomObject__c (after insert) {
    for(myCustomObject__c obj: Trigger.new){
        // work with object. ie:
        if(obj.Special_Campaign == true) {
             // do something.
        }

    }
}

 

 

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.

sales4cesales4ce

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

incuGuSincuGuS

I already replied to your other post :)

sales4cesales4ce

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

incuGuSincuGuS

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.

This was selected as the best answer
sales4cesales4ce

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:

trigger add_task on CampaignMember (before Insert) 
{
    Campaign c = [select Id,Special_Campaign__c from Campaign where Id = :Trigger.new[0].CampaignId];
    
    If(c.Special_Campaign__c==True)
    {
    List<Id> Contact_Lead_Ids= new List<Id>();
    
    for(CampaignMember cm : Trigger.New)
    {
        Contact_Lead_Ids.add(cm.ContactId);
        Contact_Lead_Ids.add(cm.LeadId);
    }
    
    List<Contact> contacts=[Select Id from Contact where Id in :Contact_Lead_Ids];
    Map<Id,Contact> Cid_to_Sobject= new Map<Id,Contact>();
    
    List<Lead> leads=[select Id from Lead where Id IN :Contact_Lead_Ids];
    Map<Id,Lead> Lid_to_Sobject=new Map<Id,Lead>();
    
    for(Contact ct : contacts)
        Cid_to_Sobject.Put(ct.Id,ct);
    
    for(Lead l : leads)
        Lid_to_Sobject.Put(l.Id,l);
    
    for(CampaignMember cm2 : Trigger.New)
    {
        if(cm2.ContactId!=NULL)
        {
            Contact ct2=Cid_to_Sobject.get(cm2.ContactId);
            Task myTask=new Task(Whoid=ct2.Id,Subject='Testing contact');
            Insert myTask;
            System.Debug('Task inserted to=='+ct2.Id);
        }
        else
        {
            Lead l2=Lid_to_Sobject.get(cm2.LeadId);
            Task myTask=new Task(WhoId=l2.Id,Subject='Testing Lead');
            Insert myTask;
            System.Debug('Task Inserted to Lead=='+l2.Id);
        }
    }
   
}
}

 

Here is the Test Class to cover the trigger. This is where i am stuck!

Public Class trigger_Testcls
{
    Static testMethod Void test_CM_Trigger()
    {
        Campaign camp=new Campaign(Name='Test Campaign',Special_Campaign__c=True,IsActive=True);
        List<Contact> ct= new List<contact>();
        List<Lead> ld=new List<lead>();
        
        for(Integer i=0;i<100;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact');
            ct.add(c);
            Lead l= New Lead(LastName='Test',Status='Open - Not Contacted',Company='Leads');
            ld.add(l);
        }
        Insert ct;
        Insert ld;
        
        List<CampaignMember> cm=New List<CampaignMember>();
        
        for(Integer i=0;i<100;i++)
        {
            CampaignMember Cl=New CampaignMember(CampaignId=camp.Id);
            cm.add(Cl);
        }
        Test.StartTest();
        Insert cm;
        Test.StopTest();
    }
}

Many Thanks for your help,

Kumar

 

incuGuSincuGuS

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.

sales4cesales4ce

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)

Public Class trigger_Testcls
{
    Static testMethod Void test_CM_Contacts()
    {
        Campaign camp=new Campaign(Name='Test Campaign',Special_Campaign__c=True,IsActive=True);
        Insert camp;
        List<Contact> ct= new List<contact>();
      
        
        for(Integer i=0;i<200;i++)
        {
            Contact c=New Contact(FirstName='Test',LastName='Contact');
            ct.add(c);
           
        }
        Insert ct;
        
        
        List<CampaignMember> cm=New List<CampaignMember>();
        
        for(Integer i=0;i<200;i++)
        {
            CampaignMember Cts=New CampaignMember(CampaignId=camp.Id, ContactId=ct[i].Id);
            cm.add(Cts);
           
            
        }
        
        
        Test.StartTest();
        Insert cm;
        Test.StopTest();
        
        List<Task> inserted_Tasks= [Select Subject from Task where Whoid IN : ct];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Testing contact',t.Subject);
        }
        
    }
    
    Static testMethod Void test_CM_leads()
    {
        Campaign Camp=new Campaign(Name='Lead Campaign',Special_Campaign__c=True,isActive=True);
        Insert Camp;
        
        List<Lead> l= New List<Lead>();
        for(Integer i=0;i<200;i++)
        {
            Lead ld=New Lead(LastName='Testing Lead',Company='Leads');
            l.add(ld);
        }
        Insert l;
        
        List<CampaignMember> cm= New List<CampaignMember>();
        for(Integer i=0;i<200;i++)
        {
            CampaignMember c= New CampaignMember(CampaignId=camp.Id, LeadId=l[i].Id);
            cm.add(c);
        }
        Test.StartTest();
        Insert cm;
        Test.StopTest();
        List<Task> inserted_Tasks= [Select Subject from Task where Whoid IN : l];
        for(Task t : inserted_Tasks)
        {
            System.assertEquals('Testing Lead',t.Subject);
        }
    }
}

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

incuGuSincuGuS

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.

sales4cesales4ce

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