gpt4 book ai didi

javascript - 如何消除以下错误。尝试创建一个函数来在 mscrm 中创建实体的记录

转载 作者:行者123 更新时间:2023-12-03 04:44:09 25 4
gpt4 key购买 nike

在下面的代码中,我创建了两个函数,其中一个函数我使用 msd-crm 动态 365 api 代码来创建记录,在主函数中我传递参数,但当我使用数组时作为参数,它显示此错误。我能够传递的单一值。

//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

最佳答案

您的问题在于您的 JavaScript 以及您如何构建对象。首先,我认为您想要做的是:

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

而且,你也让自己陷入困境。为什么不按照正常方式在外部构建对象并将其发送进去,而不是构建这些数组?

要扩展...这样的东西可能会起作用并且看起来更好:

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];
//}

并这样调用它:

    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")

关于javascript - 如何消除以下错误。尝试创建一个函数来在 mscrm 中创建实体的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42943777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com