1. Get Field Value
Get the value of a field on the form:
function getFieldValue(executionContext) {
var formContext = executionContext.getFormContext();
var value = formContext.getAttribute("fieldname").getValue();
console.log("Field Value:", value);
return value;
}
2. Set Field Value
Set or update a field value:
function setFieldValue(executionContext, newValue) {
var formContext = executionContext.getFormContext();
formContext.getAttribute("fieldname").setValue(newValue);
}
3. Make Field Required or Optional
Set the requirement level of a field:
function setFieldRequired(executionContext, isRequired) {
var formContext = executionContext.getFormContext();
formContext.getAttribute("fieldname").setRequiredLevel(isRequired ? "required" : "none");
}
4. Enable or Disable a Field
Make a field editable or read-only:
function setFieldDisabled(executionContext, isDisabled) {
var formContext = executionContext.getFormContext();
formContext.getControl("fieldname").setDisabled(isDisabled);
}
5. Show or Hide a Field
Control visibility of a field on the form:
function setFieldVisibility(executionContext, isVisible) {
var formContext = executionContext.getFormContext();
formContext.getControl("fieldname").setVisible(isVisible);
}
6. Show or Hide a Section
Show or hide a specific section inside a tab:
function toggleSectionVisibility(executionContext, isVisible) {
var formContext = executionContext.getFormContext();
var tab = formContext.ui.tabs.get("tabname");
var section = tab.sections.get("sectionname");
section.setVisible(isVisible);
}
7. Add Notification to a Field
Display an error or notification message on a field:
function addFieldNotification(executionContext, message) {
var formContext = executionContext.getFormContext();
formContext.getControl("fieldname").setNotification(message);
}
Clear notification:
function clearFieldNotification(executionContext) {
var formContext = executionContext.getFormContext();
formContext.getControl("fieldname").clearNotification();
}
8. Prevent Form Save Based on Condition
Stop the form from saving if validation fails:
function preventSaveIfInvalid(executionContext) {
var formContext = executionContext.getFormContext();
var value = formContext.getAttribute("fieldname").getValue();if (!value) {
executionContext.getEventArgs().preventDefault();
alert("Field is required.");
}
}
9. Retrieve Lookup Field Value
Get the GUID and name of a lookup field:
function getLookupValue(executionContext) {
var formContext = executionContext.getFormContext();
var lookup = formContext.getAttribute("lookupfield").getValue();if (lookup != null) {
var id = lookup[0].id;
var name = lookup[0].name;
var entityType = lookup[0].entityType;
console.log(`Lookup Id: ${id}, Name: ${name}, Entity: ${entityType}`);
}
}
10. Set Lookup Field Value
Programmatically set a lookup field:
function setLookupValue(executionContext) {
var formContext = executionContext.getFormContext();
var lookupValue = [{
id: "GUID-OF-RECORD",
name: "Record Name",
entityType: "account"
}];formContext.getAttribute("lookupfield").setValue(lookupValue);
}
11. Open a New Record Form
Open a new form for an entity:
function openNewForm() {
var entityFormOptions = {
entityName: "account"
};Xrm.Navigation.openForm(entityFormOptions).then(
function (success) {
console.log("Form opened");
}, function (error) {
console.log("Error opening form: " + error.message);
}
);
}
12. Display Alert or Confirmation Dialog
Alert dialog:
function showAlert() {
Xrm.Navigation.openAlertDialog({ text: "This is an alert message!" });
}
Confirmation dialog:
function showConfirm() {
var confirmStrings = { text: "Are you sure you want to continue?", title: "Confirm" };
Xrm.Navigation.openConfirmDialog(confirmStrings).then(function (success) {
if (success.confirmed) {
console.log("User confirmed.");
} else {
console.log("User cancelled.");
}
});
}
13. Get Current User ID and Name
function getCurrentUser() {
var globalContext = Xrm.Utility.getGlobalContext();
var userId = globalContext.userSettings.userId;
var userName = globalContext.userSettings.userName;
console.log(`User ID: ${userId}, User Name: ${userName}`);
}
14. Retrieve Data Using Xrm.WebApi
Retrieve a record asynchronously:
async function retrieveAccountName(accountId) {
try {
const result = await Xrm.WebApi.retrieveRecord("account", accountId, "?$select=name");
console.log("Account Name:", result.name);
} catch (error) {
console.error("Error retrieving account:", error.message);
}
}
- Java Script
- Microsoft Dynamic CRM