InstantiateTemplateRequest using Javascript – MS CRM 2011

If you want to create new email using template or want to place email template contents to email while replying, you need to use”InstantiateTemplateRequest” to initiate email template. It has three below parameters

TemplateId (ID of the email template that you want to use)

ObjectId (associated entity record id, (for example you have used data field from user entity then this will be the system user id)

ObjectType (data field’s associated entity name)

In my example, I have used system user entity’s data fields in my template, so here is the code to Initiate email template using Soap request.

function InstatiateEmailTemplate(TemplateID, FromUser) {
var Soapreq = “<s:Envelope xmlns:s=’http://schemas.xmlsoap.org/soap/envelope/’>” +
“<s:Body><Execute xmlns=’http://schemas.microsoft.com/xrm/2011/Contracts/Services’ xmlns:i=’http://www.w3.org/2001/XMLSchema-instance’>” +
“<request i:type=’b:InstantiateTemplateRequest’ xmlns:a=’http://schemas.microsoft.com/xrm/2011/Contracts’ xmlns:b=’http://schemas.microsoft.com/crm/2011/Contracts’>” +
“<a:Parameters xmlns:c=’http://schemas.datacontract.org/2004/07/System.Collections.Generic’>” +
“<a:KeyValuePairOfstringanyType>” +
“<c:key>TemplateId</c:key>” +
“<c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>” + TemplateID + “</c:value>” +
“</a:KeyValuePairOfstringanyType>” +
“<a:KeyValuePairOfstringanyType>” +
” <c:key>ObjectType</c:key>” +
”          <c:value i:type=’d:string’ xmlns:d=’http://www.w3.org/2001/XMLSchema’>systemuser</c:value>” +
”       </a:KeyValuePairOfstringanyType>” +
”      <a:KeyValuePairOfstringanyType>” +
”       <c:key>ObjectId</c:key>” +
”      <c:value i:type=’d:guid’ xmlns:d=’http://schemas.microsoft.com/2003/10/Serialization/’>” + FromUser + “</c:value>” +
”   </a:KeyValuePairOfstringanyType>” +
“</a:Parameters>” +
“<a:RequestId i:nil=’true’ />” +
“<a:RequestName>InstantiateTemplate</a:RequestName>” +
“</request>” +
“</Execute>” +
“</s:Body>” +
“</s:Envelope>”;
}

Enjoy !!

Note: if you are copying this code, please make sure to change quotes.

5 thoughts on “InstantiateTemplateRequest using Javascript – MS CRM 2011

  1. Albi

    Hey, I have to do something like this just for service-cases. In my case I have to write an email using a email template to a customer. I want, that if I press a button the fields are automatically filled with a email template which is associated with this button…

    Could you tell me how to do this?

    Reply
    1. HIMBAP Post author

      You can use a direct email option and select you template for creating email it will automatically have template fields populated automatically. or can have custom ribbon button to use above request. contact us if you need help to implement this requirement.

      Reply

Leave a Reply