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
Hanna BergsmaHanna Bergsma 

Apex Trigger Failing

I'll start this out by disclosing I am an admin not a developer for my org. We do not have a developer on staff and last week I started getting this email for a Apex Class failing.

So, I opened the Class.OrderTriggerHandler in my sandbox and updated the Class and the Trigger to API 50.0 (it was on V 38). I ran the test class and got this error:

Error: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.OrderTriggerHandlerTest.unitTest1: line 12, column 1

I understand that the SOQL is returning 0 rows, when it is expecting 1, but I am not sure where to add the code to solve for this 

Here is the testclass, I bolded line 12

@isTest(SeeAllData=True)

public class OrderTriggerHandlerTest{

  public static testMethod void unitTest1(){
     List<Order>  od1=[Select AccountId from Order Limit 1];
     Set<Id> objAcc = new Set<Id>();
           for(Order objOr : od1){
               objAcc.add(objOr.AccountId);
           }
     List<Account>accountlist =[select id,name,ParentId,Parent.ParentId,(select id,LastName,Account.Parent.ParentId from Contacts where id!=null and Account.Parent.ParentId!=null  Limit 1) from Account where ParentId!=null and Parent.ParentId!=null Limit 1];      
     Contact c1=[Select id,name,Email,AccountId,Account.Parent.ParentId from Contact  where AccountId IN :accountlist Limit 1];
      String accId = String.valueOf(c1.Account.Parent.ParentId).substring(0, 15);
      String stte='New Order Notification'+ accId;
     EmailTemplate templateId = [Select id,Subject, HtmlValue, Body from EmailTemplate where name =:stte];
     List<Order> od = [Select id,AccountId from Order where AccountId =: c1.AccountId];
     OrderTriggerHandler.insertOrder(od);
    } 
    
    public static testMethod void unitTest2(){
      
    Account objAccount2 = new Account();
    objAccount2.Name = 'test2';
    insert objAccount2;
    
    Order objOrder = new Order();
    objOrder.AccountId = objAccount2.Id;
    objOrder.EffectiveDate = Date.today().addDays(+6);
    objOrder.Status = 'Draft';
    insert objOrder;
    } 
 
}
ShirishaShirisha (Salesforce Developers) 
Hi Hanna,

Greetings!

As you already know that the query is returning 0 instead of 1 due to which you are getting an error.

So,I would suggest you to check for the empty list before accessing the list as below:

If the below SOQL query returns 0 then you need to add the below logic:

Contact c1=[Select id,name,Email,AccountId,Account.Parent.ParentId from Contact  where AccountId IN :accountlist Limit 1];

if (c1.size() >0)
{
remaining code here
}

Reference:https://help.salesforce.com/articleView?id=000328824&type=1&mode=1

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Hanna BergsmaHanna Bergsma
thanks, this fixed part of it. Now i am getting the same error on my trigger and a hander. I have bolded the lines the email identifies
Again, im not really sure what this code does, I just know my email is blowing up with these errors.

OrderTrigger: execution of AfterInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.OrderTriggerHandler.insertOrder: line 21, column 1
Trigger.OrderTrigger: line 4, column 1


Trigger

trigger OrderTrigger on Order (after insert) {

    if(Trigger.isInsert){
       OrderTriggerHandler.insertOrder(Trigger.new);  
     }

}

Class.OrderTriggerHandler.insertOrder:

public with sharing class OrderTriggerHandler{

    public Static void insertOrder(List<Order> objOrder)
    {
        
         Set<Id> objAcc = new Set<Id>();
         for(Order objOr : objOrder){
             objAcc.add(objOr.AccountId);
         }
         
         
         List<Contact> objCon = [select id,name,Email,AccountId,Account.Parent.ParentId, Account.Parent.Parent.Name from Contact where AccountId IN : objAcc];
         String partnerName;
         Id wideEmailAddressId;
         if(objCon.size() > 0){
             List<String> objMail = new List<String>();
             Set<String> objSetAcc = new Set<String>();
             for(Contact objContact : objCon){
                 objMail.add(objContact.Email);
                 partnerName = objContact.Account.Parent.Parent.Name;
                 String accId = String.valueOf(objContact.Account.Parent.ParentId).substring(0, 15);
                 objSetAcc.add('New Order Notification'+accId);
             }
             
        for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress where DisplayName='EvoLaw, LLC']) {
             if(owa.Address.contains('CSR')) wideEmailAddressId = owa.id; 
        } 
        System.debug('&&&&&&&objSetAcc&&&&&& '+objSetAcc);
        
        
       List<EmailTemplate> templateId = [Select id,Subject, HtmlValue, Body from EmailTemplate where name IN : objSetAcc];
       System.debug('&&&&&&&templateId&&&&&& '+templateId);
       if(templateId.size() > 0){ 
       
            System.debug('&&&&&&&templateId &&&&& '+templateId[0]); 
           String [] emailsAsArray = new String [objMail.size()];
            Integer i = 0;
            for (String singleCCEmail: objMail) {
                emailsAsArray[i++] = singleCCEmail;
            }  
             
           
           String subject =templateId[0].Subject ;
           String htmlBody = templateId[0].HtmlValue ;
           String plainBody = templateId[0].Body;
         
          
          // Messaging.sendEmail(new Messaging.Singleemailmessage[] {mail});
     @TestVisible Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
            message.toAddresses = emailsAsArray;
            message.optOutPolicy = 'FILTER';
            message.subject = 'Opt Out Test Message';
            //message.plainTextBody = 'This is the message body.';
             if(templateId[0].id != null)
             message.setTemplateId(templateId[0].id);
             if(wideEmailAddressId!=Null)
             message.setOrgWideEmailAddressID(wideEmailAddressId);
             message.setSubject(subject);
             message.setHtmlBody(htmlBody);
             message.setPlainTextBody(plainBody);
            // message.setTreatBodiesAsTemplate(true);
            Messaging.SingleEmailMessage[] messages =  new List<Messaging.SingleEmailMessage> {message};
            System.debug('___________msg____' +messages );
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
            if (results[0].success) {
                System.debug('The email was sent successfully.');
            } else {
                System.debug('The email failed to send: '+ results[0].errors[0].message);
            } 
             
           }
         }
    }

}