web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

[CRM 2013] Weird error when creating record through js (SDK.REST)

(0) ShareShare
ReportReport
Posted on by

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!")
    },
    errorHandler);
    XrmSvcToolkit.createRecord(integrationTrigger);

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.

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    
    //Get customer lookup information
        var customer = new Array();
     var name = "", id = "", entityType = "";
        customer = Xrm.Page.getAttribute("new_customerid").getValue();
        if (customer != null) {
            name = customer[0].name;
            id = customer[0].id;
            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
        };
     
     if(id !== "")
     {
         integrationTrigger.custom_Customer = {
       Id: id,
       Name: name,
       LogicalName: entityType
      };
     }
    
    SDK.REST.createRecord(integrationTrigger, "custom_integrationtrigger", function (integrationTrigger) {
            alert("everything went well!")
        },
        errorHandler);
        //XrmSvcToolkit.createRecord(integrationTrigger);
    


    In your code, there is a possibility that customer will be null. Also it seems you are creating the record twice. One using SDK.REST.createRecord, then XrmSvcToolkit.createRecord.

  • Suggested answer
    joenewstrom Profile Picture
    6 on at

    1. recommend replacing

    var invoiceName = Xrm.Page.getAttribute("new_name");

    with

    var invoiceName = Xrm.Page.data.entity.getPrimaryAttributeValue()

    but this is optional, assuming the primary name is correct.

    However, the critical part is nesting your create statement (SDK.REST or XrmSvcToolkit, one or the other) inside an if (customer != null) {} statement.

    If you invoke the request with a null object as the parameter, you're going to have a bad time.

    So at the end,

    if(customer != null) {
    SDK.REST.createRecord(integrationTrigger, "custom_integrationtrigger", function (integrationTrigger) { alert("everything went well!") }, errorHandler); /*XrmSvcToolkit.createRecord(integrationTrigger); ---This is not necessary*/
    } else { Xrm.Utility.alertDialog("Cannot create integration record with a null customer value");
    }

    Something like that. Sorry for the multiple edits.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans