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
NasirNasir 

caused by: System.QueryException: List has no rows for assignment to SObject

Hi

 

I am getting an error.I know i have to use list for opportunity but the record is not getting saved.Please help me out

 

The related trigger is

 

 

trigger OpportunityCreditCheck on Credit_Check__c (before insert, before update) {

 

 

  for(Credit_Check__c CCheck : Trigger.new){
            
    if(CCheck.RecordtypeId == '012200000009PMEAA2'){
          Opportunity oppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c];
     
      if(Oppr.Account.Business_Type__c == 'Limited Company' || Oppr.Account.Business_Type__c == 'Plc' || Oppr.Account.Business_Type__c == 'Charity'){
     
         if(oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){ 
         
        
         
             if(CCheck.Limited_Company_Account__c == null ){
            CCheck.Limited_Company_Account__c.addError('Please Enter the Account Name');
            }
            if(CCheck.Registered_Address__c == null){
            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');
            }
            
            if(CCheck.Date_of_Incorporation__c == null){
            CCheck.Date_of_Incorporation__c.addError('Please Enter the Date of Incorporation');
            }
            
             if(CCheck.Company_Registered_Number__c == null){
            CCheck.Company_Registered_Number__c.addError('Please Enter the Full Company Registered Number');
            }
             
            if(CCheck.Phone_Number__c == null){
            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');
            }
            if(CCheck.Billing_Address__c == null){
            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');
            }
            }
            
      }
       if(CCheck.Date_of_Incorporation__c < system.Today()-730){
       
           if(CCheck.Full_Company_Name__c == null){
           CCheck.Full_Company_Name__c.addError('Please Enter the Full company name');
           }
             
            if(CCheck.Registered_Address__c == null){
            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');
            }
             if(CCheck.Company_Registered_Number__c == null){
            CCheck.Company_Registered_Number__c.addError('Please Enter the Company Registered Number');
            }
             
            if(CCheck.Phone_Number__c == null){
            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');
            }
            if(CCheck.Billing_Address__c == null){
            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');
            }
            if(CCheck.Director_First_Name__c == null){
            CCheck.Director_First_Name__c.addError('Please Enter the Director First Name');
            }
            if(CCheck.Director_Last_Name__c == null){
            CCheck.Director_Last_Name__c.addError('Please Enter Director Last Name');
            }
             if(CCheck.Directors_DOB__c == null){
            CCheck.Directors_DOB__c.addError('Please Enter Director Date of Birth');
            }
            
            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){
            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');
             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');
            
            }
           
            if(CCheck.Building_PostCode__c == null){
            CCheck.Building_PostCode__c.addError('Please Enter Building Post Code');
            }
            if(CCheck.CC_Consent__c == null){
            CCheck.CC_Consent__c.addError('Please Enter the Full CC Consent');
            }
            if(CCheck.Building_Town__c == null){
            CCheck.Building_Town__c.addError('Please Enter the Town'); 
            }
       }
       
       /*if date of incorporation is greater than 2 years, new supplier is equal to Eon and product name is not equal to bespoke,
        than contact firstname,contact last name,billing building number,billing building name ,billing address,billing town,
        billing post code and date field must be filled.*/
        
          if(CCheck.Date_of_Incorporation__c > system.Today() + 730 && oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){
           
           if(CCheck.Director_First_Name__c == null){
           CCheck.Director_First_Name__c.addError('Please enter contact First name');
           }
           if(CCheck.Director_Last_Name__c == null){
           CCheck.Director_Last_Name__c.addError('Please enter contact Last name');
           }
            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){
            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');
             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');
            
            }
           if(CCheck.Building_Street_Address__c== null){
           CCheck.Building_Street_Address__c.addError('Please enter Billing Street Address');
           }
           if(CCheck.Building_Town__c== null){
           CCheck.Building_Town__c.addError('Please enter Billing Town');
           }
           if(CCheck.Building_PostCode__c== null){
           CCheck.Building_PostCode__c.addError('Please enter Billing Post Code');
           }
           if(CCheck.Directors_DOB__c== null){
           CCheck.Directors_DOB__c.addError('Please enter Date of Birth');
           }
          }
        }
         if(CCheck.RecordtypeId == '012200000009PMJAA2'){
            
            Opportunity oppr = [Select Account.Business_Type__c,  AccountId From Opportunity where Id =:CCheck.Opportunity__c];
           
          if(Oppr.Account.Business_Type__c == 'Sole Trader' || Oppr.Account.Business_Type__c == 'Trusteeships' ||  Oppr.Account.Business_Type__c == 'Partnership' ){
            
            if(CCheck.Business_Name__c == null){
             CCheck.Business_Name__c.addError('Please enter business name');
            }
            if(CCheck.Bussiness_owner_name__c == null){
                CCheck.Bussiness_owner_name__c.addError('Please enter business owner name');
            }
           if(CCheck.Current_Address_Home_Address__c == null){
            CCheck.Current_Address_Home_Address__c.addError('Please enter current address');
           }
           if(CCheck.Date_of_Occupancy_of_Home__c == null){
            CCheck.Date_of_Occupancy_of_Home__c.addError('Please enter date of occupancy at Home');
           }
           if(CCheck.Previous_Address__c == null){
            CCheck.Previous_Address__c.addError('Please enter previous address if it less than 2 years Or mention SAME AS ABOVE');
           }
           if(CCheck.Bussiness_Owner_DOB__c == null){
           CCheck.Bussiness_Owner_DOB__c.addError('Please enter Bussiness owner date of birth');  
           }
           
          }
        }
    }   
    }

