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
DennisYZFDennisYZF 

Works in Sandbox, fails in production!

OK, so here is my first time asking for help here - below is a trigger on lead, it should query a custom object, and if a match is found, update a lookup field on the lead with the custom object id - Works 100% in Sandbox, fails in production(does not populate all leads in a batch with matching custom object IDs, only about 70%)! Any recommendations?

Thanks!

 

 

trigger LeadSourceRef on Lead( before insert ) {

String DotString = '.';
String myString1;
String myString2 = '2';
String LSFormat;  

Map<String, Lead> LeadMap = new Map<String, Lead>();

Map<String , id> LSRmap= new  Map<String , id>();


System.debug( '<< LSR - Number of records in this trigger call: ' + Trigger.new.size() );
              
{  
    for( Lead record : Trigger.new )  
    {

    IF (  record.leadsource != Null  )
    {
    myString1 = record.leadsource;

   IF (! myString1.contains(DotString))
   LeadMap.put( record.LeadSource, record );  
   
    IF (  record.leadsource != Null && record.leadsource.contains ('2')  )
    {
    Integer IndexS =  myString1.indexOf(myString2, 0);
    LSFormat = myString1.substring (IndexS );
    LeadMap.put( LSFormat, record ); 
    }
    }
    }
  for(Lead_Source_Reference__c  thisLSR : [select Cookie_Identifier__c, id from Lead_Source_Reference__c where Cookie_Identifier__c in :LeadMap.keySet()])
{
    LSRMap.put(thisLSR.Cookie_Identifier__c,thisLSR.id);
    System.debug( '<<LSRMap ' + LSRMap.size() );
    System.debug( '<<LSRMap ' + LSRMap );
}

for(lead MyLead: Trigger.new){

  if (LSRmap.size() > 0  ) 
System.debug( '<<MyLead' + MyLead);
System.debug( '<<MyLead LeadSource' + MyLead.leadsource);
if (MyLead.leadsource != Null )
{
if (LSRmap.containskey (myString1) || LSRmap.containskey (LSFormat))

{
                      
                      IF (! MyLead.leadsource.contains(DotString))
                       {
                       MyLead.Lead_Source_Reference__c =  LSRmap.get(MyLead.leadsource);
                       }
                       

                       IF ( MyLead.leadsource.contains ('2') && MyLead.leadsource.contains(DotString) )
                       {
                         Integer IndexA =  MyLead.leadsource.indexOf('2', 0);
                        String LSFormatB = MyLead.leadsource.substring (IndexA );
                       MyLead.Lead_Source_Reference__c =  LSRmap.get(LSFormatB); 
                       }
               
               }
         
           }
           }
           }
           }
Best Answer chosen by Admin (Salesforce Developers) 
DennisYZFDennisYZF

Here is a re-worked trigger:

trigger LeadSourceRef on Lead( before insert ) {

String DotString = '.';
String myString1;
String myString2 = '2';
String LSFormat;  
Set<String> LeadSet = new Set<String>();
             
{  
    for( Lead record : Trigger.new )  
    {

    IF (  record.leadsource != Null  )
    {
    myString1 = record.leadsource;

   IF (! myString1.contains(DotString))
   LeadSet.add( record.LeadSource);  
   
    IF (  record.leadsource != Null && record.leadsource.contains ('2')  )
    {
    Integer IndexS =  myString1.indexOf(myString2, 0);
    LSFormat = myString1.substring (IndexS );
    LeadSet.add( LSFormat); 
    }
    }
    }
List <Lead_Source_Reference__c> thisLSRlist = [select id,Cookie_Identifier__c from Lead_Source_Reference__c where Cookie_Identifier__c in :LeadSet];

for(lead MyLead: Trigger.new){

if (thisLSRlist.size()>0 && MyLead.leadsource != Null )
{
IF (! MyLead.leadsource.contains(DotString))
for(Integer i = 0; i < thisLSRlist.size(); i++)
        {
            if(thisLSRlist[i].Cookie_Identifier__c == MyLead.leadsource)
                MyLead.Lead_Source_Reference__c = thisLSRlist[i].id;
           
        }
IF ( MyLead.leadsource.contains ('2') && MyLead.leadsource.contains(DotString) )        
for(Integer i = 0; i < thisLSRlist.size(); i++)
        {
         Integer IndexA =  MyLead.leadsource.indexOf('2', 0);
         String LSFormatB = MyLead.leadsource.substring (IndexA );
            if(thisLSRlist[i].Cookie_Identifier__c == LSFormatB)
                MyLead.Lead_Source_Reference__c = thisLSRlist[i].id;
        
        }        
         
           }
           }
           }
           }

All Answers

jkucerajkucera

looks about right, so my advice is to add mroe logging (system.debug) to determine what's happening in the 30% that fail in production.

 

Not sure why you have the initial { after the system.debug, but that shouldn't affect much:

System.debug( '<< LSR - Number of records in this  trigger call: ' + Trigger.new.size() );
              
{  
    for( Lead record : Trigger.new )
DennisYZFDennisYZF

Thank you!

DennisYZFDennisYZF

Here is a re-worked trigger:

trigger LeadSourceRef on Lead( before insert ) {

String DotString = '.';
String myString1;
String myString2 = '2';
String LSFormat;  
Set<String> LeadSet = new Set<String>();
             
{  
    for( Lead record : Trigger.new )  
    {

    IF (  record.leadsource != Null  )
    {
    myString1 = record.leadsource;

   IF (! myString1.contains(DotString))
   LeadSet.add( record.LeadSource);  
   
    IF (  record.leadsource != Null && record.leadsource.contains ('2')  )
    {
    Integer IndexS =  myString1.indexOf(myString2, 0);
    LSFormat = myString1.substring (IndexS );
    LeadSet.add( LSFormat); 
    }
    }
    }
List <Lead_Source_Reference__c> thisLSRlist = [select id,Cookie_Identifier__c from Lead_Source_Reference__c where Cookie_Identifier__c in :LeadSet];

for(lead MyLead: Trigger.new){

if (thisLSRlist.size()>0 && MyLead.leadsource != Null )
{
IF (! MyLead.leadsource.contains(DotString))
for(Integer i = 0; i < thisLSRlist.size(); i++)
        {
            if(thisLSRlist[i].Cookie_Identifier__c == MyLead.leadsource)
                MyLead.Lead_Source_Reference__c = thisLSRlist[i].id;
           
        }
IF ( MyLead.leadsource.contains ('2') && MyLead.leadsource.contains(DotString) )        
for(Integer i = 0; i < thisLSRlist.size(); i++)
        {
         Integer IndexA =  MyLead.leadsource.indexOf('2', 0);
         String LSFormatB = MyLead.leadsource.substring (IndexA );
            if(thisLSRlist[i].Cookie_Identifier__c == LSFormatB)
                MyLead.Lead_Source_Reference__c = thisLSRlist[i].id;
        
        }        
         
           }
           }
           }
           }

This was selected as the best answer