+ Start a Discussion
Esti LeiserEsti Leiser 

error trying to bulkify a trigger

Hi!

I'm trying to bulkify a trigger, and am getting an error I don't understand.

Here's the trigger:
trigger AddWebLeadToTripCampaign on Lead (after insert) {
    List<CampaignMember> CMToInsert = new List<CampaignMember> ();
    CampaignMember[] cms = new CampaignMember[0];
    for(Lead record:Trigger.new)
        try{
            if(!String.isBlank(record.Trip_Name__c)) {
                String cname = record.Trip_Name__c+' '+record.Trip_Date__c;
                cname = cname.trim();
                //System.debug('the value of cname is >>>>> ' + cname);
                
                for(Campaign CID : [select id, name from Campaign where name = :cname]){
                    cms.add(new CampaignMember(LeadId=record.Id,CampaignId=CID.id));
                }
                
            }
        CMToInsert.add(cms);
        }catch (Exception e){
            system.debug('The following exception has occurred: ' + e.getMessage());
        }
    insert CMToInsert;
}
The error I'm getting is
Incompatible element type LIST<CampaignMember> for collection of SOBJECT:CampaignMember

Any idea what I'm doing wrong?
Thanks!
Best Answer chosen by James Loghry
BalajiRanganathanBalajiRanganathan
Currently, the campaign query is inside for loop of Trigger.new

Try this 
trigger AddWebLeadToTripCampaign on Lead (after insert) {

  List<CampaignMember> CMToInsert = new List<CampaignMember> ();
  Set<String> campaignNameSet = new Set<String>();

  for(Lead record:Trigger.new) {
    if (!String.isBlank(record.Trip_Name__c)) { 
      String cname = record.Trip_Name__c+' '+record.Trip_Date__c;
      campaignNameSet.add(cname.trim());
    } 
  }

  Map<String, Campaign> campaignMap = new Map<String, Campaign>();

  for (Campaign c : [select id, name from Campaign where name = :campaignNameSet]) {
    campaignMap.put(c.name, c);
  }

  for(Lead record:Trigger.new) {
    try{
      if(!String.isBlank(record.Trip_Name__c)) {
        String cname = record.Trip_Name__c+' '+record.Trip_Date__c;
        //System.debug('the value of cname is >>>>> ' + cname);
    	Campaign CID = campaignMap.get(cname.trim());
	    CMToInsert.add(new CampaignMember(LeadId=record.Id,CampaignId=CID.id));
      }
    }catch (Exception e){
      system.debug('The following exception has occurred: ' + e.getMessage());
    }
    insert CMToInsert;
  }
}

All Answers

Esti LeiserEsti Leiser
Thanks! Now I'm getting a new error:
Method does not exist or incorrect signature: [SOBJECT:CampaignMember].add(SOBJECT:CampaignMember)
on line 12 (cms.add....)
 
BalajiRanganathanBalajiRanganathan
You dont need the variable cms. 
instead of cms.add call CMToInsert.add and remove CMToInsert.add(cms); on the line 16 that you have posted.

also you can avoid the query inside for loop. are you going to have multiple campaign with same name or only one campaign for one name?
Esti LeiserEsti Leiser
I made those two changes, and now I'm getting
Illegal variable declaration: CMToInsert.add
There will not be multiple campaigns with the same name. Which for loop am I trying to get rid of?
 
BalajiRanganathanBalajiRanganathan
Currently, the campaign query is inside for loop of Trigger.new

Try this 
trigger AddWebLeadToTripCampaign on Lead (after insert) {

  List<CampaignMember> CMToInsert = new List<CampaignMember> ();
  Set<String> campaignNameSet = new Set<String>();

  for(Lead record:Trigger.new) {
    if (!String.isBlank(record.Trip_Name__c)) { 
      String cname = record.Trip_Name__c+' '+record.Trip_Date__c;
      campaignNameSet.add(cname.trim());
    } 
  }

  Map<String, Campaign> campaignMap = new Map<String, Campaign>();

  for (Campaign c : [select id, name from Campaign where name = :campaignNameSet]) {
    campaignMap.put(c.name, c);
  }

  for(Lead record:Trigger.new) {
    try{
      if(!String.isBlank(record.Trip_Name__c)) {
        String cname = record.Trip_Name__c+' '+record.Trip_Date__c;
        //System.debug('the value of cname is >>>>> ' + cname);
    	Campaign CID = campaignMap.get(cname.trim());
	    CMToInsert.add(new CampaignMember(LeadId=record.Id,CampaignId=CID.id));
      }
    }catch (Exception e){
      system.debug('The following exception has occurred: ' + e.getMessage());
    }
    insert CMToInsert;
  }
}
This was selected as the best answer
Esti LeiserEsti Leiser
Cool - that did it! Many thanks!!
Esti LeiserEsti Leiser
Uh Oh!
I'm trying to upload some new leads, and now I get this error:
 
"AddWebLeadToTripCampaign: execution of AfterInsert

caused by: System.DmlException: Insert failed. First exception on row 0 with id 00v8000001TcIvkAAF; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Trigger.AddWebLeadToTripCampaign: line 30, column 1"

Any ideas? Thanks!
 
BalajiRanganathanBalajiRanganathan
Move the line no 30(insert CMToInsert;) to after line number 31. the insert is inside for loop. we have to move it out of the for loop
Esti LeiserEsti Leiser
That did it - thanks!