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
chikkuchikku 

List has no rows for assignment to SObject error in apex with process builder.

I Am stuck with an error I have created an apex with process bulder.
If I run the process it throws an error.

Error Occurred: common.apex.runtime.impl.ExecutionException: List has no rows for assignment to SObject
 
public class sendAnEmail
{
    

    @InvocableMethod(label='Test' description='sends an email')
       
    public static void sendEmailWithAttachment(List<id> listofQuoteHeader)
    {   
        Map<Id, Application__c> quotesMap =  new Map<Id, Application__c>([SELECT id,Contact__r.Email FROM Application__c WHERE Id IN :listofQuoteHeader]);
           for(Id QuoteHeaderid :listofQuoteHeader)
           {
               PageReference pref= page.PDFGEN;
               pref.getParameters().put('id',(Id)QuoteHeaderid);
               pref.setRedirect(true);
               
               Attachment attachment = new Attachment();      
               Blob b=pref.getContentAsPDF();
               attachment.Body = b;
               attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Quote' + '.pdf';
               attachment.IsPrivate = false;
               attachment.ParentId = QuoteHeaderid;
               attachment.Name='Sign.png';
               insert attachment;
               
               Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
               Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
               attach.setFileName('AttachmentEmailFile.pdf');
               attach.setBody(b);
               semail.setSubject('Quote Issued');
            //    String[] emailIds= new String[]{'abc@gmail.com'}; 
            String[] emailIds= new String[]{quotesMap.get(QuoteHeaderid)?.Contact__r.Email};
               semail.setToAddresses(emailIds);
               
               semail.setPlainTextBody('Please find the attached quote details');
               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
               
           } }
      
      private final Application__c account;
        public sendAnEmail() {
        account = [SELECT Id, Name  FROM Application__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
        public Application__c getAccount() {
        return account;
    }

}

There is any resolved answer
 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Chikku,
 
private final Application__c account;
        public sendAnEmail() {
        account = [SELECT Id, Name  FROM Application__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
        public Application__c getAccount() {
        return account;
    }

Please try changing the above snippet to the below way and try it.
 
private final Application__c account;
        public sendAnEmail() {
        account tempacc= [SELECT Id, Name  FROM Application__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }
        public Application__c getAccount() {
        return tempacc;
    }

Also, make sure the soql you are using is proper and is returning valid record.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
chikkuchikku
Hi ANUTEJ,

 I have tried your code it also throws an error like this, can you help me out
User-added image
User-added image
Maharajan CMaharajan C
Hi Chikku,

Try anyone of the below approach:

1. Please use the separate class for InvocableMethod because in your class the constructor is running where you have Application__c record query based on current page Id it's throwing this error.
 
public class sendAnEmailfromPB
{
    
    @InvocableMethod(label='Test' description='sends an email')
       
    public static void sendEmailWithAttachment(List<id> listofQuoteHeader)
    {   
        Map<Id, Application__c> quotesMap =  new Map<Id, Application__c>([SELECT id,Contact__r.Email FROM Application__c WHERE Id IN :listofQuoteHeader]);
           for(Id QuoteHeaderid :listofQuoteHeader)
           {
               PageReference pref= page.PDFGEN;
               pref.getParameters().put('id',(Id)QuoteHeaderid);
               pref.setRedirect(true);
               
               Attachment attachment = new Attachment();      
               Blob b=pref.getContentAsPDF();
               attachment.Body = b;
               attachment.Name = Datetime.now().format('yyyy-MM-dd HH:mm') + ' ' + 'Quote' + '.pdf';
               attachment.IsPrivate = false;
               attachment.ParentId = QuoteHeaderid;
               attachment.Name='Sign.png';
               insert attachment;
               
               Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
               Messaging.EmailFileAttachment attach= new Messaging.EmailFileAttachment();
               attach.setFileName('AttachmentEmailFile.pdf');
               attach.setBody(b);
               semail.setSubject('Quote Issued');
            //    String[] emailIds= new String[]{'abc@gmail.com'}; 
            String[] emailIds= new String[]{quotesMap.get(QuoteHeaderid)?.Contact__r.Email};
               semail.setToAddresses(emailIds);
               
               semail.setPlainTextBody('Please find the attached quote details');
               semail.setFileAttachments(new Messaging.EmailFileAttachment[]{attach});
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
               
           } 
	}
}


2. Do the null check in below constructor:
 
public sendAnEmail() {
	if(ApexPages.currentPage().getParameters().get('id') != null){
		account = [SELECT Id, Name  FROM Application__c 
			   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
	}
}

Thanks,
Maharajan.C
​​​​​​​
chikkuchikku
How to link that method in invocable method becoz the process going on invocable method