You are here:
Create the Apex Class that Sends Emails
The ReportCardEmail Apex class sends an email based on the specified Email Template to the Contact related to the current Report_Card__c object. You can rewrite it to work with a different base object.
An Apex class used in a Vlocity Action must implement VlocityOpenInterface2 and be global.
To send email notifications if your base object is Application__c, use InsVlocityActionService:notifyApplicant.
To create the ReportCardEmail Apex class:
- In Lightning Experience, click the gear icon. Select Setup from the menu.
- In the Quick Find box, type class, then click Apex Classes.
- Click New.
- At line 1 of the new Apex Class, paste the template code.
Here is the template code for the ReportCardEmail Apex class. Be sure to copy everything, including the last three closing braces.
global with sharing class ReportCardEmail implements VlocityOpenInterface2 { private static final String NAMESPACE_PREFIX = CalculationUtilities.NAMESPACE_PREFIX; global public Boolean invokeMethod(String methodName, Map<String,Object> inputs, Map<String,Object> output, Map<String,Object> options) { Boolean success = false; if (methodName == 'notifyContact') { success = notifyContact(inputs, output, options); } return success; } /* * Sends email notification to the Contact. */ private Boolean notifyContact( Map<String,Object> inputs, Map<String,Object> output, Map<String,Object> options) { Boolean success = true; Id rcId = (Id) InsuranceUtilities.getValueFromMap('Id', inputs, options); if(rcId == null){ throw new NoRecordIdParameterException(); } List<String> contactEmailList = new List<String>(); List<Report_Card__c> rcs = [SELECT Id, Name, ContactId__c FROM Report_Card__c WHERE Id =: rcid]; if (rcs.size() == 0) { throw new NoRecordInstanceFoundException(); } Report_Card__c rc = rcs.get(0); /* Retrieval of email address */ if(rc.ContactId__c != null){ Contact con = [SELECT Id, Email FROM Contact WHERE Id =: rc.ContactId__c LIMIT 1]; if (con.Email != null) contactEmailList.add(con.Email); } if(contactEmailList.size() == 0){ throw new NoEmailAddressFoundException(); } String appReferenceLink = '<a href='+URL.getSalesforceBaseUrl().toExternalForm()+'/'+rcId+'>'+rcId+'</a>'; String emailTemplateName; Map<String, Object> invokeMethodInput = (Map<String, Object>) InsuranceUtilities.getValueFromMap('invokeMethodInput', inputs, options); if (invokeMethodInput == null) { throw new NoInvokeMethodInputParameterFoundException(); } else if (invokeMethodInput.get('emailTemplateName') == null) { throw new NoInvokeMethodInputParameterFoundException(new List<String>{'emailTemplateName'}); } emailTemplateName = (String) invokeMethodInput.get('emailTemplateName'); List<EmailTemplate> emailTemplates = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name =: emailTemplateName]; if (emailTemplates.size() == 0) { throw new NoEmailTemplateInstanceFoundException(); } EmailTemplate emailTemplate = emailTemplates.get(0); /* Sending of email */ Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses = contactEmailList; message.htmlBody = String.format(emailTemplate.HtmlValue, new List<Object>{appReferenceLink}); message.subject = emailTemplate.Subject; Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message}; Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].success) { Logger.dbg('The email was sent successfully.'); output.put('emailSentSuccessfully', true); } else { Logger.dbg('The email failed to send: ' + results[0].errors[0].message); output.put('emailSentSuccessfully', false); success = false; } return success; } private class NoEmailTemplateInstanceFoundException extends Exception {} private class NoEmailAddressFoundException extends Exception {} private class NoRecordIdParameterException extends Exception {} private class NoInvokeMethodInputParameterFoundException extends Exception { public NoInvokeMethodInputParameterFoundException(List<String> inputParameters){ System.debug('- NoInvokeMethodInputParameterFoundException.invokeMethodInput values not found: '+inputParameters); } } private class NoRecordInstanceFoundException extends Exception { public NoRecordInstanceFoundException(Id instanceId){ System.debug('- NoRecordInstanceFoundException.instanceId: '+instanceId); } } } - Customize the template code:
-
Change
ReportCardEmailto a class name that references a different base object name. -
Change all instances of
Report_Card__cto a different base object name. -
Change all instances of
ContactId__cto a different Contact lookup field name if necessary.
If you are recreating the example, leave the template code as is.
-
- Click Save.

