Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Getting OptionSet value and setting Label Name based on Value

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
    Guido Preite 54,063 Moderator on at
    RE: Getting OptionSet value and setting Label Name based on Value

    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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,554 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,588 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans