Here’s a quick reference guide covering Microsoft Dynamics CRM syntax for common java script.
Most of the examples are provided as functions that you can easily test in the On Load and On Save by generating form Context from execution Context.
Save Form
function SaveForm(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Save Form
formContext.data.entity.save();
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Save Form and Close Record
function SaveFormAndClose(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Save Form and Close
formContext.data.entity.save(“saveandclose”);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Save Form and Open Create Form
function SaveFormAndOpenCreateForm(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Save Form and Close
formContext.data.entity.save(“saveandnew”);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Close Form
function CloseForm(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Close Form
formContext.ui.close();
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Save the data on Read Only Field
function ForceSaveDataOnReadOnlyField(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Save forcefully
formContext.getAttribute(“new_employeetype”).setSubmitMode(“always”);
//Save forcefully
formContext.getAttribute(“new_employeetype”).setSubmitMode(“never”);
//Save forcefully
formContext.getAttribute(“new_employeetype”).setSubmitMode(“dirty”);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Get Dirty Fields from Form
function GetFormDirtyFields(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
var attributes = formContext.data.entity.attributes.get()
for (var i in attributes) {
var attribute = attributes[i];
if (attribute.getIsDirty()) {
Xrm.Utility.alertDialog(“Attribute dirty: ” + attribute.getName());
}
}
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Get Form Type
function GetFormType(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
//Get Form Type
var formType = formContext.ui.getFormType();
Xrm.Utility.alertDialog(formType);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}