locked
Set Default Email-Template when Create New Email RRS feed

  • Question

  • Hi,

     

    i have a question.

    Can you please tell me how to set a default template when creating a new email?

    Until yet i saved the title of the template in a field in the user-entity.

    so in the onload-Event of an email i can fetch the specific template from the crm webservice, but the body and subject contains xml with placeholders.

    How can i get the filled data?

     

    greetings

    Andy

    Monday, July 21, 2008 10:28 AM

Answers

  • Andy,

     

    Here's some code that should get you close to automatically inserting an e-mail template. This code will insert the template once you've defined the GUID for the template to load.

     

    There are lots of examples online for querying for entity data (e.g., getting a value from a contact record) from client-side code so I didn't include that in this code. But I didn't see anywhere online where someone showed how to retrieve the body and subject of an e-mail template so that's what I put together for you.

     

    Code Snippet

     

    // This script can be placed in the onload event for the New E-mail form
    // to automatically insert an e-mail template.

    // Set the e-mail template id. This is where your code will go to
    // translate an e-mail template name into the GUID-based ID.
    var emailTemplateToLoad = "A2D561D2-3D4C-DC11-9989-02BF9D366036";

     

    // Get Regarding object details
    var RegardingItems = crmForm.all.regardingobjectid.Items();
    var regardingObjectId = RegardingItems[0].id;
    var regardingObjectType = RegardingItems[0].type;

     

    var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
    command.SetParameter("templateId", emailTemplateToLoad );
    command.SetParameter("objectId", regardingObjectId);
    command.SetParameter("objectTypeCode", regardingObjectType);
    var result = command.Execute();

     

    if (result.Success)
    {
        var o = new Object();
        o.EmailBody = "";
        o.EmailSubject = "";
        if(typeof(result.ReturnValue) == "string")
        {
            oXml = CreateXmlDocument(false);
            oXml.loadXML(result.ReturnValue);
            crmForm.all.description.InsertValue(oXml.selectSingleNode("template/body").text);
            o.EmailSubject = oXml.selectSingleNode("template/subject").text;
        }
    }

     

     

    I pulled this code from a recent project and tested it but you'll likely have to tweak it to meet your needs. For example, you may not want the e-mail subject line to be overwritten by the template's subject value.

     

    Cheers,

     

    -Tim

    Monday, July 21, 2008 7:02 PM

All replies

  • Andy,

     

    Here's some code that should get you close to automatically inserting an e-mail template. This code will insert the template once you've defined the GUID for the template to load.

     

    There are lots of examples online for querying for entity data (e.g., getting a value from a contact record) from client-side code so I didn't include that in this code. But I didn't see anywhere online where someone showed how to retrieve the body and subject of an e-mail template so that's what I put together for you.

     

    Code Snippet

     

    // This script can be placed in the onload event for the New E-mail form
    // to automatically insert an e-mail template.

    // Set the e-mail template id. This is where your code will go to
    // translate an e-mail template name into the GUID-based ID.
    var emailTemplateToLoad = "A2D561D2-3D4C-DC11-9989-02BF9D366036";

     

    // Get Regarding object details
    var RegardingItems = crmForm.all.regardingobjectid.Items();
    var regardingObjectId = RegardingItems[0].id;
    var regardingObjectType = RegardingItems[0].type;

     

    var command = new RemoteCommand("EmailTemplateService", "GetInstantiatedEmailTemplate");
    command.SetParameter("templateId", emailTemplateToLoad );
    command.SetParameter("objectId", regardingObjectId);
    command.SetParameter("objectTypeCode", regardingObjectType);
    var result = command.Execute();

     

    if (result.Success)
    {
        var o = new Object();
        o.EmailBody = "";
        o.EmailSubject = "";
        if(typeof(result.ReturnValue) == "string")
        {
            oXml = CreateXmlDocument(false);
            oXml.loadXML(result.ReturnValue);
            crmForm.all.description.InsertValue(oXml.selectSingleNode("template/body").text);
            o.EmailSubject = oXml.selectSingleNode("template/subject").text;
        }
    }

     

     

    I pulled this code from a recent project and tested it but you'll likely have to tweak it to meet your needs. For example, you may not want the e-mail subject line to be overwritten by the template's subject value.

     

    Cheers,

     

    -Tim

    Monday, July 21, 2008 7:02 PM
  • Thanks thats what i was looking for.

    I want to insert a global template only with informations from the actual user. So i think i have to use the ownerid instead of the regardingobjectid. is this right?

    but in both ways i get the crm error "Invalid Argument" when it executes.

     

    Tuesday, July 22, 2008 7:59 AM
  •  

    Ok i find the problem (not really but after a few changes it works).

    But now i get a html-body and i try to insert it in the description field.

    the methos InsertValue(...) doesn't exist. When i set the DataValue of description he inserts the html-code into the description-field, like <p>Hello World</p>

    How does it work that the html-code will be transformed?

    Tuesday, July 22, 2008 9:45 AM