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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Waiting for a control to Load to allow for a field to be checked for tab visibility

(0) ShareShare
ReportReport
Posted on by

Hi all,

I am now attempting to have certain tabs, fields, and sections appear and disappear from the page based on a choice in a pick list field.  I've gotten it to work when tied directly to the picklist field as the handler.  However, when trying to tell the function to also work upon the loading of the form (onLoad handler), it errors out with an "undefined".  My guess is that the field hasn't fully finished loading when the script starts executing.

To accommodate the loading of the form's fields prior to running the script, I've tried putting in a timeout based on a while loop that SHOULD wait until the field is no longer null and then proceed with the rest of the script.  However, when doing this, the script hangs the browser.  Any thoughts?

function ideaPathVisibility()
{   
	//Wait until the Idea Analysis Path Choice field is populated
		 
	var ideaChoicePresent = Xrm.Page.ui.controls.get("new_ideaanalysispath")._control;


	while (ideaChoicePresent == null)
	{
		Xrm.Page.ui.setFormNotification("The path choice is showing as null.  Now Waiting.", "INFO", "PathNotify");
		
		//Wait a little bit to accommodate the onload use case
		setTimeout
		(
			function () {
				Xrm.Page.ui.clearFormNotification("PathNotify");
			}, 3000
		);
		
		var ideaChoicePresent = Xrm.Page.ui.controls.get("new_ideaanalysispath")._control;
		
	}
		
	//Obtain the Idea Analysis Path Choice
	var ideaChoice =  Xrm.Page.getAttribute("new_ideaanalysispath").getText();
		
//perform the rest of the setting tab visibility below here - this works.
}

Thank you very much for your help!

Best Regards,

Eric


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hi Eric,

    normally you are not required to use setTimeout function, because the OnLoad event occurs after the form has loaded :

    msdn.microsoft.com/.../gg334481.aspx

    Did you try to debug your function?

    Xrm Object provides several methods to interact with an OptionSet field, you can try the getSelectedOption object:

    var option = Xrm.Page.getAttribute("fieldname").getSelectedOption();

    var value = option != null ? option.text : "";

    Hope this helps! If so I'd appreciate if you would mark this as a Verified answer.

    Thanks,

    Youssef.

  • Eric Vasbinder Profile Picture
    on at

    I'm not a JavaScript expert.  My training ends at AppleSoft BASIC in elementary school.  As such, I am confused by the second line:

         var value = option != null ? option.text : "";

    What is that line doing?

  • Eric Vasbinder Profile Picture
    on at

    One thing that might help is that if the second line were written out in multiple lines, instead of combined into one.  It looks very dense.

  • Verified answer
    Community Member Profile Picture
    on at

    it's true it's easier in multiple lines

    var value = "";

    var option = Xrm.Page.getAttribute("fieldname").getSelectedOption();

    if(option != null){

     value = option.text;

    }

  • Eric Vasbinder Profile Picture
    on at

    Hi there.  Unfortunately, I'm still getting the Undefined error when this script is attached to the page onLoad.  

    "There was an error with this field's customized event.

    Field:window

    Event:onload

    Error:undefined"

    If I remove the onLoad handler, it works great.

  • Eric Vasbinder Profile Picture
    on at

    Now I figured out the error.  The problem was in the subsequent If condition.  Basically, Dynamics, in it's infinite wisdom decides to take forever to load subgrids.  As such, one cannot hide a subgrid on page load, which is what I was attempting.

    I worked around the issue by putting the subgrid into a unique section along with a few other fields that I needed to hide in the same situation anyway.  Then changed the JavaScript to hide the entire section, not just the subgrid.  BOOM - problem went away.

    Thank you for your help!

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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans