You need to sign in to do that
Don't have an account?
Samantha Chua
Custom Button calling Apex Class on Email Service is not Working
Hi guys,
I have got 2 codes, 1 for the apex class with webservice and the other for my custom button. While the button seems to be running fine, the class dont seem to be executing. I am unable to find where I have gone wrong. Would appreicate any form of feedback possible.
Regards,
Samantha
I have got 2 codes, 1 for the apex class with webservice and the other for my custom button. While the button seems to be running fine, the class dont seem to be executing. I am unable to find where I have gone wrong. Would appreicate any form of feedback possible.
Regards,
Samantha
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")} var email = "{!Contact.Email}"; if (email != null) { sforce.apex.execute("sendWelcomeEmailtoContactEmail","sendWelcomeEmail",{caseId:'{!Case.Id}', contactId:'{!Contact.Id}'}); alert ('Email sent. Please refresh page and check activity history.'); } else { alert ('Contact email is empty. Please specify email and try again.'); }
global class sendWelcomeEmailtoContactEmail{ WebService static void sendWelcomeEmail(ID caseId, ID contactId){ Attachment aAtt; Id templateId; list <String> ccAdd = new List<String>(); ccAdd.add('samanthachuazl@gmail.com'); try{ EmailTemplate fiberTemplate = [SELECT ID FROM EmailTemplate WHERE DeveloperName = 'receipt']; templateId = fiberTemplate.Id; //Creating a new email file attachment list and file attachment object Messaging.EmailFileAttachment [] efaList = new List<Messaging.EmailFileAttachment> () ; Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); efa.setFileName(aAtt.Name); efa.setBody(aAtt.Body); efa.setContentType(aAtt.ContentType); efaList.add(efa); //creating a new email envelope Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setSenderDisplayName('Sales'); if(efaList != null) { mail.setFileAttachments(efaList) ; } //mail.setToAddresses(toAdd); mail.setTargetObjectId(contactId); mail.setWhatid(caseId); mail.setTemplateId(templateId); //mail.setPlainTextBody('hellos'); mail.setUseSignature(false); mail.setCcAddresses(ccAdd); mail.setBccSender(false); mail.setSaveAsActivity(true); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); } catch(Exception e) { System.debug('***Catch Exception : ' + e); } } }
What Pankaj said is Right. You have just declared the Attachment aAtt; , but it is not initialised. If you want to send the attachment from the respective Contact you can try below code.
Please let us know if that helps to solve your problem.
Best Regards,
BALAJI
All Answers
It seems the cause behind the issue is not initializing the Attachment object at line no 5. Please initialize this object or fetch the Attachment using SOQL which you want to send as an attachment in an email.
What Pankaj said is Right. You have just declared the Attachment aAtt; , but it is not initialised. If you want to send the attachment from the respective Contact you can try below code.
Please let us know if that helps to solve your problem.
Best Regards,
BALAJI
Awesome. It works. I was trying to send an attachment from the Case itself. So I modified your code for the contactid part. Nonetheless, it worked (:
Thank you Pankaj too!
Regards,
Samantha