2
votes

I'm looking a method or way how to check that the text field in crm form is "null"

I've got a tab, there are section and text field inside of it;

furthermore, I'm using that function in order to hide/show tab.

function setVisibleTabSection(tabname, TextFieldName, show) {
  var tab = Xrm.Page.ui.tabs.get(tabname);
  if (tab != null) {
    if (TextFieldName == null)
       tab.setVisible(show);
    else {
      var section =  Xrm.Page.data.entity.attributes.get(TextFieldName).getValue();
      if (section != null) {
        show == true; 
        tab.setVisible(show);
      }
    }
  }
} 

however, It doesn't work. There is nothing inside of the text box, and the tab expanded anyway.

by the way, parameters, which I give the function: "tab_8", "new_conf_report", false where the secon one the name of the text field

4
Comment on what this seems to be intended to do, for others to advise on best code to achieve that:AdamV

4 Answers

3
votes

Try

if (section != null && section !="")...

You may find that a field which is initially blank is null, whereas one from which you have deleted content but not yet saved the form is simply an empty string. Certainly worth a shot.

show==true

is incorrect as others have pointed out (needs to be show=true) but is simply redundant as written inside the same IF statement, just replace next line as:

tab.setVisible(true);

It is possible you intended "show" to be the default tab state to use if text field is not empty, in which case just move this line outside the IF instead of changing it (as shown below)

It looks like the construction using the third "show" parameter is to allow you to use the function to set the tab state to a specific state of shown or not without looking for a text field value at all. You would need to pass parameters as eg tabname,,true - you might consider swapping the TextFieldName and Show parameters so it is easier to just drop the third rather than remember to double-comma.

While we're fixing stuff, lets replace that variable "section" with something with a more meaningful name:

function setVisibleTabSection(tabname, show, TextFieldName) //usage: show is state Tab will have if no TextFieldName is specified, or if text field is empty
  {
      var tab = Xrm.Page.ui.tabs.get(tabname);
      if (tab != null) 
       {
        if (show==null){show=true;}
        if (TextFieldName == null)
          {
          tab.setVisible(show);
          }
        else
          {
          var strFieldValue = Xrm.Page.data.entity.attributes.get(TextFieldName).getValue();
          if (strFieldValue != null && strFieldValue !="") 
              {show=true;}
          tab.setVisible(show);
          }
       }
   } 
3
votes

I don't see anything wrong with your Javascript (besides what Guido points out, which basically will only set the tab to visible if you pass in true for show). Use the debugging tool within IE by pushing F12, and set a break point at the top of your function to see where your logic is failing.

If you've never debugged javascript before, see http://social.technet.microsoft.com/wiki/contents/articles/3256.how-to-debug-jscript-in-microsoft-dynamics-crm-2011.aspx

or

How to debug jScript for Dynamics CRM?

2
votes

I think there is a typo in the code:

show == true;

actually the code (assuming "=" instead of "==") will show always the tab if TextFieldName isn't empty, removing that line will show/hide the tab according to show parameter value

0
votes

It seems to work when I run it but I'm not sure what you'd expect it to do so it might not be working the way you'd like it to. :)

function setVisibleTabSection(tabName, textFieldName, show) {
  var tab = Xrm.Page.ui.tabs.get(tabName);
  if(!tab) return;

  if (!TextFieldName)
    tab.setVisible(show);
  else {
    var section = Xrm.Page.data.entity.attributes.get(textFieldName).getValue();
    if (section)
      tab.setVisible(true);
  }
}