Resolving a Template with Merge Fields
Prerequisites
The developer will need proficiency in:
- Salesforce.com Object model
- Apex programming
What are templates?
SMS-Magic is a native application that integrates with Salesforce.com, which is primarily used to store lead and contact information. SMS communication is all about 1-to-1 communication, and many customers will give a prompt response if the message is personalized.
Merge-field templates can be used by sales or marketing teams to send personalized messages. This article explains how to do this with Salesforce.com Apex. It is very simple to implement, so that you can maintain focus on your business logic while SMS-Magic handles the SMS messaging.
Class & Method Details
Class: TemplateResolver
This class renders a template by resolving merge fields in the template text. There is only one global method definition in this class (see below).
Method: resolveTemplate
resolveTemplate(String originalTemplate, SObjectType objectType, List recordIds, Set extraFields) Return Type : Map
Parameters:
- originalTemplate – the text of the template. If this value is passed as null or blank, an exception error will be raised.
- objectType – the sObjectType of the object instance that corresponds to the template. An exception error will be raised if this is null.
- recordIds – IDs of the records in the object instance. The field values will be used to populate the dynamic fields in originalTemplate. An exception error will be raised if this is null.
- extraFields – fields in addition to those in the template text.
How to render the template text
Now, let’s see how to use the TemplateResolver class and resolveTemplate method in Apex.
First, a template containing merge fields must be created from the Template tab in SMS-Magic. It will be stored in the SMS Template custom object, which is accessible with API name
smagicinteract__SMS_Template__c.
Any template that corresponds to an object instance (and template text) will contain the merge fields of that object. Resolving a template consists of substituting actual field values in the merge fields of the template text.
Template text is resolved with the TemplateResolver class using its resolveTemplate method. Follow these steps:
- Select a predefined template record that corresponds to a Salesforce.com object.
- Using Schema.getGlobalDescribe(), set sObjectType to the object that correspond to the template.
- Create a list of IDs for the records that correspond to the template.
- Optionally, create a set of extraFields.
- Create an instance of the object smagicinteract.TemplateResolver class.
- Call the resolveTemplate method, using that object instance.
Code Sample
Feel free to copy and modify this code example:
String templateText ='Hello {!Contact.name}, Welcome to SMS-Magic . Regards, {!Contact.Account.name}'; List contactlist=[select id from Contact limit 10]; List IdList=new List(); for(Contact key:contactlist){ IdList.add(key.id); Set extraFieldSet=new Set{'name' , 'department', 'MobilePhone'}; smagicinteract.TemplateResolver temp = new smagicinteract.TemplateResolver(); Map objectTextmap =temp.resolveTemplate(templateText, contactlist.getSObjectType(), IdList, extraFieldSet);
Double checking
- Length of List of Objects (Number of record IDs) should not be greater than 200 else it will violate SF Limits.
- Don’t send the TemplateText, ObjectType, or the record list as null or blank.
- Don’t pass the Object ID field in extraFields.
- If there are more than 20 merge fields in the template, the result will be null.
- If the user does not have field-level permissions for each of the fields in the template text, then returned result will be null.
- It is also necessary for the user to have read permission for the object.