Thanks

 

Nasir

SargeSarge

Hi Nasir,

 

      It is always  a good practice to check whether you have a Opportunity record selected in the lookup or not. The error might have resulted becasue of the save of the record and not having Opportunity selected in the lookup.

 

So In the if statement you can use something like below

 

 if(CCheck.RecordtypeId == '012200000009PMEAA2' && CCheck.Opportunity__c != null){
          Opportunity oppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c];

 

 

This should be applied for the another similar if statement in your code at the bottom.

Hope this helps.

NasirNasir

hi sarge,

 

cau u please  use "list" for the above SOQL statements,because the error is list has no assignment for Sobject and also if the query fails,for that also i have to provide a condition so that it should't through null pointer exception.

Please help

 

Under i have use list

 

List<Opportunity> oppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c];
_Prasu__Prasu_

you can try out following set of statements

Opportunity[] arrOppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c];
Opportunity oppr = null;
if(arrOppr != null && arrOppr.size() > 0) 
oppr = arrOppr[0];
SargeSarge

Hi Nasir,

 

 

    Since you are querying the opportunity object and filtering it with a single id, it is clear that you will fetch only and only one record. Hence it is not necessary to have a list. The only thing you have to  make sure is that opportunity id is not null. This will return with no results and hence query exception.

 

     Also I havent noticed this previously; It might be hitting the governor limits with the pattern of code written for trigger. Basically, the SOQL statement (which we are discussing) is inside for loop and hence not a best practice for optimum coding. Instead collect all Opportunity Ids outside this for loop and then query for Opportunity records and use the list of opp records inside for loop.

 

Thanks

 

 

NasirNasir

 

Hi Prasanna,

 

I appriciate your concern with this code.As ur code has been saved.As i am not much aware of coding.Can u please tell me the code what u had used is also free from throwing null pointer exception.

I would appriciate u if u can explain ur code like how the list is not empty and this will not through null pointer exception and also the error list had no assignment for Sobject.

 

Please help.Also check the code what i had used below.

 

 

t

 

