Hello guys,
Need your help to lead me what's wrong with my JavaScript code. Forgive me because I have no experience in JavaScript, I only get this from the web with addition of my needed.
function CreateRecord() {
var globalContext = Xrm.Utility.getGlobalContext();
var userId = globalContext.userSettings.userId;
var today = new Date();
try {
var entityName = "xyz_orders"; //Entity Logical Name
//Default company
Xrm.WebApi.online.retrieveRecord("systemuser", userId, "?$expand=cdm_Company($select=cdm_companyid,cdm_name)").then(
function success(result) {
if (result.hasOwnProperty("cdm_Company")) {
var cdm_Company_cdm_companyid = result["cdm_Company"]["cdm_companyid"];
var cdm_Company_cdm_name = result["cdm_Company"]["cdm_name"];
var companyLookup = new Array();
companyLookup[0] = new Object();
companyLookup[0].id = cdm_Company_cdm_companyid;
companyLookup[0].name = cdm_Company_cdm_name;
companyLookup[0].entityType = "cdm_company";
var companycode = companyLookup;
}
},
function(error) {
console.log(error.message);
}
);
//Data used to Create record
var data = {
"xyz_description": "New order",
"xyz_company": companycode,
"xyz_date": today
};
Xrm.WebApi.createRecord(entityName, data).then(
function success(result) {
Xrm.Utility.alertDialog("Success");
},
function (error) {
Xrm.Utility.alertDialog("Error creating header");
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
The error is actually what I highlighted in red, companycode. It seems when it is run, it is said company code is not defined. I thought to defined variable is only adding "var" in front of it ?
Your kind help is appreciated.
Thanks