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
mnickelsmnickels 

How to automatically email a link to a file attachment

Hello,

When a user request a graphic I have my graphic designers upload a file under the chatter section of the Case Object in SalesForce.com.  I can see that by default everyone with a link can view this document.

When the Case status is changed, I have a workflow process in place to automatically send an email out to certain roles.

I would like to include a link to any file attachments related to the specific case in this automatic email.

How can I do this?

Thanks,

- Mike

imutsavimutsav
Chatter files attached to a particular object is stored as an attachment on that Object. In your case when the file is attached on the Case the file is attached as an attachment on the Case. I don't think you would be able to get this link through work flow rule. Rather you need to write a trigger to get this done. Trigger would be on Case Object. If the status of the case is changed then check all the related Attachment and send the email with the link of this attachment.

Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]
mnickelsmnickels

I am new to SalesForce - is there an example of similar Apex code to accomplish this?

 

Thanks,

 

Mike

imutsavimutsav

 

This should give you some help.

 

trigger trgSendEmailOnCaseStatusChange on Case (after update) {

for(Case c:Trigger.New) {

if(c.status=='xyz') {

List<Attachment> attachmentLists = 'Select Id from Attachment where ParentID=:c.Id];

String msg = 'Your email msg here ';

for(Attachment at:attachmentLists) {

msg = msg + '<BR>' + URL.getSalesforceBaseUrl().toExternalForm() +'/' + at.Id;

}

 

List<String> toList = new List<String>();
toList.add('xyz@abc.com');

mail.setReplyTo('abcd@abc.com');

mail.setSenderDisplayName('noreply@abc.com');
mail.setToAddresses(toList);

mail.setSubject('your subject');

mail.setHtmlBody(msg);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

}

}

 

Thanks

Utsav

 

[Do mark this answer as solution if it works for you and give a kudos.]

mnickelsmnickels

EDIT

 

Here is my updated code - I have two problems:

1) Owner.ID and Owner.Email are returning null - how can I fix that?

2) The attachments piece is not working - I've uploaded a document but it does not get added to the email.

 

Thanks,

 

Mike

 

 

trigger trgSendEmailOnCaseStatusChange on Case (after update) {
    for(Case c:Trigger.New) {
        if(c.status=='Finished Production') {
            List<Attachment> attachmentLists = [Select Id from Attachment where ParentID=:c.Id];
            String msg = 'Subject';
            for(Attachment at:attachmentLists) {   
                msg = msg + '<BR>' + URL.getSalesforceBaseUrl().toExternalForm() +'/' + at.Id;   
            }
            List<String> toList = new List<String>();
            
            //These evaluate to null - can i fix?
            msg = msg + '<BR>Owner.ID: ' + c.Owner.Id;  
            msg = msg + '<BR>Owner.Email: ' + c.Owner.Email;
                        
            // Contact cOwner = [select Email from Contact where Id =:c.Owner.ID and Email!=null];
            // toList.add(cOwner.Email);
            toList.add('myemail@abc.com');
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            // mail.setReplyTo('postmaster@abc.com');
                // mail.setSenderDisplayName('postmaster@abc.com');
            mail.setToAddresses(toList);
                mail.setSubject('subject');
            mail.setHtmlBody(msg);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        }
    }
}

 

EDIT

 

When I go to the developer console and type "SELECT Attachment.ID FROM Attachment" I get no results even though I've uploaded 2 attachments to a case - a pdf and a txt file.

 

What am I doing wrong? I am working in a sandbox if that makes a difference.

 

EDIT

 

I changed the select statement to List<ContentDocumentLink> attachmentLists = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=:c.Id];

 

A couple questions:

1) How do I get the correct link to the exact document using the content document object?

2) Can I use this trigger to make sure that the documents sharing is set to "Anyone with a link"

 

Thanks

 

imutsavimutsav
My apologies, I forgot the chatter part. However I believe that this would help you or atleast it would guide you to the right direction.

To get the ownerId you need to modify your updated code you need to add
Select c.LinkedEntityId, c.Id, c.ContentDocumentId,c.ContentDocument.Title,c.ContentDocument.ownerId From ContentDocumentLink c WHERE LinkedEntityId=:c.Id

To get the correct link to the content Document you need to add

for(ContentDocumentLink cLink:attachmentLists) {
msg = msg + '<BR>' + URL.getSalesforceBaseUrl().toExternalForm() +'/' + cLink.Id;
}

I don't think you would be able to make sure if the link to the document is accessible to that person before sending it to them. This link will work for anyone who has the access to view this record.

Thanks
Utsav

[Do mark this answer as solution if it works for you and give a kudos.]