trigger OpportunityCreditCheck on Credit_Check__c (before insert, before update) { 

 

 

for(Credit_Check__c CCheck : Trigger.new){

 

    if(CCheck.RecordtypeId == '012200000009PMEAA2' && CCheck.Opportunity__c != null){

         Opportunity[] arrOppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c limit 1];

 

       Opportunity oppr = null;

      if(arrOppr != null && arrOppr.size() > 0) 

       oppr = arrOppr[0];

      if(Oppr.Account.Business_Type__c == 'Limited Company' || Oppr.Account.Business_Type__c == 'Plc' || Oppr.Account.Business_Type__c == 'Charity'){

 

         if(oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){ 

 

 

 

             if(CCheck.Limited_Company_Account__c == null ){

            CCheck.Limited_Company_Account__c.addError('Please Enter the Account Name');

            }

            if(CCheck.Registered_Address__c == null){

            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');

            }

 

            if(CCheck.Date_of_Incorporation__c == null){

            CCheck.Date_of_Incorporation__c.addError('Please Enter the Date of Incorporation');

            }

 

             if(CCheck.Company_Registered_Number__c == null){

            CCheck.Company_Registered_Number__c.addError('Please Enter the Full Company Registered Number');

            }

 

            if(CCheck.Phone_Number__c == null){

            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');

            }

            if(CCheck.Billing_Address__c == null){

            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');

            }

            }

 

      }

       if(CCheck.Date_of_Incorporation__c < system.Today()-730){

 

           if(CCheck.Full_Company_Name__c == null){

           CCheck.Full_Company_Name__c.addError('Please Enter the Full company name');

           }

 

            if(CCheck.Registered_Address__c == null){

            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');

            }

             if(CCheck.Company_Registered_Number__c == null){

            CCheck.Company_Registered_Number__c.addError('Please Enter the Company Registered Number');

            }

 

            if(CCheck.Phone_Number__c == null){

            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');

            }

            if(CCheck.Billing_Address__c == null){

            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');

            }

            if(CCheck.Director_First_Name__c == null){

            CCheck.Director_First_Name__c.addError('Please Enter the Director First Name');

            }

            if(CCheck.Director_Last_Name__c == null){

            CCheck.Director_Last_Name__c.addError('Please Enter Director Last Name');

            }

             if(CCheck.Directors_DOB__c == null){

            CCheck.Directors_DOB__c.addError('Please Enter Director Date of Birth');

            }

 

            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){

            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');

             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');

 

            }

 

            if(CCheck.Building_PostCode__c == null){

            CCheck.Building_PostCode__c.addError('Please Enter Building Post Code');

            }

            if(CCheck.CC_Consent__c == null){

            CCheck.CC_Consent__c.addError('Please Enter the Full CC Consent');

            }

            if(CCheck.Building_Town__c == null){

            CCheck.Building_Town__c.addError('Please Enter the Town'); 

            }

       }

 

       /*if date of incorporation is greater than 2 years, new supplier is equal to Eon and product name is not equal to bespoke,

        than contact firstname,contact last name,billing building number,billing building name ,billing address,billing town,

        billing post code and date field must be filled.*/

 

          if(CCheck.Date_of_Incorporation__c > system.Today() + 730 && oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){

 

           if(CCheck.Director_First_Name__c == null){

           CCheck.Director_First_Name__c.addError('Please enter contact First name');

           }

           if(CCheck.Director_Last_Name__c == null){

           CCheck.Director_Last_Name__c.addError('Please enter contact Last name');

           }

            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){

            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');

             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');

 

            }

           if(CCheck.Building_Street_Address__c== null){

           CCheck.Building_Street_Address__c.addError('Please enter Billing Street Address');

           }

           if(CCheck.Building_Town__c== null){

           CCheck.Building_Town__c.addError('Please enter Billing Town');

           }

           if(CCheck.Building_PostCode__c== null){

           CCheck.Building_PostCode__c.addError('Please enter Billing Post Code');

           }

           if(CCheck.Directors_DOB__c== null){

           CCheck.Directors_DOB__c.addError('Please enter Date of Birth');

           }

          }

        }

 

