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
Samantha ChuaSamantha 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
 
{!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);
        }
    }    
}
Best Answer chosen by Samantha Chua
BALAJI CHBALAJI CH
Hi Samantha,

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.
 
global class sendWelcomeEmailtoContactEmail{
    
    WebService static void sendWelcomeEmail(ID caseId, ID contactId){
        
        Attachment aAtt=[select id, name, Body, ContentType from attachment where parentId=:contactId limit 1];
        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);
            system.debug('mail'+mail);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            system.debug('Exit');
        }        
        
        catch(Exception e)
        {
            System.debug('***Catch Exception : ' + e);
        }
    }    
}

Please let us know if that helps to solve your problem.

Best Regards,
BALAJI

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

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.
BALAJI CHBALAJI CH
Hi Samantha,

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.
 
global class sendWelcomeEmailtoContactEmail{
    
    WebService static void sendWelcomeEmail(ID caseId, ID contactId){
        
        Attachment aAtt=[select id, name, Body, ContentType from attachment where parentId=:contactId limit 1];
        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);
            system.debug('mail'+mail);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            system.debug('Exit');
        }        
        
        catch(Exception e)
        {
            System.debug('***Catch Exception : ' + e);
        }
    }    
}

Please let us know if that helps to solve your problem.

Best Regards,
BALAJI
This was selected as the best answer
Samantha ChuaSamantha Chua
Hi 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!
 
aAtt = [select ID, name, Body, ContentType FROM Attachment where ParentID =: caseId limit 1];

Regards,
Samantha