Render Templates

May 16, 2014
admin

Render Templates

Class – TemplateEngine

This class is help to render the templates. In this class, there are few methods are defined, using which one can render the text of Template. The methods are as follows:

 Constructor Summary

TemplateEngine (String templateText): Initialize the templateText which is passed as the parameter.

 Method Summary

a)  List  getAllFieldsFromSMSTextOfObjectType (String objectType)
Gets all the fields from the template text (sms text) of particular object type passed as parameter such as Contact, Lead, customObject__c etc and returns the List of fields from the template text.

b)  void  getFieldMap(SObject s)
Gets the Map of fieldname and Schema.Sobjectfieldpair for sObject passed as a parameter.

c)  String  getReplacedTextForObject (SObject s, Integer loggedInUserRelated)
Gets the rendered text from the template. It replaces all the fields of sObject instance which is passed as a parameter and returns the string by replacing the values of the fields. LoggedInUserRelated = 1 when objecttype is $User or $Organization.

How to Render Template

One template refers to one particular object and template contains many fields of objects. One need to replace those fields and get the plain text. Template can be rendered using TemplateEngine class.

$User and $Organization are two special objects which referes to user and Organization from which sms is being sent.

Below are the 5 steps to use templateEngine:

  1. Initialize TemplateEngine Class with text
  2. get Fieldlist using “getFieldsFromSMSTextOfObjectType(objecttype)”;
  3. query all template fields from DB using SOQL query
  4. call getFieldMap using one “sObject” instance
  5. evaluate templatetext by providing object instance to“getReplacedTextForObject”method

 An example code to use template:

String extraFieldText = '';
List args ;
String query = '';
string userId = UserInfo.getUserId();
string orgId = UserInfo.getOrganizationId();
String tplText = 'Hi {!Contact.Name}, Wish you a Happy Birthday Regards, {!$User.Name} {!$Organization.name}';
smagicinteract.TemplateEngineTEngine = new smagicinteract.TemplateEngine(tplText);
// render templatetext for loggedin user
fields = TEngine.getFieldsFromSMSTextOfObjectType('$User');
for(string x: fields){
if(x.equalsIgnoreCase('Name'))
continue;
if(!extraFieldText.contains(x))
extraFieldText = extraFieldText + ', '+x;
}
extraFieldText = String.escapeSingleQuotes(extraFieldText);
args = new List{};
args.add(extraFieldText);
query = 'select Id, Name, MobilePhone {0} from User where id =:userId';
query = String.format(query, args);
User u = Database.query(query);
TEngine.getFieldMap(u);
TEngine.smsText = TEngine.getReplacedTextForObject(u, 1);
// render templatetext for Organization
fields = TEngine.getFieldsFromSMSTextOfObjectType('$Organization');
for(string x: fields){
if(x.equalsIgnoreCase('Name'))
continue;
if(!extraFieldText.contains(x))
extraFieldText = extraFieldText + ', '+x;
}
extraFieldText = String.escapeSingleQuotes(extraFieldText);
args = new List{};
args.add(extraFieldText);
query = 'select Id, Name, MobilePhone {0} from Organization where id = : orgId';
query = String.format(query, args);
Organization org = Database.query(query);
TEngine.getFieldMap(org);
TEngine.smsText = TEngine.getReplacedTextForObject(org, 1);
// get field values for Contact/Leads/Cases/custom object
List recordIds; // List of Ids of selected Contacts.
String objectType = 'Contact';
String nameField = 'Name';
String mobilePhoneField = 'MobilePhone';
fields = TEngine.getFieldsFromSMSTextOfObjectType(objectType);
for(string x: fields){
if(x.equalsIgnoreCase('Name'))
continue;
if(!extraFieldText.contains(x))
extraFieldText = extraFieldText + ', '+x;
}
extraFieldText = String.escapeSingleQuotes(extraFieldText);
args = new List{};
args.add(extraFieldText);
args.add(objectType);
query ='select id, '+nameField+','+mobilePhoneField+ ' {0} from {1} where id in :recordIds';
query = String.format(query, args);
sObjects = Database.query(query);
TEngine.getFieldMap(sObjects[0])
for (sObject c :sObjects){
name = String.valueOf(c.get('Name'));
mobilePhone = String.valueOf(c.get('MobilePhone');
smsText = TEngine.getReplacedTextForObject(c, 0);
/* Create smagicinteract__smsMagic__c object.
* Add required fields.
* send smsText to number as shown earlier;
*/
}
Using above code we can render templateText of type “Contact”.  Here in above code“smagicinteract” is the Namespace Prefix from the install package( SMS Magic Interact ). We can use same code for different objects by changing the objectType.

From SMS Magic Interact version 1.44 on wards this method has been deprecated .  Please use Resolve Template API for resolving template text . For details please refer to the link : Resolve Template

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>