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
kiran punurukiran punuru 

List has no rows for assignment to a sobject

Hi Iam trying attach a document which is uploaded in chatter to the email and send that email but iam getting the system.query exception:list has no rows for assignment to sobject.
Trigger:
trigger SendEmailOnFileUploadTrigger2 on ContentDocumentLink (after insert,after update) {
    Set<Id> fieldIds = new Set<Id>();
    Set<Id> docIdSet = new Set<Id>();
    for(ContentDocumentLink content :trigger.new){
        fieldIds.add(content.LinkedEntityId);
        docIdSet.add(content.ContentDocumentId);
    }
    system.debug('@@@ Linked Entity Id:'+fieldIds);
    system.debug('@@@ Content Document Id:'+docIdSet);
    if(trigger.isAfter)
    EmailHandler.sendUploadNotificationEmail(docIdSet);
}

Class:
public without sharing class EmailHandler {
    public static void sendUploadNotificationEmail(set<Id> docIds){
         system.debug('@@@@Document Ids:'+docIds);
         Document doc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds]; 
         Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
            attach.setContentType(doc.contentType);
         attach.setFileName(doc.developerName+'.'+doc.type);
         attach.setInline(false);
         attach.Body = doc.Body; */
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {'test@xyz.com'};
         String[] ccAddresses = new String[] {'kirankumar.punuru@xyz.com'};
         mail.setUseSignature(false);
         mail.setToAddresses(toAddresses);
         mail.setCcAddresses(ccAddresses);
         mail.setReplyTo('kirankumar.punuru@eclerx.com');
         mail.setSenderDisplayName('Kiran Kumar reddy Punuru');
         mail.setSubject('The Below File Has been uploaded : ');
         //mail.setHtmlBody('Here is the email you requested: '+doc.name);
         //mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
         mail.setBccSender(false);
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
      }
  }

Please correct the above if iam doing in correct.

Thanks
kiran
anil jadhav 8anil jadhav 8
Hi Kiran,
On which line you are getting error or please provide the error message.

Regards,
Anil
Amit Chaudhary 8Amit Chaudhary 8
Please update your class like below
 
public without sharing class EmailHandler 
{
    public static void sendUploadNotificationEmail(set<Id> docIds)
	{
         system.debug('@@@@Document Ids:'+docIds);
   
		 List<Document> Lisdoc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds]; 
         
		 if(Lisdoc.size()>0 )
		 {
			Document doc =Lisdoc[0];
		 
			 Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
				attach.setContentType(doc.contentType);
			 attach.setFileName(doc.developerName+'.'+doc.type);
			 attach.setInline(false);
			 attach.Body = doc.Body; */
			 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			 String[] toAddresses = new String[] {'test@xyz.com'};
			 String[] ccAddresses = new String[] {'kirankumar.punuru@xyz.com'};
			 mail.setUseSignature(false);
			 mail.setToAddresses(toAddresses);
			 mail.setCcAddresses(ccAddresses);
			 mail.setReplyTo('kirankumar.punuru@eclerx.com');
			 mail.setSenderDisplayName('Kiran Kumar reddy Punuru');
			 mail.setSubject('The Below File Has been uploaded : ');
			 //mail.setHtmlBody('Here is the email you requested: '+doc.name);
			 //mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
			 mail.setBccSender(false);
			 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
		 }	 
      }
  }

Let us know if this will help you

Thanks
Amit Chaudhary
kiran punurukiran punuru
Hi Amit,

When  i change the class then it is not giving error but i need to send email if listdoc.size() > 0 .The problem is it is showing the size of the list as zero.i did not understand the reason .

thanks,
Kiran
kiran punurukiran punuru
Hi Anil ,
I am getting the error like this  for the below line:
Document doc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds];
User-added image

Thanks
kiran
Amit Chaudhary 8Amit Chaudhary 8
Hi Kiran,

Issue is coming because of below line:-
Document doc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds];

In your code No result is coming in above query so i added List to check if you will get zero record.

Try to update your code like below, Use document only when you will get document record from query
public without sharing class EmailHandler 
{
    public static void sendUploadNotificationEmail(set<Id> docIds)
	{
			system.debug('@@@@Document Ids:'+docIds);
			List<Document> Lisdoc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds]; 
         
			Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
			if(Lisdoc.size()>0 )
			{
				Document doc =Lisdoc[0];
				attach.setContentType(doc.contentType);
				attach.setFileName(doc.developerName+'.'+doc.type);
				attach.setInline(false);
				attach.Body = doc.Body; 
			} 
			 
			 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
			 String[] toAddresses = new String[] {'test@xyz.com'};
			 String[] ccAddresses = new String[] {'kirankumar.punuru@xyz.com'};
			 mail.setUseSignature(false);
			 mail.setToAddresses(toAddresses);
			 mail.setCcAddresses(ccAddresses);
			 mail.setReplyTo('kirankumar.punuru@eclerx.com');
			 mail.setSenderDisplayName('Kiran Kumar reddy Punuru');
			 mail.setSubject('The Below File Has been uploaded : ');
			 mail.setBccSender(false);
			 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
		 }	 
      }
  }


Let us know if that will help you

Thanks
Amit Chaudhary
kiran punurukiran punuru
Can you please tell me why it is not giving results if the debug statement has document id:
system.debug('@@@@Document Ids:'+docIds);(here it is giving document id)
This size of the list is coming as zero 
List<Document> Lisdoc = [select id, name, body, contenttype, developername, type from Document where Id IN :docIds];
This size of the list is coming as zero 

Thanks 
kiran
Amit Chaudhary 8Amit Chaudhary 8
Please check you are passing valid document id in your method