var Sdk = window.Sdk || {};
(function () {
// Code to run in the form OnLoad event
this.formOnLoad = function (executionContext) {
var formContext = executionContext.getFormContext();
// Add your code from the other tables here
}
// Code to run in the column OnChange event
this.attributeOnChange = function (executionContext) {
var formContext = executionContext.getFormContext();
// Add your code from the other tables here
}
// Code to run in the form OnSave event
this.formOnSave = function (executionContext) {
var formContext = executionContext.getFormContext();
// Add your code from the other tables here
}
}).call(Sdk);
// Add your code from the other tables here
// Add your code from the other tables here
// Add your code from the other tables here
FORM EVENTS
Power Apps Model Driven Apps JavaScript Cheatsheet
JavaScript Code Snippets for Dynamics 365 Cheat Sheet by
Fredrik Engseth.
Client API Reference for model-driven apps Microsoft Docs.
var currentRow = formContext.data.entity.getEntityReference();
// Get row table type ex: “incident” or “account
var currentRowEntityType = currentRow.entityType;
// Get row GUID ex: “{67e86a65-4cd6-ec11-a7b5-000d3a9c27d2}”
var currentRowId = currentRow.id;
// Get row GUID without brackets ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c…”
var currentRowId2 = currentRow.id.replace(/{|}/g, '');
// Get row logical name ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c27d2”
var currentRowName = currentRow.name;
GET CURRENT ROW DATA
var customer = formContext.getAttribute("customerid").getValue();
// Get row table type ex: “incident” or “account
var customerEntityType = customer[0].entityType;
// Get row GUID ex: “{67e86a65-4cd6-ec11-a7b5-000d3a9c27d2}”
var customerId = customer[0].id;
// Get row logical name ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c27d2”
var customerName = customer[0].name;
READ VALUES FROM LOOKUP
// Basic retrieve
Xrm.WebApi.retrieveRecord("contact", customerId,
"?$select=firstname").then(
function success(result) {
console.log("Retrieved values: Name: " + result.firstname);
// perform operations on record retrieval
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
// Using expand
Xrm.WebApi.retrieveRecord("contact", customerId,
"?$select=firstname&$expand=modifiedby($select=fullname;$expand=businessu
nitid($select=name))").then(
function success(result) {
console.log("Name: " + result.modifiedby.fullname);
// perform operations on record retrieval
},
function (error) {
console.log(error.message);
// handle error conditions
}
);
READ VALUES FROM RELATED TABLES
//Show
formContext.getControl("caseorigincode").setVisible(true);
//Hide
formContext.getControl("caseorigincode").setVisible(false);
SHOW / HIDE FIELDS
// Set lookup value
var lookupValue = new Array();
lookupValue[0] = new Object();
lookupValue[0].id = "a431636b-4cd6-ec11-a7b5-000d3a9c27d2";
lookupValue[0].entityType = "contact";
lookupValue[0].name = "Nancy Anderson (sample)"
formContext.getAttribute("customerid").setValue(lookupValue);
// Set choices values
formContext.getAttribute("multichoice").setValue([100000000,100000001,100
000002]);
// Set text value
formContext.getAttribute("textfield").setValue("Those are the steps");
// Set number value
formContext.getAttribute("numberfield").setValue(100);
SET FIELD VALUES
// Show section within a specified tab
var tab = formContext.ui.tabs.get("Summary");
var section = tab.sections.get("Timeline");
section.setVisible(true);
// Hide section within a specified tab
var tab = formContext.ui.tabs.get("Summary");
var section = tab.sections.get("Timeline");
section.setVisible(false);
SHOW / HIDE SECTIONS
// Show tab
var tab = formContext.ui.tabs.get("Details");
tab.setVisible(true);
// Hide tab
var tab = formContext.ui.tabs.get("Details");
tab.setVisible(false);
SHOW / HIDE TABS
// Set field as required
formContext.getAttribute("fieldname").setRequiredLevel("required");
// Set field as recommended
formContext.getAttribute("fieldname").setRequiredLevel("recommended");
// Set field as optional
formContext.getAttribute("fieldname").setRequiredLevel("none");
SET REQUIRED FIELDS
// Save and refresh the form
formContext.data.refresh(true);
// Refresh the form (without saving)
formContext.data.refresh(false);
REFRESH & SAVE THE FORM
// alert dialog
var alertStrings = { confirmButtonLabel: "Yes", text: "This is an
alert.", title: "Sample title" };
var alertOptions = { height: 120, width: 260 };
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
function (success) {
console.log("Alert dialog closed");
},
function (error) {
console.log(error.message);
}
);
// confirm dialog
var confirmStrings = { text:"This is a confirmation.",
title:"Confirmation Dialog" };
var confirmOptions = { height: 200, width: 450 };
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(
function (success) {
if (success.confirmed)
console.log("Dialog closed using OK button.");
else
console.log("Dialog closed using Cancel button or X.");
});
DIALOG
// Get column value
var title = formContext.getAttribute("fieldname").getValue();
// Get choice value
var caseorigin = formContext.getAttribute("fieldname").getValue();
// Get choice text
var caseorigin = formContext.getAttribute("fieldname").getText();
READ VALUES FROM COLUMN
this.disableTab = function(formContext, tab) {
formContext.ui.tabs.get(tab).sections.forEach(function (section){
section.controls.forEach(function (control) {
control.setDisabled(true);
})
});
}
// call the function to disable all the fields in the section
Sdk.disableTab(formContext,"Summary");
SET ALL FIELDS READ-ONLY IN TAB
// Set field read-only
formContext.getControl("caseorigincode").setDisabled(true);
// Set field editable
formContext.getControl("caseorigincode").setDisabled(false);
SET READ-ONLY FIELDS
this.disableSection = function(formContext, tab, section) {
var section = formContext.ui.tabs.get(tab).sections.get(section);
var controls = section.controls.get();
var controlsLenght = controls.length;
for (var i = 0; i < controlsLenght; i++) {
controls[i].setDisabled(true);
}
}
// call the function to disable all the fields in the section
Sdk.disableSection(formContext,"Summary","Case Details Summary");
SET ALL FIELDS READ-ONLY IN SECTION
// Set field read-only
formContext.getControl("iframe").setSrc(" https://danikahil.com/");
SET URL FOR IFRAME
// Add "header process_" to the field name
// Set field as required
formContext.getAttribute("header_process_fieldname").setRequiredLevel("re
quired");
// Set field read-only
formContext.getControl("header_process_fieldname").setDisabled(true);
FIELDS IN BPF (Business Process Flow)
ADD THE JAVASCRIPT TO THE FORM
1
2
3
4
5
6
7
8
9
1. Open your form.
2. Select Form libraries (JS Logo) from
the left navigation pane.
3. Click on Add library.
4. Click on + New web resource.
5. Upload your JavaScript(JS) file and
name your web resource.
6. Open your form
7. Select Events tab. You'll notice that
both the On Save and On Load event
handlers.
8. Click on + Event Handler.
9. Configure the event by selecting On
Load or On Save.
10. Pick the library (web resource) that
you created.
11. Type the name of the function.
12. Check Enabled and Pass execution
context as first parameter.
10
11
11
12
REFERENCES
JavaScript (JS) Cheat Sheet Online (htmlcheatsheet.com)
GENERAL JS CHEATSHEET