If condition on reference field not not evaluating correctly

E_19
Giga Expert

Hi All,

I have 2 client scripts both are same as below 1 onload and the other on change.

The problem I have with the below script is that the section tabs are only briefly shown regardless if condition is met or not. I added the alert(gr. name); to check that the company name returned   matches my if condition but despite a match the tabs are only shown briefly and then hidden again.

Default for the section is show.

I think the problem lies in the if condition but I am unable to figure out what it could be.

Does anyone have any idea what is going on with my script and why its behaving this way?

var gr = new GlideRecord('core_company');

gr.addQuery('sys_id', g_form.getValue('company'));

gr.query(myCallbackFunction); //Execute the query with callback function

function myCallbackFunction(gr){

while(gr.next()){

alert(gr.name);

if(g_form.getValue('gr.name').indexOf("ABB") != -1 ){

g_form.setSectionDisplay('company', true);

}

else {

g_form.setSectionDisplay('company', false);

Many Thanks,

Ellie

PS: I have the 2 types of script at the moment as there are a lot of incidents with ABB company that don't display the tab but there will not be an onchange happening on these. Any better idea on this also welcome

1 ACCEPTED SOLUTION

veena_kvkk88
Mega Guru

Hi Ellie,



I'm not very good with the callback function kind of script. Could you explain what the requirement is and may be i can provide an alternate script? The sections should appear on the form if the company's name contains the phrase 'ABB'?



If so, may be try this script: (In the script part of a UI policy - this way you only need the script in one place and prevents writing the same script twice, onLoad and onChange)



UI Policy When to run:


Dot-walk to the Name of the Company. (If you select show related fields, it shows the related fields of all reference field) and choose 'Contains' and give your phrase.


1.png



2.png




3.png



...here's the script part:



Screen Shot 2017-04-04 at 1.49.46 PM.png


View solution in original post

17 REPLIES 17

Yay! UI Policies are our best friends


Hi Veena,



Would you also be able to help me on another problem on a UI Policy. I have added one to make fields mandatory, false if user has role Inside_Sales



if True condition


var isInside = g_user.hasRole('Inside_Sales');


  if (isInside){


    g_form.setMandatory('work_notes', false);


      g_form.setMandatory('comments', false);


    g_form.addInfoMessage('this is ' + isInside);



the strange results I get are it evaluates to true if user ha stat role or not but the fields are still mandatory, true.



Any thoughts appreciated.


Ellie


The scripts in the UI Policies are evaluated based on the condition we put in the 'When to Apply' section. The 'Execute if true' executes if the 'When to Apply' is satisfied. If it is empty, then the condition is always true, so the script in ''Execute if true' is always executed.



What you need is this:



'Execute if true' script:



var isInside = g_user.hasRole('Inside_Sales');


  if (isInside){


      g_form.setMandatory('work_notes', false);


      g_form.setMandatory('comments', false);


  else{


      g_form.setMandatory('work_notes', true);


      g_form.setMandatory('comments', true);


  }