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
Rbb24Rbb24 

Trigger not working

Hello,

I am fairly new to apex and triggers, and I am having a hard time getting this particular trigger to do what I need it too.  I have looked over it several times and can't understand why it isn't working as I expect.  Any help is greatly appreciated!!

trigger isNewLeadCreated on Campaign_Influence__c (before insert) {
    for(Campaign_Influence__c CI: Trigger.new) {
        if(CI.LeadID__c != null) {
          List<Campaign_Influence__c> siblings = [Select id, new_lead_created__c, campaign_add_date__c from campaign_influence__c where LeadID__c = :CI.LeadID__c order by new_lead_created__c desc];
          List<Lead> parent = [Select id, SR_date_created__c from Lead where id = :CI.LeadID__c];        
        
        
        //If campaign is first campaign, not of the 3 types, not a list, and not setter contact without set.....True
        if(siblings.size() == 0 
            && (CI.campaign__r.type != 'List Upload' 
                || CI.campaign__r.type != 'Other' 
                || CI.campaign__r.type != 'Pricing')
            && CI.campaign__r.List__c != true
            && CI.campaign__r.ADR_Set__c != true ) {
        CI.new_lead_created__c = true;
        }
        
        //If campaign is not first campaign, new lead never checked on other campaigns, not of the 3 types, not a list, and not setter contact without set.....True
        if(siblings.size() > 0
            && Siblings[0].new_lead_created__c != true
            && (CI.campaign__r.type != 'List Upload' 
                || CI.campaign__r.type != 'Other' 
                || CI.campaign__r.type != 'Pricing')
            && CI.campaign__r.List__c != true
            && CI.campaign__r.ADR_Set__c != true ){
        CI.new_lead_created__c = true;
        }    
        
        if(siblings.size() > 0
            && Siblings[0].new_lead_created__c == true
            && CI.campaign__r.type == 'Cold Call') {
        CI.new_lead_created__c = true;
        Siblings[0].new_lead_created__c = false;
        Update Siblings[0];
        }               
}
}
}


Best Answer chosen by Rbb24
kaustav goswamikaustav goswami
See if this helps. Please refer to the inline comments.

trigger isNewLeadCreated on Campaign_Influence__c (before insert){
// those campaign influence records that have a lead id populated will be selected and stored in this list
List<Campaign_Influence__c> qualifiedCampInf = new List<Campaign_Influence__c>();
// the list of Lead record ids for the selected campaign influence records
List<Id> leadIds = new List<Id>();
// the list of campaign ids for the selected camapign influence records
List<Id> campIds = new List<Id>();
// a map to hold the id and the required camapign fields for the selected camapigns
Map<Id, Campaign> mapCamp;
// query and get all the sibling records for the slected campaign influence records
List<Campaign_Influence__c> siblingCampInf = new List<Campaign_Influence__c>();
// a map of the lead id to which a camapaign influence is related and the other sibling camapign influence records with the same lead id
Map<Id, List<Campaign_Influence__c>> mapLeadToCampInf = new Map<Id, List<Campaign_Influence__c>>();
// the final list to which you should add the campaign influence records this will be used to update records in the database
List<Campaign_Influence__c> updateCampInf = new List<Campaign_Influence__c>();

// first step - iterate and choose the CI records that have a Lead Id populated
for(Campaign_Influence__c CI : Trigger.new){
  if(CI.LeadID__c != null){
   qualifiedCampInf.add(CI);
   leadIds.add(CI.LeadID__c);
   campIds.add(CI.Campaign__c);
  }
}

if(qualifiedCampInf.size() > 0){
  // query and get all the sibling records for the selected CI records
  siblingCampInf = [SELECT Id, new_lead_created__c, campaign_add_date__c FROM campaign_influence__c WHERE LeadID__c IN :leadIds];
  // query and get all the related campaign records with the required fields
  mapCamp = new Map<Id, Campaign>([SELECT Id, Type, List__c, ADR_Set__c FROM Campaign WHERE ID IN :campIds]);
  // prepare a map between the lead id and the sibling CI records
  for(Campaign_Influence__c CI : siblingCampInf){
   List<Campaign_Influence__c> tempCampList = new List<Campaign_Influence__c>();
   tempCampList = mapLeadToCampInf.get(CI.LeadId__c)
   tempCampList.add(CI);
   mapLeadToCampInf.put(CI.LeadId__c, tempCampList);  
  }
 
  // now iterate over the selected CI records
  // get the corresponding camapign record from the map of camapaign and the sibling records from the map of
  // lead to list of sibling records
  for(Campaign_Influence__c CI : qualifiedCampInf){
   Campaign camp = new Campaign();
   camp = mapCamp.get(CI.Campaign__c);
   List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
   siblingToThisCI = mapLeadToCampInf.get(CI.LeadID__c);
  
   if(siblingToThis.size() > 0 && camp != null){
    // your if else logic - here perform all the checking for the if else logic as necessary
    // add the required reccords to the "updateCampInf" list based on your conditions
   
   }
  }
}

// if there are records present in the to be updated list then perform the DML
if(updateCampInf.size() > 0){
  try{
   update updateCampInf;
  }catch(Exception ex){
   System.debug('#### Error while updating the camapaign influence records #### '  +ex.getMessage());
  }
}
}

