You need to sign in to do that
Don't have an account?
mnickels
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
Thanks
Utsav
[Do mark this answer as solution if it works for you and give a kudos.]
I am new to SalesForce - is there an example of similar Apex code to accomplish this?
Thanks,
Mike
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.]
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
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.]