+ Start a Discussion
SSK9SSK9 

InquiryTrigger: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.InquiryTriggerHandler.InquiryHandler: line 13, column 1

Error:Apex trigger InquiryTrigger caused an unexpected exception, contact your administrator: InquiryTrigger: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.InquiryTriggerHandler.InquiryHandler: line 13, column 1

How do I fix this issue?

 
public class InquiryTriggerHandler {
    
    public static void InquiryHandler(string caseid){
       
        
        
        
        
        Case cs=[SELECT id,OwnerId,ContactID,Confirmation_Email__c,ContactEmail From Case WHERE id=:caseid];
        
        system.debug('CaseID: '+caseid);
        
        user u=[select id,email from user where id=:cs.ownerid];
        
        system.debug('User Email: '+u.email);
        
        system.debug('Confirmation Mail: '+cs.Confirmation_Email__c);
        
        system.debug('Contact Email: '+cs.ContactEmail);
        
        List<Messaging.SingleEmailMessage> lsem=new List<Messaging.SingleEmailMessage>();
        
        Messaging.SingleEmailMessage sem=new Messaging.SingleEmailMessage();
        
        List<String> address=new List<String>();
        
        //address.add(u.email);
        
        address.add(cs.Confirmation_Email__c);
        
        address.add(cs.contactemail);
        
        address.add('siddharth.koduri@gmail.com');
        
        sem.setToAddresses(address);
        
        sem.setCcAddresses(new string[]{u.email});

        //sem.setSubject('testing');
        
        //sem.setplaintextbody('jjjj');
        EmailTemplate et=[SELECT id,Name FROM EmailTemplate WHERE name='Email Confirmation Template II'];
        
        sem.setTemplateId(et.id);
        
        sem.setTargetObjectId(cs.contactId);
        
        sem.setWhatId(cs.id);
        
       
       OrgWideEmailAddress owd=[SELECT id,Address FROM OrgWideEmailAddress WHERE Address=:system.label.Taconic_Mail_id];
       
       sem.setOrgWideEmailAddressId(owd.id);
        
        
       List<Attachment> att=[SELECT id,Name,Body,ContentType FROM Attachment WHERE ParentID=:caseid];
        
        List<Messaging.EmailFileAttachment> lefa=new List<Messaging.EmailFileAttachment>();
        
        for(Attachment a:att){
            
            Messaging.EmailFileAttachment efa=new Messaging.EmailFileAttachment();
            string contentvalue=(a.contentType).substringAfter('/');
            efa.setBody(a.body);
                efa.setFileName(a.Name);
            lefa.add(efa);
            
        } 
   
        sem.setFileAttachments(lefa); 
          
        
        
        lsem.add(sem);
            
        messaging.sendEmail(lsem);
    
        
        
    }
}

 
Raj VakatiRaj Vakati
Hi, Siddharth 7,

What I can see problems in your code is ... 
  • caseid can be user or queue. So you have to check if the case owner is user or queue then only perform the query 
Sample code is here 
if(string.valueOf(cs.OwnerId).startsWith('005'))
       {
                user u=[select id,email from user where id=:cs.ownerid];

       }

if(string.valueOf(cs.OwnerId).startsWith('00G'))
       {
                  //owner is Queue
       }

 
Ishwar ShindeIshwar Shinde
Hi, 

You should use collection to catch the SOQL results. It will not throw an exception. Then you can check the collection size before procedding with rest of your logic.

instead of - 
 user u=[select id,email from user where id=:cs.ownerid];

it should be - 
List<User> uList  = [select id,email from user where id=:cs.ownerid];

if(uList.size() > 0 ){
  do something..
}

Salesforce never give you null list as result of query, so size check is sufficient.

Thanks,
Ishwar 

Please mark this as best answer if its solve your problem.
Raj VakatiRaj Vakati
It is after update trigger. So 
Point 1  : 
 user u=[select id,email from user where id=:cs.ownerid] should always return the record if an owner is a user  . If Owner is Queue then it will be null 

Point 2. Even if you check the below code 
 List<User> uList  = [select id,email from user where id=:cs.ownerid]; 
Your list should be zero rows if the owner is Queue and only record if the record is owned by the user. One records can't contain more than one User as owner. 

Let me know if that is the root cause of your error. 


 
shailesh_rawteshailesh_rawte
Hi Siddharth 7


You can check the who is owner of that case either queue or user.
Refered following program.

if(string.valueOf(c.OwnerId).startsWith('005'))
       {
                  //owner is User
       }

if(string.valueOf(c.OwnerId).startsWith('00G'))
       {
                  //owner is Queue
       }

Regards
Shailesh Rawte