All Answers

AshwaniAshwani
If you can post what this trigger does and waht exception you are getting then it may be helpful to resolve your problem. Don't expect someone will read the code and try to know the issue.
Matt WhalleyMatt Whalley
From reading the trigger, I can't tell what you want it to do.  I also noticed that you have two SOQL queries in a trigger.new for loop - which is a bad in triggers.  You only get so many SOQL queries per context.  If someone did a bulk operation or more than 100 records, the transaction would fail.  It's also a good idea to do your DML outside of the for loop.

AshlekhAshlekh
Hi,

You really need to post your requirment and this trigger is not good as many soql in for loop.
kaustav goswamikaustav goswami
Your trigger should have a structure similar to this.

trigger isNewLeadCreated on Campaign_Influence__c (before insert){
List<Campaign_Influence__c> qualifiedCampInf = new List<Campaign_Influence__c>();
List<Id> leadIds = new List<Id>();
List<Campaign_Influence__c> siblingCampInf = new List<Campaign_Influence__c>();
// this will hold the lead id and the corresponding latest campaign influence record ordered by the required field
Map<Id,Campaign_Influence__c> mapLeadToCampInf = new Map<Id,Campaign_Influence__c>();

// first step choosing the CI records that have a Lead Id populated
for(Campaign_Influence__c CI : Trigger.new){
  if(CI.LeadID__c != null){
   qualifiedCampInf.add(CI);
   leadIds.add(CI.LeadID__c);
  }
}

if(qualifiedCampInf.size() > 0){
  siblingCampInf = [SELECT Id, new_lead_created__c, campaign_add_date__c FROM campaign_influence__c WHERE LeadID__c IN :leadIds];
 
  if(siblingCampInf.size() > 0){
   // logic here
  }else {
   // logic here
  }
 
}
}

Also note that you are ordering by a boolean field in the SOQL query. Not sure if that is what you want.

Post the requirement along with the object relations and then we can provide you with a sample code.

Thanks,
Kaustav
Marcio FritschMarcio Fritsch
try to put "List<Lead> parent = [Select id, SR_da..." to outside "for" statement. And preferentially create you workflow inside a separete class.
Marcio FritschMarcio Fritsch
You should not use fix "Siblings[0]", for instance. I bet it's being your issue.
Matt WhalleyMatt Whalley
The CI.Campaign__r field values do not come in with the trigger.new list.  You will have to query the fields to get those values.
kaustav goswamikaustav goswami
See if this helps. Please refer to the inline comments.

