Hi guys,
I need some help in this forum, to guide me is there anything wrong within my JavaScript. I got this code from my previous thread whereas I actually didn't have any knowledge of JavaScript. I'm from Dynamics F&O, btw.
So here is the code :
function OpenAndSaveForm(executionContext) {
//Get the form context
var formContext = executionContext.getFormContext();
// Get the form type
var formType = formContext.ui.getFormType();
if(formType == 1) { //create form
try {
// for default company
var globalContext = Xrm.Utility.getGlobalContext();
var userId = globalContext.userSettings.userId;
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";
formContext.getAttribute("mscrm_company").setValue(companyLookup);
}
},
function(error) {
console.log(error.message);
};
formContext.data.entity.save();
}
catch (e) {
Xrm.Utility.alertDialog(e.message);}
}}
This is for my model-driven app, What I want to achieve from this code is actually, whenever user click "+ new record", when the form is open, it will automatically save the record.
So I put this into my project as Web Resources and add as OnLoad event in my form. While the saving is working, in that code I also need to set my field "mscrm_company" to user's default company.
I'm not sure why, when this form open, the field company appear in the form with the correct company, but when I hit back, check from he table's raw data, the field is enpty.
May I know is there something wrong with the code? or what is happening ? Is the company displayed on the form actually not save ? or somehow overridden ?
Thanks in advance,







