• KRISHNAMURTHY KOLUMAM ANANTHARAMAK
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 29
    Replies
Hi All,

This is my trigger which will send an email with attachement whenever a pdf is uplaoded in Notes and Attachement. I wrote a test class but it doesn't cover the code? Would be great if anyone could help me on this. Thanks.
 
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
        
		    //Retrieve all invoices based on the requried Invoice Ids. Add your criteria to fetch 
		    // relevant invoices in the Where clause based on the Invoice and ContentVersion 
		    // object relationship. Replace <yourInvoiceIdSet> below with the actual Id Set
            List<Invoice_Payment__c> invoiceList = [Select Id, Name
										    From Invoice_Payment__c 
										   where Id IN (SELECT FirstPublishLocationId FROM ContentVersion WHERE Id IN :trigger.new)];
		
            for(ContentVersion cv : trigger.new){

                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                attachment.setBody(Blob.valueof(cv.ContentBodyId));
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                
					message.setToAddresses(new String[] { 'andre.nobre@cleantechsolar.com' ,'rupesh.baker@cleantechsolar.com'});
                    message.setccAddresses(new String[] { 'krishnamurthy.ka@cleantechsolar.com' ,'divina.minoza@cleantechsolar.com'});
                
				//Subject with the retrieved Invoice Number
				message.subject = 
					String.format('Your Invoice having Invoice # {0} is due for payment', 
						new String[]{invoiceList[0].Name}); 
                
				//Email body containing Name with the retrieved invoice
				message.plainTextBody = 
					String.format('Your invoice for {0} with the file name {1} is attached alongwith.', 
						new String[]{invoiceList[0].Name, cv.Title});
						
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

Kind Regards
Krishnamurthy
I am trying to send an email with attachment whenever a pdf is uploaded in Notes and attachment object. How do I write a test class ? Kindly help.
 
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
            
            
            for(ContentVersion cv : trigger.new){ 
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                attachment.setBody(Blob.valueof(cv.ContentBodyId));
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' }; 
                message.ccAddresses = new String[] {'krishnamurthy.ka@cleantechsolar.com'};
                message.subject = 'Test Mail'; 
                message.plainTextBody = 'Attached file name: ' + cv.Title; 
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

 
Hi

I am trying to send an email with pdf whenever a pdf is attached in "Notes and Attachment" section. Whenever I try to upload it is giving me an error 

10:04:27:005 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]

Please find the below code for Apex trigger.
trigger EmailTrigger on ContentVersion (after insert) 
{
List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>();
for(ContentVersion cv : trigger.new)
{
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setBody(cv.ContentBodyId);
attachment.setFileName(cv.Title + '.' + cv.FileType);

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' };
message.subject = 'Test Mail';
message.plainTextBody = 'Attached file name: ' + cv.Title;
message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});

allMessages.add(message); 
}

Messaging.sendEmail(allMessages);
}
Kindly help me to rectify the same.
 
Hi,

I would appreciate if you could suggest me a sample code to automate sending pdf through mail.

So the "Notes and Attachment" related list is present in the custom object "Invoice". Whenever a pdf is uploaded in the "Notes and Attachment", an email should be sent to few people. How do I achieve the same ? 
Hi All,

This is my trigger which will send an email with attachement whenever a pdf is uplaoded in Notes and Attachement. I wrote a test class but it doesn't cover the code? Would be great if anyone could help me on this. Thanks.
 
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
        
		    //Retrieve all invoices based on the requried Invoice Ids. Add your criteria to fetch 
		    // relevant invoices in the Where clause based on the Invoice and ContentVersion 
		    // object relationship. Replace <yourInvoiceIdSet> below with the actual Id Set
            List<Invoice_Payment__c> invoiceList = [Select Id, Name
										    From Invoice_Payment__c 
										   where Id IN (SELECT FirstPublishLocationId FROM ContentVersion WHERE Id IN :trigger.new)];
		
            for(ContentVersion cv : trigger.new){

                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                attachment.setBody(Blob.valueof(cv.ContentBodyId));
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                
					message.setToAddresses(new String[] { 'andre.nobre@cleantechsolar.com' ,'rupesh.baker@cleantechsolar.com'});
                    message.setccAddresses(new String[] { 'krishnamurthy.ka@cleantechsolar.com' ,'divina.minoza@cleantechsolar.com'});
                
				//Subject with the retrieved Invoice Number
				message.subject = 
					String.format('Your Invoice having Invoice # {0} is due for payment', 
						new String[]{invoiceList[0].Name}); 
                
				//Email body containing Name with the retrieved invoice
				message.plainTextBody = 
					String.format('Your invoice for {0} with the file name {1} is attached alongwith.', 
						new String[]{invoiceList[0].Name, cv.Title});
						
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

Kind Regards
Krishnamurthy
I am trying to send an email with attachment whenever a pdf is uploaded in Notes and attachment object. How do I write a test class ? Kindly help.
 
trigger EmailTrigger on ContentVersion (after insert){
    
    if(Trigger.isAfter){
        
        List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>(); 
        if(Trigger.isInsert){
            
            
            for(ContentVersion cv : trigger.new){ 
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment(); 
                attachment.setBody(Blob.valueof(cv.ContentBodyId));
                attachment.setFileName(cv.Title + '.' + cv.FileType); 
                
                Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); 
                message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' }; 
                message.ccAddresses = new String[] {'krishnamurthy.ka@cleantechsolar.com'};
                message.subject = 'Test Mail'; 
                message.plainTextBody = 'Attached file name: ' + cv.Title; 
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment}); 
                
                allMessages.add(message); 
            } 
        }
        Messaging.sendEmail(allMessages); 
    }
}

 
Hi

I am trying to send an email with pdf whenever a pdf is attached in "Notes and Attachment" section. Whenever I try to upload it is giving me an error 

10:04:27:005 FATAL_ERROR System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, No body supplied for the file attachment.: [fileAttachments]

Please find the below code for Apex trigger.
trigger EmailTrigger on ContentVersion (after insert) 
{
List<Messaging.SingleEmailMessage> allMessages = new List<Messaging.SingleEmailMessage>();
for(ContentVersion cv : trigger.new)
{
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setBody(cv.ContentBodyId);
attachment.setFileName(cv.Title + '.' + cv.FileType);

Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.toAddresses = new String[] { 'andre.nobre@cleantechsolar.com' };
message.subject = 'Test Mail';
message.plainTextBody = 'Attached file name: ' + cv.Title;
message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});

allMessages.add(message); 
}

Messaging.sendEmail(allMessages);
}
Kindly help me to rectify the same.
 
Hi,

I would appreciate if you could suggest me a sample code to automate sending pdf through mail.

So the "Notes and Attachment" related list is present in the custom object "Invoice". Whenever a pdf is uploaded in the "Notes and Attachment", an email should be sent to few people. How do I achieve the same ?