padfoot
padfoot

Reputation: 21

How to remove following error. trying to create a function to create an entity's record in mscrm

In the below code, I have created two functions where through one I am using msd-crm dynamic 365 api code , to create a record and in main function I am passing the parameters but when I am using an array as parameter, its showing this error. single value I am able to pass.

//Generic.js

if (typeof (API) == "undefined")
{ API = {}; }
API.CommonFunctions = {

    CreateRequest: function (entityname, fieldNames, fieldValues, process, MSCRMcallerId, requestType) {
        debugger;
        var entity = {};
        
        
        for (var a = 0; a < fieldNames.length; a++) {

            entity.fieldNames[a] = fieldValues[a];
        }
        
        //entity.lastname = fieldValues;


        var req = new XMLHttpRequest();
        req.open(requestType, Xrm.Page.context.getClientUrl() + "/api/data/v8.2/" + entityname, process);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

        req.setRequestHeader("MSCRMCallerID", MSCRMcallerId);
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 204) {
                    var uri = this.getResponseHeader("OData-EntityId");
                    var regExp = /\(([^)]+)\)/;
                    var matches = regExp.exec(uri);
                    var newEntityId = matches[1];
                } else {
                   Xrm.Utility.alertDialog(this.statusText); //
                }
            }
        };
        req.send(JSON.stringify(entity));
    }
}






//main.js

if (typeof (API) == "undefined")
{ API = {}; }
API.MainFuntions = {

    CreateAccount: function(){

        var fieldNames = ["lastname", "gendercode", "wholenumber"];

        var fieldValues = ["Hello", 1,12];

        API.CommonFunctions.CreateRequest("contacts", fieldNames,fieldValues, "true", "1232", "POST")

    }
}

enter image description here

Upvotes: 0

Views: 52

Answers (1)

Conor Gallagher
Conor Gallagher

Reputation: 1499

Your problem is with your JavaScript and how you're building your object. Firstly, I think what you're trying to do is this:

    for (var a = 0; a < fieldNames.length; a++) {
        entity[fieldNames[a]] = fieldValues[a];
    }

But also, you're making things difficult on yourself. Why not just build the object outside as per normal and just send it in rather than building these arrays?

To exand... something like this might work and look better:

CreateRequest: function (entityname, entity, process, MSCRMcallerId, requestType) {
    debugger;
    // don't need all of this now as your passing in an entity...
    //var entity = {};
    //for (var a = 0; a < fieldNames.length; a++) {
    //    entity.fieldNames[a] = fieldValues[a];
    //}

And call it like this instead:

    var contact = {
        lastname:"Hello",
        gendercode:1
      };

    // or even set properties on it like this:
    contact.wholenumber= 12;

   // And just send in the entity
    API.CommonFunctions.CreateRequest("contacts", contact, "true", "1232", "POST")

Upvotes: 1

Related Questions