trigger isNewLeadCreated on Campaign_Influence__c (before insert){
// those campaign influence records that have a lead id populated will be selected and stored in this list
List<Campaign_Influence__c> qualifiedCampInf = new List<Campaign_Influence__c>();
// the list of Lead record ids for the selected campaign influence records
List<Id> leadIds = new List<Id>();
// the list of campaign ids for the selected camapign influence records
List<Id> campIds = new List<Id>();
// a map to hold the id and the required camapign fields for the selected camapigns
Map<Id, Campaign> mapCamp;
// query and get all the sibling records for the slected campaign influence records
List<Campaign_Influence__c> siblingCampInf = new List<Campaign_Influence__c>();
// a map of the lead id to which a camapaign influence is related and the other sibling camapign influence records with the same lead id
Map<Id, List<Campaign_Influence__c>> mapLeadToCampInf = new Map<Id, List<Campaign_Influence__c>>();
// the final list to which you should add the campaign influence records this will be used to update records in the database
List<Campaign_Influence__c> updateCampInf = new List<Campaign_Influence__c>();

// first step - iterate and choose the CI records that have a Lead Id populated
for(Campaign_Influence__c CI : Trigger.new){
  if(CI.LeadID__c != null){
   qualifiedCampInf.add(CI);
   leadIds.add(CI.LeadID__c);
   campIds.add(CI.Campaign__c);
  }
}

if(qualifiedCampInf.size() > 0){
  // query and get all the sibling records for the selected CI records
  siblingCampInf = [SELECT Id, new_lead_created__c, campaign_add_date__c FROM campaign_influence__c WHERE LeadID__c IN :leadIds];
  // query and get all the related campaign records with the required fields
  mapCamp = new Map<Id, Campaign>([SELECT Id, Type, List__c, ADR_Set__c FROM Campaign WHERE ID IN :campIds]);
  // prepare a map between the lead id and the sibling CI records
  for(Campaign_Influence__c CI : siblingCampInf){
   List<Campaign_Influence__c> tempCampList = new List<Campaign_Influence__c>();
   tempCampList = mapLeadToCampInf.get(CI.LeadId__c)
   tempCampList.add(CI);
   mapLeadToCampInf.put(CI.LeadId__c, tempCampList);  
  }
 
  // now iterate over the selected CI records
  // get the corresponding camapign record from the map of camapaign and the sibling records from the map of
  // lead to list of sibling records
  for(Campaign_Influence__c CI : qualifiedCampInf){
   Campaign camp = new Campaign();
   camp = mapCamp.get(CI.Campaign__c);
   List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
   siblingToThisCI = mapLeadToCampInf.get(CI.LeadID__c);
  
   if(siblingToThis.size() > 0 && camp != null){
    // your if else logic - here perform all the checking for the if else logic as necessary
    // add the required reccords to the "updateCampInf" list based on your conditions
   
   }
  }
}

// if there are records present in the to be updated list then perform the DML
if(updateCampInf.size() > 0){
  try{
   update updateCampInf;
  }catch(Exception ex){
   System.debug('#### Error while updating the camapaign influence records #### '  +ex.getMessage());
  }
}
}
This was selected as the best answer
kaustav goswamikaustav goswami
Were there any records retrieved in the SOQL queries?

Thanks,
Kaustav
kaustav goswamikaustav goswami
In this part of the code I am trying to create a map between the Lead Id and the existing campaign influence records.

Let us say a new camp influence record is getting created c1. It is associated to lead L1. C2, C3 are already existing cmpaign influence records that are linked to the lead L1. The map will containn the id to campaign influence mapping in this fashion - 

L1.Id(key) --> C1, C2.

Can you please try the following code. I have replaced the key with the Lead__c field and have now checked for the existence of the key using the containsKey() method.

for(Campaign_Influence__c CI : siblingCampInf){
   if(mapLeadToCampInf.containsKey(CI.Lead__c)){
    List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
    siblingToThisCI.get(CI.Lead__c);
    siblingToThisCI.add(CI);
    mapLeadToCampInf.put(CI.Lead__c, siblingToThisCI);
   }else{
  List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
  siblingToThisCI.add(CI);
  mapLeadToCampInf.put(CI.Lead__c, siblingToThisCI);
   } 
  }

Please let me know if this works.

Thanks,
Kaustav
kaustav goswamikaustav goswami
Sorry. That was a mistake.

for(Campaign_Influence__c CI : siblingCampInf){
   if(mapLeadToCampInf.containsKey(CI.Lead__c)){
    List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
    siblingToThisCI = mapLeadToCampInf.get(CI.Lead__c);
    siblingToThisCI.add(CI);
    mapLeadToCampInf.put(CI.Lead__c, siblingToThisCI);
   }else{
  List<Campaign_Influence__c> siblingToThisCI = new List<Campaign_Influence__c>();
  siblingToThisCI.add(CI);
  mapLeadToCampInf.put(CI.Lead__c, siblingToThisCI);
   }
  }

Please try this.

Thanks,
Kaustav