You need to sign in to do that
Don't have an account?
dev_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
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
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.
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.
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
Sorry dev_sfdc1,
that has to be
List<CampaignMember> lstcm = [Select id,CampaignId,ContactId,Status from CampaignMember where CampaignId =: lstc];
Thansk
D naveen rahul.
please marked it close if it helps.
Thanks
D Naveen rahul.
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..