This article will explain, how to get the Choice Text and Value from the Dataverse environment on the Onload event using Javascript, and below is the scenario.
Scenario: Get the ContactMethod Choice Text and Value on the Onload of Account record.
Configured the On Load event on the Account Form and checked the execution context parameter.
Code
function onLoad(executionContext) {
let choiceTextAndVal = getChoiceTextAndValue(executionContext, "preferredcontactmethodcode");
console.log("Choice Text: " + choiceTextAndVal[0]);
console.log("Choice Value: " + choiceTextAndVal[1]);
}
function getChoiceTextAndValue(executionContext, choiceFieldSchemaName) {
let choiceText = null;
let choiceValue = null;
if (executionContext !== undefined && executionContext !== null) {
// Get the FormContext
let formContext = executionContext.getFormContext();
// Get the Option Set Attribute
let choiceFieldObj = formContext.getAttribute(choiceFieldSchemaName);
// Check for the Choice not null
if (choiceFieldObj !== null) {
// Get the Chioce Text
choiceText = choiceFieldObj.getText();
// Get the Choice Value
choiceValue = choiceFieldObj.getValue();
}
return [choiceText, choiceValue];
}
}
Save and Publish the JS Library and the Form.
Open any Account record and check for the output.
Output
Onload of Account record, JS will trigger and show the Output on the Console.
Press F12 to Open DevTools and Click on Console.
Hope you have used the code and got the Choice Text and Value.
Please like and share your valuable feedback on this article.