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
dev_sfdc1dev_sfdc1 

Task is not created in Trigger

Hi,
I'm new to trigger concepts.My requirement is if any CampaignMember is created for particular two Campaign records,task is created to that contacts.For me, task is not created in this trigger.Please point me out where I did mistake.

Trigger:

trigger add_task on CampaignMember (before Insert) 
{
    List<Campaign> c = [select Id,Name from Campaign where Id = :Trigger.new[0].CampaignId]; 
    List<Campaign> lstc = new List<Campaign>();
    
    for(Campaign cpn : c)
    {
    if(cpn.Name == 'First Mail' || cpn.Name == 'Second Mail' )
       
        lstc.add(cpn);
    }
  
    List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc];
    List<Id> Contact_Lead_Ids= new List<Id>();
    for(CampaignMember cm : lstcm)  
    
        Contact_Lead_Ids.add(cm.ContactId);
        System.Debug('ContactId=='+Contact_Lead_Ids);     

    List<Contact> contacts=[Select Id from Contact where Id in :Contact_Lead_Ids];  
    List<Contact> cidcon = new List<Contact>();
    for(Contact ct : contacts)       
       cidcon.add(ct);
       
    List<CampaignMember> lstmember = [Select id,CampaignId,ContactId,Status from CampaignMember where ContactId =: cidcon];
    
    for(CampaignMember cm2 : lstmember) 
    {
        if(cm2.ContactId!=NULL & cm2.Status == 'Responded')
        {
           
            Task myTask=new Task(Whoid=cm2.ContactId,Subject='Testing Task Created');
            Insert myTask;
            System.Debug('Task inserted to=='+myTask);
        }
    
    }
    
   

}

Thank You
Naveen Rahul 3Naveen Rahul 3
Hi dev_sfdc1,

can you check the Below code,

its working for me,have made slight changes,please check and let me know.

trigger add_task on CampaignMember (After Insert) 
{

    List<Campaign> c = [select Id,Name from Campaign where Id = :Trigger.new[0].CampaignId]; 
    List<Campaign> lstc = new List<Campaign>();
    
    for(Campaign cpn : c)
    {
    if(cpn.Name == 'International Electrical' || cpn.Name == 'Second Mail' )
       
        lstc.add(cpn);
    }
  
  
    System.debug('Coming here'+lstc);
    System.debug('Coming here'+lstc[0].Id);
    List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc[0].Id];
    List<Id> Contact_Lead_Ids= new List<Id>();
    
     System.debug('Coming here'+lstcm);
    
    for(CampaignMember cm : lstcm)  
    
        Contact_Lead_Ids.add(cm.ContactId);
        System.Debug('ContactId=='+Contact_Lead_Ids);     

    List<Contact> contacts=[Select Id from Contact where Id in :Contact_Lead_Ids];  
    List<Contact> cidcon = new List<Contact>();
    for(Contact ct : contacts)       
       cidcon.add(ct);
       
    List<CampaignMember> lstmember = [Select id,CampaignId,ContactId,Status from CampaignMember where ContactId =: cidcon];
    System.Debug('ContactId=='+cidcon);     
    for(CampaignMember cm2 : lstmember) 
    {
        if(cm2.ContactId!=NULL & cm2.Status == 'Responded')
        {
           
            Task myTask=new Task(WhoID=cm2.ContactId,Subject='Testing Task Created');
            Insert myTask;
            System.Debug('Task inserted to=='+myTask);
        }
    
    }
    
   

}

Thanks
D Naveen Rahul.
Naveen Rahul 3Naveen Rahul 3

I have tested for single/multiple contact its working fine,

Task myTask=new Task(Whoid=cm2.ContactId,Subject='Testing Task Created')
Task myTask=new Task(WhoID=cm2.ContactId,Subject='Testing Task Created');

please check the above difference.

Thanks 
D Naveen rahul.
 

dev_sfdc1dev_sfdc1
Thanks for your reply Naveen. I'm Getting error for this below line, if I select anyother Campaign other than 2 hardcoded values.
execution of AfterInsert caused by: System.ListException: List index out of bounds: 0: Trigger.add_task: line 18, column 1
List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc[0].Id];

Please help for this error. And also can you please explain concept of choosing AfterInsert Event here.

Thank You
Naveen Rahul 3Naveen Rahul 3

Sorry dev_sfdc1,

 

that has to be 

List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc];

Thansk
D naveen rahul.
 

Naveen Rahul 3Naveen Rahul 3
trigger add_task on CampaignMember (After Insert) 
{

    List<Campaign> c = [select Id,Name from Campaign where Id = :Trigger.new[0].CampaignId]; 
    List<Campaign> lstc = new List<Campaign>();
    
    for(Campaign cpn : c)
    {
     //you getting error becaue these hardcoded campaign names.
    //if(cpn.Name == 'International Electrical' || cpn.Name == 'Second Mail' )
       
        lstc.add(cpn);
    //}
  
  
    System.debug('Coming here'+lstc);
    System.debug('Coming here'+lstc[0].Id);
    List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc[0].Id];
    List<Id> Contact_Lead_Ids= new List<Id>();
    
     System.debug('Coming here'+lstcm);
    
    for(CampaignMember cm : lstcm)  
    
        Contact_Lead_Ids.add(cm.ContactId);
        System.Debug('ContactId=='+Contact_Lead_Ids);     

    List<Contact> contacts=[Select Id from Contact where Id in :Contact_Lead_Ids];  
    List<Contact> cidcon = new List<Contact>();
    for(Contact ct : contacts)       
       cidcon.add(ct);
       
    List<CampaignMember> lstmember = [Select id,CampaignId,ContactId,Status from CampaignMember where ContactId =: cidcon];
    System.Debug('ContactId=='+cidcon);     
    for(CampaignMember cm2 : lstmember) 
    {
        if(cm2.ContactId!=NULL & cm2.Status == 'Responded')
        {
           
            //Here you have added WhoID as Whoid 
            Task myTask=new Task(WhoID=cm2.ContactId,Subject='Testing Task Created');
            Insert myTask;
            System.Debug('Task inserted to=='+myTask);
        }
    
    }
    
   

}
i have tryed with after insert becuase well we get a complete data when we complete an circle thats why,and more over we don have any depency.

please marked it close if it helps.


Thanks
D Naveen rahul.
Naveen Rahul 3Naveen Rahul 3
u got them working ??
dev_sfdc1dev_sfdc1
Hi Naveen,

Thanks for your continuous help.Actually my requirement is needed for that two hardcoded values and checking null conditions.
I have done that checking null conditions as below 
if(!lstc.isEmpyty()) 

Anyway thank you again..