JavaScript:
In this blog, we will see the most used javascript codes used in the dynamics 365
Set a field’s value based on another field’s value
function calculateTotal(e) {
var formContext = e.getFormContext();
var quantity = formContext.getAttribute(“new_quantity”).getValue();
var unitPrice = formContext.getAttribute(“new_unitprice”).getValue();
var totalField = formContext.getAttribute(“new_total”);
if (quantity && unitPrice) {
totalField.setValue(quantity * unitPrice);
}
}
Display an alert when a specific field is changed
formContext.getAttribute(“new_field”).addOnChange(function() {
alert(“Field value changed!”);
});
Display a Confirmation Dialog on Form Save
function confirmSave() {
if (confirm(“Are you sure you want to save this record?”)) {
Xrm.Page.data.save();
}
}
Hide/Show a Section Based on Option Set Value
function toggleSection(e) {
var formContext = e.getFormContext();
var optionSetValue = formContext.getAttribute(“new_optionset”).getValue();
var section = formContext.ui.tabs.get(“tab_name”).sections.get(“section_name”);
if (optionSetValue === 1) {
section.setVisible(true);
} else {
section.setVisible(false);
}
}
Change the requirement level of a field based on the value of another field.
function setRequirementLevel(e) {
var formContext = e.getFormContext();
var triggerField = formContext.getAttribute(“new_triggerfield”).getValue();
var dependentField = formContext.getAttribute(“new_dependentfield”);
if (triggerField === 1) {
dependentField.setRequiredLevel(“required”);
} else {
dependentField.setRequiredLevel(“none”);
}
}
Refresh the form to display the most up-to-date data from the server.
formContext.data.refresh(false);
Set the value of a lookup field with a specific record.
function setLookupValue(e) {
var formContext = e.getFormContext();
var accountId = “{Account-GUID}”;
var accountName = “Sample Account”;
var lookupValue = [{ id: accountId, name: accountName, entityType: “account” }];
formContext.getAttribute(“new_lookupfield”).setValue(lookupValue);
}
Set default values for certain fields when creating a new record.
function setDefaultValues(e) {
var formContext = e.getFormContext();
if (formContext.ui.getFormType() === 1 /* Create */) {
formContext.getAttribute(“new_status”).setValue(1 /* Default Status Value */);
formContext.getAttribute(“new_assignedto”).setValue(formContext.context.getUserId());
}
}
Show/Hide Fields Based on Option Set Value
function toggleFields(e) {
var formContext = e.getFormContext();
var selectedOption = formContext.getAttribute(“new_optionset”).getValue();
var fieldToToggle = formContext.getControl(“new_fieldtotoggle”);
if (selectedOption === 2 /* Specific Option Value */) {
fieldToToggle.setVisible(true);
} else {
fieldToToggle.setVisible(false);
}
}
Show Notification on Form Load
function showFormNotification(e) {
var formContext = e.getFormContext();
formContext.ui.setFormNotification(“Welcome to Dynamics 365!”, “INFO”, “notificationId”);
}
formContext.context.client.getClient() === “Web” && formContext.ui.formType === 2 && showFormNotification();
Dynamic Option Set Values based on Another Field
function updateOptionSetValues(e) {
var formContext = e.getFormContext();
var industryField = formContext.getAttribute(“new_industry”);
var subIndustryField = formContext.getAttribute(“new_subindustry”);
var industryValue = industryField.getValue();
if (industryValue === 1 /* Manufacturing */) {
// Set option set values for Manufacturing sub-industries
subIndustryField.removeOption(2);
subIndustryField.removeOption(3);
subIndustryField.addOption({ value: 4, text: “Automobile Manufacturing” });
} else if (industryValue === 2 /* Healthcare */) {
// Set option set values for Healthcare sub-industries
subIndustryField.removeOption(4);
subIndustryField.addOption({ value: 2, text: “Pharmaceuticals” });
subIndustryField.addOption({ value: 3, text: “Medical Devices” });
}
}
formContext.getAttribute(“new_industry”).addOnChange(updateOptionSetValues);
For any Help or Queries Contact us on info@crmonce.com or +918096556344