Search This Blog

Saturday, February 8, 2025

Power Apps – Model Driven Apps/CRM – JavaScript

 1-FORM EVENTS

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

//

2-GET CURRENT ROW DATA

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;

3-READ VALUES FROM LOOKUP

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;


4-READ VALUES FROM COLUMN

// 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();

5-SET FIELD VALUES

// 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);

6-READ VALUES FROM RELATED TABLES

// 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
}
);


7-SHOW / HIDE FIELDS

//Show
formContext.getControl("caseorigincode").setVisible(true);
//Hide
formContext.getControl("caseorigincode").setVisible(false);

8-SHOW / HIDE SECTIONS

// 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);


9-SHOW / HIDE TABS

// Show tab
var tab = formContext.ui.tabs.get("Details");
tab.setVisible(true);
// Hide tab
var tab = formContext.ui.tabs.get("Details");
tab.setVisible(false);


10-SET REQUIRED FIELDS

// 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");

11-SET READ-ONLY FIELDS

// Set field read-only
formContext.getControl("caseorigincode").setDisabled(true);
// Set field editable
formContext.getControl("caseorigincode").setDisabled(false);

12-SET ALL FIELDS READ-ONLY IN SECTION


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");

13-SET ALL FIELDS READ-ONLY IN TAB

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");

14-FIELDS IN BPF (Business Process Flow)

// 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);

15-REFRESH & SAVE THE FORM

// Save and refresh the form
formContext.data.refresh(true);
// Refresh the form (without saving)
formContext.data.refresh(false);

16-DIALOG

// 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.");
});

17-SET URL FOR IFRAME

// Set field read-only formContext.getControl("iframe").setSrc(" https://XYZ.com/");

18-Link

https://htmlcheatsheet.com/js/

No comments:

Post a Comment