Search code examples
soapdynamics-crm

Unable to create the "template" record using sdk.soap.js in Dynamics CRM


Hi I am trying to create an email template record in Dynamics CRM using SDK SOAP.Below is my code

    var email_body = "ehhlo";
        var email_subject = $("#subject").val();
        var email_title = $("#title").val();

        var templateColumns = {
            "title": Sdk.String,
            "subject": Sdk.String,
            "languagecode": Sdk.Int,
            "templatetypecode": Sdk.OptionSet,
            "body": Sdk.String
        };

        var email = new Sdk.Entity("template");



        initializeEntity(email, templateColumns);

        email.setValue("title", email_title);
        email.setValue("subject", email_subject);
        email.setValue("languagecode", 1033);
        email.setValue("templatetypecode", 1);
        email.setValue("body", email_body);




        try {
            templateid= Sdk.Sync.create(email);
            alert("Created account with id: " + templateid);
        }
        catch (e) {
            throw new Error("Error on Create : " + e.message);
        }

I have tried creating using sync and async both but I got the error

An unexpected error occurred.

On capturing the logs using Fiddler I am getting the following response

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring xml:lang="en-US">An unexpected error occurred.</faultstring>
<detail><OrganizationServiceFault xmlns="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic"/>
<Message>An unexpected error occurred.</Message>
<Timestamp>2016-08-16T13:01:27.627155Z</Timestamp>
<InnerFault>
<ErrorCode>-2147220970</ErrorCode>
<ErrorDetails xmlns:a="http://schemas.datacontract.org/2004/07/System.Collections.Generic"/>
<Message>System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #9F27DAAC</Message>
<Timestamp>2016-08-16T13:01:27.6427801Z</Timestamp>
<InnerFault i:nil="true"/>
<TraceText i:nil="true"/>
</InnerFault>
<TraceText i:nil="true"/>
</OrganizationServiceFault>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

Now can some one please point me what I am doing wrong and why this System.InvalidCastException is generating.

Thanks


Solution

  • It's erroring on the cast of templatetypecode which isn't an OptionSet you should be passing the entity name here (so you'd pass "account" instead of 1).