Resolve Templates

March 03, 2016
Mayuresh Borse

Resolve Templates

Class : TemplateResolver

This class will help to render the templates by resolving the template text. In this class, there is one global method defined, using which one can render the resolved text of Template.

Constructor Summary :

TemplateResolver() : No need to pass any parameter.

Method Summary :

resolveTemplate(String originalTemplate, SObjectType objectType, List<Id> recordIds, Set<String> extraFields)

Return Type : Map<SObject, String>

SObject : Corresponding object for which the template is created containing values of the fields used in template text and extraFields.

String : Resolved template text i.e. plain text containing the values of fields put up in template text .

Parameters :

1. originalTemplate : Template text of the template being used in the context.

2. objectType : sObjectType of the object being used in the context of template.

3. recordIds : Ids of the  records of the above sobjectType , whose field values will be used to render dynamic fields of template mentioned in the first parameter.

4. extraFields : The fields which one wants to get in the result along with the fields used in template text. [Suppose mobile number of the record is also needed , then user will not have to write separate query to fetch mobile number , just mention that field in the set of extraFields , returned map will fetch the mobile number also.]

How to resolve the template text :

Template is created in the context of a particular object & template text contains dynamic reference to the fields of the corresponding object .Template resolving refers to replacing the dynamic references with actual values of the field in template text . Template text can be resolved & rendered using TemplateResolver class and its resolveTemplate method .

Below are the 6 steps to use resolveTemplate:

1.Finalize the template text and format.

2.Get sObjectType of the object for which the template is created using Schema.getGlobalDescribe().

3.Create a list of Ids of the records relevant to the template context.

4.Create set of extraFields.

5.Create object instance of “smagicinteract.TemplateResolver” class.

6.Call the “resolveTemplate” method using created object instance of TemplateResolver class.

[Here above “smagicinteract” is the Namespace Prefix from the install package( SMS Magic Interact ). ]
String templateText ='Hello  {!} , welcome to SMS-Magic . Regards , {!}';

List<Contact> contactlist=[select id from Contact];

List<id> IdList=new List<id>();

for(Contact key:contactlist){



Set<String> extraFieldSet=new Set<String>{'name' , 'department','MobilePhone'};

smagicinteract.TemplateResolver temp = new smagicinteract.TemplateResolver();

Map<sobject,String> objectTextmap =temp.resolveTemplate(templateText,contactlist.getSObjectType(),IdList,extraFieldSet);

How to use the above result in sending sms :

List<smagicinteract__smsMagic__c> smsObjectList = new List<smagicinteract__smsMagic__c>();

//Set the senderId which you want to use for the following sms .

String senderId = 'smsMagic';

for(sObject objectName : objectTextmap.keySet()) {

// Iterate over the resulting map from resolveTemplate to create sms object .

           smagicinteract__smsMagic__c smsObject = new smagicinteract__smsMagic__c();

           smsObject.smagicinteract__SenderId__c = senderId;

//get mobilephone value for the selected object from the map.


// Object Type of the selected object over which SMS will be sent

          smsObject.smagicinteract__ObjectType__c = 'Contact';             

//Refer below note to set value of disableSMSOnTrigger.

          smsObject.smagicinteract__disableSMSOnTrigger__c =1;  

//External field to identify record to push delivery status.

         smsObject.smagicinteract__external_field__c = smagicinteract.ApexAPI.generateUniqueKey();

//Fetch the template text            

         smsObject.smagicinteract__SMSText__c = objectTextmap.get(objectName);            



* Note :

When you are using sendSMS method to send the SMS then please make sure that smagicinteract__disableSMSOnTrigger__c should have value as 1.

If you do not want to use sendSMS then use send sms code of trigger.  When you insert sms history record it invokes a trigger code which will send out sms to your customer/user. To use this you will have set the value of disablesmsonTrigger to 0. And just use database.insert(smsObjectList).

There will be no need of separate database.insert when you are using sendSMS method.


String response = smagicinteract.ApexAPI.sendSMS(smsObjectList);

For more details over sending sms , please refer to the link :

Why new API ? 

Previous way of rendering templates was quite cumbersome as it was including 3 methods, required to be referred in particular manner and sequence. As compare to that , this new API gives a better and simple process of use as it is just a single method with four parameters and resulting map can be effectively, easily used in further processes like sending SMS.

Limitations :

  1. If parameter 1 i.e. Template Text is passed as null or blank then the resulting map will be null.
  2. If parameter 2 i.e. sObjectType is null then the  resulting map will be null. Same is the case with parameter 3 i.e. List of record Ids.
  3. If Id field is added to set of extrafields then an error “System.QueryException: duplicate field selected: Id “  will be shown as Id field is already been taken care of in the resulting map.
  4. If the number of fields referred in  template text exceeds the count of 20 then it result will be null.
  5. If user is not having read permission in field level security over any of the fields mentioned in template text then returned result will be null.
  6. Read permission over the object for which template is created is also required.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>