_Prasu__Prasu_

Coding is done in wrong way, what i have provided is a fix for exception. 

 

trigger OpportunityCreditCheck on Credit_Check__c (before insert, before update) { 

 

 

for(Credit_Check__c CCheck : Trigger.new){

 

    Opportunity[] arrOppr  = null;

    if(CCheck.RecordtypeId == '012200000009PMEAA2' && CCheck.Opportunity__c != null){

        arrOppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c limit 1];

 

      Opportunity oppr = null;

      if(arrOppr != null && arrOppr.size() > 0) 

       Oppr = arrOppr[0];

      if(Oppr != null && (Oppr.Account.Business_Type__c == 'Limited Company' || Oppr.Account.Business_Type__c == 'Plc' || Oppr.Account.Business_Type__c == 'Charity' )){

 

.

.

.

.

Exception message does provide the line of code. 

Please make habit to check if the variable is not null before using it. I have highlighted how you to check for the value. You may require making such changes in some more lines of you code.

 

NasirNasir

hi Prasana and

 

I have used this code.Will it be giving Null pointer exception.Thanks for the above code u had provided.But my manager is asking me to use this code and make free from null pointer exception.Please don't use array because there is only one object.just below code should't give null pointer exception.Please help i would appriciate ur concern.

Thanks

 

 

trigger OpportunityCreditCheck on Credit_Check__c (before insert, before update) {

for(Credit_Check__c CCheck : Trigger.new){
            
    if(CCheck.RecordtypeId == '012200000009PMEAA2'){
          Opportunity oppr = [Select Account.Business_Type__c,Account.Name,Account.Registered_Charity_Number__c,Product_Name__c, New_Supplier__c, AccountId From Opportunity where Id =:CCheck.Opportunity__c limit 1];
     
      if(Oppr.Account.Business_Type__c == 'Limited Company' || Oppr.Account.Business_Type__c == 'Plc' || Oppr.Account.Business_Type__c == 'Charity'){
     
         if(oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){ 
         
        
         
             if(CCheck.Limited_Company_Account__c == null ){
            CCheck.Limited_Company_Account__c.addError('Please Enter the Account Name');
            }
            if(CCheck.Registered_Address__c == null){
            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');
            }
            
            if(CCheck.Date_of_Incorporation__c == null){
            CCheck.Date_of_Incorporation__c.addError('Please Enter the Date of Incorporation');
            }
            
             if(CCheck.Company_Registered_Number__c == null){
            CCheck.Company_Registered_Number__c.addError('Please Enter the Full Company Registered Number');
            }
             
            if(CCheck.Phone_Number__c == null){
            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');
            }
            if(CCheck.Billing_Address__c == null){
            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');
            }
            }
            
      }
       if(CCheck.Date_of_Incorporation__c < system.Today()-730){
       
           if(CCheck.Full_Company_Name__c == null){
           CCheck.Full_Company_Name__c.addError('Please Enter the Full company name');
           }
             
            if(CCheck.Registered_Address__c == null){
            CCheck.Registered_Address__c.addError('Please Enter the Full Registered Address');
            }
             if(CCheck.Company_Registered_Number__c == null){
            CCheck.Company_Registered_Number__c.addError('Please Enter the Company Registered Number');
            }
             
            if(CCheck.Phone_Number__c == null){
            CCheck.Phone_Number__c.addError('Please Enter the Full Phone Number');
            }
            if(CCheck.Billing_Address__c == null){
            CCheck.Billing_Address__c.addError('Please Enter the Full Billing Address');
            }
            if(CCheck.Director_First_Name__c == null){
            CCheck.Director_First_Name__c.addError('Please Enter the Director First Name');
            }
            if(CCheck.Director_Last_Name__c == null){
            CCheck.Director_Last_Name__c.addError('Please Enter Director Last Name');
            }
             if(CCheck.Directors_DOB__c == null){
            CCheck.Directors_DOB__c.addError('Please Enter Director Date of Birth');
            }
            
            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){
            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');
             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');
            
            }
           
            if(CCheck.Building_PostCode__c == null){
            CCheck.Building_PostCode__c.addError('Please Enter Building Post Code');
            }
            if(CCheck.CC_Consent__c == null){
            CCheck.CC_Consent__c.addError('Please Enter the Full CC Consent');
            }
            if(CCheck.Building_Town__c == null){
            CCheck.Building_Town__c.addError('Please Enter the Town'); 
            }
       }
       
       /*if date of incorporation is greater than 2 years, new supplier is equal to Eon and product name is not equal to bespoke,
        than contact firstname,contact last name,billing building number,billing building name ,billing address,billing town,
        billing post code and date field must be filled.*/
        
         if(CCheck.Date_of_Incorporation__c > system.Today() + 730 && oppr.New_Supplier__c == 'Eon' && oppr.Product_Name__c != 'Bespoke Price'){
           
           if(CCheck.Director_First_Name__c == null){
           CCheck.Director_First_Name__c.addError('Please enter contact First name');
           }
           if(CCheck.Director_Last_Name__c == null){
           CCheck.Director_Last_Name__c.addError('Please enter contact Last name');
           }
            if(CCheck.Building_Name__c == null && CCheck.Building_Number__c == null){
            CCheck.Building_Name__c.addError('Please Enter Building number or Building name');
             CCheck.Building_Number__c.addError('Please Enter Building number or Building name');
            
            }
           if(CCheck.Building_Street_Address__c== null){
           CCheck.Building_Street_Address__c.addError('Please enter Billing Street Address');
           }
           if(CCheck.Building_Town__c== null){
           CCheck.Building_Town__c.addError('Please enter Billing Town');
           }
           if(CCheck.Building_PostCode__c== null){
           CCheck.Building_PostCode__c.addError('Please enter Billing Post Code');
           }
           if(CCheck.Directors_DOB__c== null){
           CCheck.Directors_DOB__c.addError('Please enter Date of Birth');
           }
          }
        }
         if(CCheck.RecordtypeId == '012200000009PMJAA2'){
            
            Opportunity oppr = [Select Account.Business_Type__c,  AccountId From Opportunity where Id =:CCheck.Opportunity__c limit 1];
           
          if(Oppr.Account.Business_Type__c == 'Sole Trader' || Oppr.Account.Business_Type__c == 'Trusteeships' ||  Oppr.Account.Business_Type__c == 'Partnership' ){
            
            if(CCheck.Business_Name__c == null){
             CCheck.Business_Name__c.addError('Please enter business name');
            }
            if(CCheck.Bussiness_owner_name__c == null){
                CCheck.Bussiness_owner_name__c.addError('Please enter business owner name');
            }
           if(CCheck.Current_Address_Home_Address__c == null){
            CCheck.Current_Address_Home_Address__c.addError('Please enter current address');
           }
           if(CCheck.Date_of_Occupancy_of_Home__c == null){
            CCheck.Date_of_Occupancy_of_Home__c.addError('Please enter date of occupancy at Home');
           }
           if(CCheck.Previous_Address__c == null){
            CCheck.Previous_Address__c.addError('Please enter previous address if it less than 2 years Or mention SAME AS ABOVE');
           }
           if(CCheck.Bussiness_Owner_DOB__c == null){
           CCheck.Bussiness_Owner_DOB__c.addError('Please enter Bussiness owner date of birth');  
           }
           
          }
        }
    }   
    }

 

 

NasirNasir

Hi Prasanna 

 

I have also used limit 1 to both the SOQL query,which was not there before in the first code.As i am using Salesforce language so array dosen't comes here.If feasible please use "list".But try that the code dosen through null pointer exception for the above code what i had send.

 

Thanks a lot.

 

Nasir