Search code examples
javascriptjsondynamics-crmodatamicrosoft-dynamics

Creating record using JS and JSON not correctly using owner value


I'm attempt to create a record in Dynamics 365 using JavaScript however the Owner field is not being set properly. The record creates just fine if I remove the setting of the 'ownerid' field. I have also tried formatting the guid both in lowercase and uppercase with no success (see comments in code). The fields are displayed as expected in the alert.

When the script is run both with the code that makes the guid lowercase or not, I get the following error:

Error: An error occurred while validating input paramters: Microsoft.OData.ODataException: A node of type 'StartArray' was read from the JSON reader when trying to read the contents of the property 'ownerid'; however, a 'StartObject' node or 'PrimitiveValue' node with null value was expected.

var managingDirector = Xrm.Page.getAttribute("new_managingdirector").getValue();

var md_id = managingDirector[0].id;
var md_name = managingDirector[0].name
var md_entityType = "systemuser"

//md_id = md_id.replace(/[{}]/g,"");
//md_id = md_id.toLowerCase();
//md_id = "{" + md_id + "}";

if (managingDirector != null) {
    console.log(managingDirector[0]);
    alert("MD is " + md_name + " with id " + md_id + " and type " + md_entityType);
} else {
    alert("MD is null");
}

var md_owner = new Array();
md_owner[0] = new Object();
md_owner[0].name = md_name;
md_owner[0].id = md_id;
md_owner[0].entityType = md_entityType;

var data =
{
    "new_name": "Sample Practice Management",
    "new_totalamountdue": amountDue,
    "new_deductions": deductionAmount,
    "new_deductionsnotes": deductionNotes,
     "ownerid": md_owner

}    

// create pm record
Xrm.WebApi.createRecord("new_practicemanagement", data).then(
    function success(result) {
        alert("Practice Management record created with ID: " + result.id);
        // perform operations on record creation
    },
    function (error) {
        alert("Error: " + error.message);
        // handle error conditions
    }
);

When I attempt to restructure the data variable like this (with both lowercase and uppercase ID)

var data =
{
    "new_name": "Sample Practice Management",
    "new_totalamountdue": amountDue,
    "new_deductions": deductionAmount,
    "new_deductionsnotes": deductionNotes,
    "ownerid": {
        name: md_name,
        id: md_id,
        entityType: md_entityType
    }
} 

I get the following error:

An error occurred while validating input paramters: Microsoft.OData.ODataException: Does not support untyped vvalue in non-open type.


Solution

  • When I see your code you have data i.e field and it's value as below

    var data =
    {
        "new_name": "Sample Practice Management",
        "new_totalamountdue": amountDue,
        "new_deductions": deductionAmount,
        "new_deductionsnotes": deductionNotes,
         "ownerid": md_owner
    
    } 
    

    Now if you look at my code owner id is set as

    entity["[email protected]"] = "/systemusers(58127B9D-AFBC-E811-A958-000D3AB42BE8)";
    

    Below is the code which worked for me, I just tried creating contact record.

    var entity = {};
        entity.firstname = "Webapi1";
        entity["[email protected]"] = "/systemusers(58127B9D-AFBC-E811-A958-000D3AB42BE8)";
    
        Xrm.WebApi.online.createRecord("contact", entity).then(
            function success(result) {
                var newEntityId = result.id;
            },
            function(error) {
                Xrm.Utility.alertDialog(error.message);
            }
        );
    

    To make your life easier w.r.t developement try CRMRESTBuilder you will find most of your code auto generated here.