JavaScript to save a form:
function saveRecordOnChange(executionContext) {
try {
let formContext = executionContext.getFormContext();
if (formContext) {
let confirmStrings = { text: "This is the text in the dialog box", title: "This is the Title" };
let confirmOptions = { height: 200, width: 450 };
// Open Confirm Dialog
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
function (success) {
if (success.confirmed) {
// Save the Record if OK is clicked
formContext.data.save();
}
});
}
}
catch (ex) {
throw new Error(ex.message);
}
}
Get and Set Values:
// get a field value
formContext.getAttribute("cds_myfieldname").getValue();
// set a field value
formContext.getAttribute(schemaname).setValue("myvalue");
Set entities to search on a look up: See MS Docs
function onFormRestrictEntity(executionContext) {
var formContext = executionContext.getFormContext();
//formContext.getControl(arg).setEntityTypes([entityLogicalNames]);
formContext.getControl('customerid').setEntityTypes(['contact']);
}
Get User Info including GUID
For full user context see the MS doc.
// Get the User GUID
var UserId = Xrm.Utility.getGlobalContext().userSettings.userId.replace("{", "").replace("}", "");
Use the built in dialogs
Xrm.Navigation.openErrorDialog({ message: 'The important Date field needs to be populated.'});