0% found this document useful (0 votes)
450 views

Power Apps Model Driven Apps JavaScript Cheatsheet 1 PDF

This JavaScript cheatsheet provides examples of common tasks when working with model-driven apps and Dynamics 365 forms using JavaScript, including: 1. Retrieving values from related records and performing operations on the retrieved data. 2. Setting all fields in a section or tab to read-only. 3. Getting data from the current row, like the entity type, GUID, and logical name. 4. Showing/hiding individual fields or entire sections.

Uploaded by

Carlo Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
450 views

Power Apps Model Driven Apps JavaScript Cheatsheet 1 PDF

This JavaScript cheatsheet provides examples of common tasks when working with model-driven apps and Dynamics 365 forms using JavaScript, including: 1. Retrieving values from related records and performing operations on the retrieved data. 2. Setting all fields in a section or tab to read-only. 3. Getting data from the current row, like the entity type, GUID, and logical name. 4. Showing/hiding individual fields or entire sections.

Uploaded by

Carlo Rossi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Power Apps – Model Driven Apps – JavaScript Cheatsheet

ADD THE JAVASCRIPT TO THE FORM

FORM EVENTS READ VALUES FROM RELATED TABLES SET ALL FIELDS READ-ONLY IN SECTION 1

var Sdk = window.Sdk || {}; // Basic retrieve this.disableSection = function(formContext, tab, section) {
Xrm.WebApi.retrieveRecord("contact", customerId, var section = formContext.ui.tabs.get(tab).sections.get(section); 4
(function () { "?$select=firstname").then( var controls = section.controls.get(); 3
function success(result) { var controlsLenght = controls.length;
// Code to run in the form OnLoad event console.log("Retrieved values: Name: " + result.firstname); for (var i = 0; i < controlsLenght; i++) {
11 this.formOnLoad = function (executionContext) { // perform operations on record retrieval controls[i].setDisabled(true); 2
var formContext = executionContext.getFormContext(); }, }
function (error) { }
// Add
// Add your
your code
code from
from the
the other
other tables
tables here
here console.log(error.message);
// handle error conditions // call the function to disable all the fields in the section
} } Sdk.disableSection(formContext,"Summary","Case Details Summary");
);
// Code to run in the column OnChange event 1. Open your form. 5
this.attributeOnChange = function (executionContext) { // Using expand
var formContext = executionContext.getFormContext(); Xrm.WebApi.retrieveRecord("contact", customerId,
"?$select=firstname&$expand=modifiedby($select=fullname;$expand=businessu SET ALL FIELDS READ-ONLY IN TAB 2. Select Form libraries (JS Logo) from
nitid($select=name))").then(
the left navigation pane.
// Add
// Add your
your code
code from
from the
the other
other tables
tables here
here
function success(result) { this.disableTab = function(formContext, tab) {
} console.log("Name: " + result.modifiedby.fullname); formContext.ui.tabs.get(tab).sections.forEach(function (section){
// perform operations on record retrieval
// Code to run in the form OnSave event },
section.controls.forEach(function (control) {
control.setDisabled(true); 3. Click on Add library.
this.formOnSave = function (executionContext) { function (error) { })
console.log(error.message);
4. Click on + New web resource.
var formContext = executionContext.getFormContext(); });
// handle error conditions }
//
// Add
Add your
your code
code from
from the
the other
other tables
tables here
here }
);
}
// call the function to disable all the fields in the section
Sdk.disableTab(formContext,"Summary"); 5. Upload your JavaScript(JS) file and
}).call(Sdk); name your web resource.

GET CURRENT ROW DATA SHOW / HIDE FIELDS FIELDS IN BPF (Business Process Flow)
var currentRow = formContext.data.entity.getEntityReference(); //Show
formContext.getControl("caseorigincode").setVisible(true); // Add "header process_" to the field name

