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

Notifications

Announcements

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 24 Super User 2025 Season 2

#2
TAHER Mehdi Profile Picture

TAHER Mehdi 14

#3
Fameeda Yaseen Profile Picture

Fameeda Yaseen 10

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans