web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Getting OptionSet value and setting Label Name based on Value

(0) ShareShare
ReportReport
Posted on by 90

Hi all, 

Could anyone help me understand why this code doesn't work? I'm trying to get the option set value of a field, and if it's "C", then set the label name to Celius. Here's my code:

function onload(executionContext) {
	debugger 
	if (executionContext == null)
		return;
	var formContext = executionContext.getFormContext();
	oppForm(executionContext);

};

function oppForm(executionContext) {
	debugger
	if (executionContext == null)
		return;
	var formContext = executionContext.getFormContext();
	var optionSetLabelName = formContext.getControl("dom_temperature");
	var optionSetChoice = formContext.getAttribute("dom_temperature").getText();
	console.log(optionSetChoice);
		
	if (optionSetChoice == null) {
		return;
	if (optionSetChoice == ("C")) {
		formContext.getControl("dom_temperature").setLabel("Celsius");
		}
		}
};

  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    main reason is because the if condition checking the "C" text is inside the if condition checking the null, so it is never executed.

    This is your code a bit rewritten, you can start with this and debug if you have additional errors

    function onload(executionContext) {
    	oppForm(executionContext);
    }
    
    function oppForm(executionContext) {
    	var formContext = executionContext.getFormContext();
    	var optionSetChoice = formContext.getAttribute("dom_temperature").getText();
    	if (optionSetChoice == "C") {
    		formContext.getControl("dom_temperature").setLabel("Celsius");
    	}
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Syed Aqib Raza Profile Picture

Syed Aqib Raza 96

#2
11manish Profile Picture

11manish 88 Super User 2026 Season 2

#3
ParthPatel249 Profile Picture

ParthPatel249 84

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans