Create entity record through Javascript , CRM 2013 , SDK.Rest.CreateRecord RRS feed

  • Question

  • CRM 2013

    I am using SDK.REST.js to create a new record of a custom entity using the createRecord function, all according to msdn (http://msdn.microsoft.com/en-us/library/gg334427.aspx)

    This worked initially, but now - a couple of weeks later- this isn't. I don't know if the code has been changed but when I am going through it with alerts everything seems just fine..

    Basically I create an object with 3 attributes, 1 text and 2 other objects (because its 1 text field and 2 lookup fields in the record that is being created)

    If I run the function I get "Internal Server Error: 340. Nullable object must have a value." , and by commenting out the attributes in the object to see what was causing the problem i found that it is one of the object attributes/lookup values and the rest works fine, record gets created when I don't set this lookup value.

    This is the code to create the object: 

    //Get customer lookup information
        var customer = new Array();
        customer = Xrm.Page.getAttribute("new_customerid").getValue();
        if (customer != null) {
            var name = customer[0].name;
            var id = customer[0].id;
            var entityType = customer[0].entityType;
        //Get current record information
        var invoiceId = Xrm.Page.data.entity.getId();
        var entityName = Xrm.Page.data.entity.getEntityName();
        var invoiceName = Xrm.Page.getAttribute("new_name").getValue();
        var newId = invoiceId.replace('{', '');
        newId = newId.replace('}', '');
    //Create IntegrationTrigger record
        var integrationTrigger = {};
        integrationTrigger.custom_name = "Sync'ed invoice: " + invoiceName;
        integrationTrigger.custom_Invoice = 
            Id: invoiceId,
            Name: invoiceName,
            LogicalName: entityName
        integrationTrigger.custom_Customer = {
            Id: id,
            Name: name,
            LogicalName: entityType
    SDK.REST.createRecord(integrationTrigger, "custom_integrationtrigger", function (integrationTrigger) {
            alert("everything went well!")

    The failing attribute that the SDK.REST.createRecord function isn't liking is "IntegrationTrigger.custom_Invoice". By alerting Id, Name and LogicalName for both invoice and customer objects, they look exactly the same and correct. Still - one lookup is getting set properly, not the other.. 

    Does anybody have any idea why? I'm at the end of the rope.

    Friday, August 15, 2014 10:02 AM


  • If this code block works fine , maybe there will be a plugin of this entity create and it throws the exception?

    Polat Aydın Crm Software Developer

    • Marked as answer by Simon Billeng Friday, August 15, 2014 1:59 PM
    Friday, August 15, 2014 10:12 AM

All replies

  • If this code block works fine , maybe there will be a plugin of this entity create and it throws the exception?

    Polat Aydın Crm Software Developer

    • Marked as answer by Simon Billeng Friday, August 15, 2014 1:59 PM
    Friday, August 15, 2014 10:12 AM
  • This code block should be working fine, but it isn't. the createRecord function in SDK.REST gives me the error, so the record is never created. 

    There are no null values in the invoice object, it has a name, a GUID and a logicalname, so the record should be created just fine with that lookup value, just as the customer lookup is.. 

    Friday, August 15, 2014 10:43 AM
  • I used this method in real life projects. You must add JSON and JQUERY libraries to work it out

    var myentityobject= {};
    myentityobject.new_surveyid = { Id: guidoflookup, LogicalName: logicalname, Name: nameoflookup };
    myentityobject.new_surveyname = "trailer";
    function CreateRecord(dataSetName, rEntityObject) {
        var serverUrl = Xrm.Page.context.getServerUrl()
    	var oDataEndPoint = "/XRMServices/2011/OrganizationData.svc/";
            if (rEntityObject == null || rEntityObject == "") {
                alert("Entity Information is required!");
            var odataUri = serverUrl + oDataEndPoint + dataSetName;
            var jsonEntity = window.JSON.stringify(rEntityObject);
            var result;
            //Asynchronous AJAX function to Create a CRM record using OData
                type: "POST",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: odataUri,
                cache: false,
                async: true,
                data: jsonEntity,
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.             
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                success: function (data, textStatus, XmlHttpRequest) {
                    if (data && data.d && data.d.results) {
                        result = data.d.results;
                    else if (data && data.d) {
                        result = data.d;
                    else {
                        result = data;
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    alert("Error : " + jQuery.parseJSON(XmlHttpRequest.responseText).error.message.value);

    Polat Aydın Crm Software Developer

    Friday, August 15, 2014 10:58 AM
  • Okay, thanks :) But I'd like to learn what has happened/what was the problem and not just avoid the issue by working around it. Thanks anyway

    I tried your function, I copied it and called it CreateRecord("custom_integrationtriggerSet", integrationTrigger)  , still get the error "Nullable object must have a value".. What is going on!

    Friday, August 15, 2014 11:28 AM
  • Your code seems to be true.Make a debug will give more information about your error. BTW pls mark as answer or vote  if it is helpful for you

    Polat Aydın Crm Software Developer

    Friday, August 15, 2014 11:49 AM
  • I appreciate the help but I can't mark your reply as we haven't found a solution yet.
    Friday, August 15, 2014 12:14 PM
  • My function  works in live project. It is impossible to work wrong: ) Also i tried the code above it works perfectly.

    Is there any plugin in this entity(custom_integrationtrigger) working on Create? 

    Also please check your entity,fields name that used in your code is same with the SchemaNames.

    Polat Aydın Crm Software Developer

    Friday, August 15, 2014 12:54 PM
  • Okay you were right from the very start, it was the plugin that was on Post Create of that entity. 

    But what really confused me was how this error "Nullable object must have a value" was being returned from the javascript function to create the record.. I thought that meant it had to be the creation that was failing, because whatever errors the plugin registered on that other entitys create message should be handled by its own. Could you explain why I get the plugins errormessage in the javascript function? All that JS function should do is to create the record and return a successcallback, then my plugin should fire immediately after and throw an exception on its own?

    I will now happily mark your answer as helpful! :)

    Friday, August 15, 2014 1:58 PM