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 :
Service | Customer Service, Contact Center, Fie...
Answered

using variable in Java script to apply an attribute

(0) ShareShare
ReportReport
Posted on by 85

Hi all,

I'm looking for a better way to complete Hide/Show of sections based on a selection in a pick list.  Here is the code that I am currently working with.

The logic is, if "Blues_Topic" = 552190000 then show a section, if not hide the section. 

function hideOrShowDispSup() {
var AA = Xrm.Page.getAttribute("blues_topic").getValue();
if (AA == 552190000) {
Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_RiskBased_Part1").setVisible(true);
} else {
Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_RiskBased_Part1").setVisible(false);
}
}

I have a 6 choice options in the Blues_topic and 2 sections for each option.  I looking to name the section based on the selection. Rather than a suite of If, Then, statements, I would like to perhaps use the variable in this format

function hideOrShowDispSup() {
var AA = Xrm.Page.getAttribute("blues_topic").getValue();

Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_552190000_SecA").setVisible(false);
Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_552190000_SecB").setVisible(false);

Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_552190001_SecA").setVisible(false);
Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_552190001_SecB").setVisible(false);

Xrm.Page.ui.tabs.get("TopicDetail").sections.get(AA+"_SecA").setVisible(true);
Xrm.Page.ui.tabs.get("TopicDetail").sections.get(AA+"_SecB").setVisible(true);

}
}

The logic is variable AA is set to the choice value.

Set all sections not visible.

then 

set the sections AA_1 and AA_2 as visible

If the selection was 552190000

AA = 552190000

Section 552190000_SecA and 552190000_B would be visible.

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to use something like following:

    function hideOrShowDispSup() {
    	var AA = Xrm.Page.getAttribute("blues_topic").getValue();
    
    	var allAvailableOptions = [552190000, 552190001, 552190002, 552190003, 552190004, 552190005];
    
    	for(var i = 0; i < allAvailableOptions.length; i  ) {
    		var currentOption = allAvailableOptions[i];
    
    		Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_"   currentOption   "_SecA").setVisible(currentOption === AA);
    		Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_"   currentOption   "_SecB").setVisible(currentOption === AA);
    	}
    }

  • Mark TVC Profile Picture
    85 on at

    Thanks Andrew,

    Tried this and we have the following error

    Cannot read property 'setVisible' of null

    Mark

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Mark,

    Can you please provide names of all sections you want to manipulate visibility of and the list of options of your optionset.

  • Mark TVC Profile Picture
    85 on at

    552190000_SecA

    552190000_SecB

    552190001_SecA

    552190001_SecB

    552190002_SecA

    552190002_SecB

    552190003_SecA

    552190003_SecB

    552190004_SecA

    552190004_SecB

    552190005_SecA

    552190005_SecB

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Then replace lines

    Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_" + currentOption + "_SecA").setVisible(currentOption === AA);

    Xrm.Page.ui.tabs.get("TopicDetail").sections.get("Top_" + currentOption + "_SecB").setVisible(currentOption === AA);

    with lines

    Xrm.Page.ui.tabs.get("TopicDetail").sections.get(currentOption + "_SecA").setVisible(currentOption === AA);

    Xrm.Page.ui.tabs.get("TopicDetail").sections.get(currentOption + "_SecB").setVisible(currentOption === AA);

  • Mark TVC Profile Picture
    85 on at

    Thank you Andrew

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Service | Customer Service, Contact Center, Field Service, Guides

#1
NeerajPawar Profile Picture

NeerajPawar 31

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 19 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 16

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans