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

nthumma
Giga Guru

How are you setting company field value on the form? are you setting using any on load script?


Hi srnwebie,



This is on incident form and the company field is a reference field from core_company table. Either SD add company via lookup or if caller is added first company is auto populated.



Dictionary Info: incident.companyClose
<rendered_body>
Table

task


Field

company


Type

reference


Reference

core_company



Thanks for helping


Ellie


tobrien
Kilo Guru

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


g_form.setSectionDisplay('company', true);


}


else {


g_form.setSectionDisplay('company', false);




This appears incorrect to me ...



Using single quotes around the gr.name will force it to NOT be evaluated... and double quotes inside javascript is always dicey ...



I'd suggest trying:



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


    g_form.setSectionDisplay('company', true);


}


else {


    g_form.setSectionDisplay('company', false);


Hi Tony,



I have made the recommended changes but the behaviour is still the same the section tab briefly appears for all companies and then disappears again.



Any other idea on this?



Many Thanks,


Ellie