// Get row table type ex: “incident” or “account” // Set field as required
var currentRowEntityType = currentRow.entityType; //Hide
formContext.getControl("caseorigincode").setVisible(false); formContext.getAttribute("header_process_fieldname").setRequiredLevel("re
quired");
// Get row GUID ex: “{67e86a65-4cd6-ec11-a7b5-000d3a9c27d2}”
var currentRowId = currentRow.id; // Set field read-only
formContext.getControl("header_process_fieldname").setDisabled(true);
// Get row GUID without brackets ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c…” 6
var currentRowId2 = currentRow.id.replace(/{|}/g, '');
SHOW / HIDE SECTIONS 7
// Get row logical name ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c27d2”
var currentRowName = currentRow.name; // Show section within a specified tab
var tab = formContext.ui.tabs.get("Summary");
REFRESH & SAVE THE FORM
var section = tab.sections.get("Timeline");
// Save and refresh the form
READ VALUES FROM LOOKUP
section.setVisible(true);
formContext.data.refresh(true);
// Hide section within a specified tab
// Refresh the form (without saving)
var tab = formContext.ui.tabs.get("Summary");
var customer = formContext.getAttribute("customerid").getValue(); formContext.data.refresh(false);
var section = tab.sections.get("Timeline");
section.setVisible(false);
// Get row table type ex: “incident” or “account”
var customerEntityType = customer[0].entityType;
8
// Get row GUID ex: “{67e86a65-4cd6-ec11-a7b5-000d3a9c27d2}”
var customerId = customer[0].id;
DIALOG
6. Open your form
// Get row logical name ex: “67e86a65-4cd6-ec11-a7b5-000d3a9c27d2” SHOW / HIDE TABS // alert dialog
var customerName = customer[0].name; var alertStrings = { confirmButtonLabel: "Yes", text: "This is an
9
// Show tab
alert.", title: "Sample title" }; 7. Select Events tab. You'll notice that
var alertOptions = { height: 120, width: 260 };
var tab = formContext.ui.tabs.get("Details");
Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then( both the On Save and On Load event
READ VALUES FROM COLUMN
tab.setVisible(true);
function (success) { handlers.
console.log("Alert dialog closed"); 10
// Hide tab
},
var tab = formContext.ui.tabs.get("Details");
// Get column value
var title = formContext.getAttribute("fieldname").getValue(); tab.setVisible(false);
function (error) { 8. Click on + Event Handler.
console.log(error.message);
}
11
// Get choice value
var caseorigin = formContext.getAttribute("fieldname").getValue();
); 9. Configure the event by selecting On
// confirm dialog Load or On Save. 12
SET REQUIRED FIELDS
// Get choice text var confirmStrings = { text:"This is a confirmation.",
var caseorigin = formContext.getAttribute("fieldname").getText(); title:"Confirmation Dialog" };
var confirmOptions = { height: 200, width: 450 }; 10. Pick the library (web resource) that
// Set field as required
Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then( you created.
SET FIELD VALUES
function (success) {
formContext.getAttribute("fieldname").setRequiredLevel("required");
if (success.confirmed)
// Set field as recommended
console.log("Dialog closed using OK button."); 11. Type the name of the function.
else
// Set lookup value formContext.getAttribute("fieldname").setRequiredLevel("recommended");
console.log("Dialog closed using Cancel button or X.");
var lookupValue = new Array();
lookupValue[0] = new Object(); // Set field as optional
}); 12. Check Enabled and Pass execution
lookupValue[0].id = "a431636b-4cd6-ec11-a7b5-000d3a9c27d2"; formContext.getAttribute("fieldname").setRequiredLevel("none"); context as first parameter.
lookupValue[0].entityType = "contact";

SET URL FOR IFRAME


lookupValue[0].name = "Nancy Anderson (sample)"
formContext.getAttribute("customerid").setValue(lookupValue);

SET READ-ONLY FIELDS


// Set choices values
formContext.getAttribute("multichoice").setValue([100000000,100000001,100 // Set field read-only
000002]); formContext.getControl("iframe").setSrc(" https://danikahil.com/");

// Set field read-only


REFERENCES
// Set text value
formContext.getAttribute("textfield").setValue("Those are the steps"); formContext.getControl("caseorigincode").setDisabled(true);
• JavaScript Code Snippets for Dynamics 365 – Cheat Sheet – by
// Set number value // Set field editable GENERAL JS CHEATSHEET Fredrik Engseth.
formContext.getAttribute("numberfield").setValue(100); formContext.getControl("caseorigincode").setDisabled(false);
JavaScript (JS) Cheat Sheet Online (htmlcheatsheet.com) • Client API Reference for model-driven apps – Microsoft Docs.

You might also like