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
prad_p25prad_p25 

Need some help in trigger when creating lead

Hi

 

I need help.

 

 When ever I create a lead I want to search unique org number in lead and account database? here is my code but i am getting error as Apex trigger blockDuplicates_tgr1 caused an unexpected exception, contact your administrator: blockDuplicates_tgr1: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.blockDuplicates_tgr1: line 36, column 22

 

 

 

trigger blockDuplicates_tgr1 on Lead bulk(after insert, after update) 
{
      /* 
       * begin by building a map which stores the (unique) list of leads 
       * being inserted/updated, using email address as the key. 
       */
      Map<String, Lead> leadMap = new Map<String, Lead>();
      for (Lead lead : System.Trigger.new) 
      {                
              if (lead.Organisation_Number_Norway__c != null) 
              {                
               /* for inserts OR  
                 * updates where the email address is changing 
                 * check to see if the email is a duplicate of another in 
                 * this batch, if unique, add this lead to the leadMap
                 */
                  if  ( System.Trigger.isInsert ||
                      (System.Trigger.isUpdate && 
                    lead.Organisation_Number_Norway__c != System.Trigger.oldMap.get(lead.Id).Organisation_Number_Norway__c)) 
                   { 
                            
                     if (leadMap.containsKey(lead.Organisation_Number_Norway__c))
                      {
                            lead.Organisation_Number_Norway__c.addError('Another Lead  or Account has the same Organisation Number.');
                      } 
                      else 
                      {
                          leadMap.put(lead.Organisation_Number_Norway__c, lead);
                      }
                   }
               
               }
               
               String leadOrgNR = lead.Organisation_Number_Norway__c;
               Account acc = new Account ();
               acc = [Select Organisation_Number_Norway__c from Account 
                                   WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];  
                                   
               if (acc.Organisation_Number_Norway__c.equals(lead.Organisation_Number_Norway__c)) 
                 {
                       lead.Organisation_Number_Norway__c.addError('Another Lead or Account has the same Organisation Number.');
                    }
                
                    else 
                    {
                          // leadMap.put(lead.Organisation_Number_Norway__c, lead);
                    }
           
      }
          
 }

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The issue is with the following line of code I believe:

 

 

 Account acc = new Account ();
 acc = [Select Organisation_Number_Norway__c from Account 
                       WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];  

 

 

Looking at your trigger, this looks like it is attempting to ensure that there is no account with the same organisation number norway as the current lead.  However, this will throw an exception if there is not exactly one record returned by the SOQL query.

 

You'll need to query the SOQL into a list and check if that is empty or not,   e.g.

 

 

 String leadOrgNR = lead.Organisation_Number_Norway__c;
 List<Account> accs = [Select Organisation_Number_Norway__c from Account 
                       WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];  
                    
 // if accs is not empty, that means there is an account that already has the number               
 if (!accs.IsEmpty()) 
 {
     lead.Organisation_Number_Norway__c.addError('Another Lead or Account has the same Organisation Number.');
 }
 else 
 {
    // leadMap.put(lead.Organisation_Number_Norway__c, lead);
 }

 

 

 

All Answers

bob_buzzardbob_buzzard

The issue is with the following line of code I believe:

 

 

 Account acc = new Account ();
 acc = [Select Organisation_Number_Norway__c from Account 
                       WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];  

 

 

Looking at your trigger, this looks like it is attempting to ensure that there is no account with the same organisation number norway as the current lead.  However, this will throw an exception if there is not exactly one record returned by the SOQL query.

 

You'll need to query the SOQL into a list and check if that is empty or not,   e.g.

 

 

 String leadOrgNR = lead.Organisation_Number_Norway__c;
 List<Account> accs = [Select Organisation_Number_Norway__c from Account 
                       WHERE Account.Organisation_Number_Norway__c =: leadOrgNR limit 1];  
                    
 // if accs is not empty, that means there is an account that already has the number               
 if (!accs.IsEmpty()) 
 {
     lead.Organisation_Number_Norway__c.addError('Another Lead or Account has the same Organisation Number.');
 }
 else 
 {
    // leadMap.put(lead.Organisation_Number_Norway__c, lead);
 }

 

 

 

This was selected as the best answer
prad_p25prad_p25

Hi Bob, many thanks buddy. It's working fine now for me.

 

many thanks.

 

regards,

Pradip