The CreatorCon Call for Content is officially open! Get started here.

g_form.setValue for reference field doesn ´t work as setting manually

diegobermudez
Tera Expert

Hi everyone,

We have done a Client script similar to OOTB "(BP) Set Location to User" wich sets the location of a incident same as caller.location when the caller is changed. Our script is equally to this OOTB but it sets the company.

The script seems to work perfectly, it sets the correct value for the company.

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || (g_form.isLiveUpdating && g_form.isLiveUpdating()))

return;

if (newValue == '') {

g_form.setValue('company', '');

return;

}

if (!g_form.hasField('company'))

return;

var caller = g_form.getReference('caller_id', setCompany);

}

function setCompany(caller) {

if (caller){

g_form.setValue('company', caller.company, caller.company.name);

}

}

The problem is when we try to use the company value before saving the record. I explain it....

The field cmdb_ci on incident table is depending on company field.

When we set the company value using the script above in a New Record, the CI is not filtered by the company.

After saving the record, it works perfectly and the CI is filtered by company but it doesn ´t work before submit.

If after the script sets the value of company, we select company manually again, CI is filtered by company without needing to save/submit

So we are thinking that g_form.setValue() for a reference field is not the same that select the value of that field manually as we can see with our tests.

Is there any wayof do a set Value for a refefence field wich do the same of selecting it manually that can override this behavior?

Thanks and regards,

1 ACCEPTED SOLUTION

diegobermudez
Tera Expert

I discover this behavior only occurs if List v3 are activated. I opened a case in HI-Portal and support team recognized the issue but notify to me they are not going to provide a solution as List v3 are deprecated in London. 

View solution in original post

9 REPLIES 9

Hi Diego,



If you want to filter it dynamically when the value of company changes you'll need to use a script include. Something like below might work, you may need to change the field name depending on what it's called on your CI form. You will need to call the script include returnCIs and call it in an advanced reference qualifier like:



javascript: new returnCIs().returnCIs()



var returnCIs = Class.create();


returnCIs.prototype = {


initialize: function() {


},



returnCIs:function() {


var ci;


var comp = current.company;


if(comp != "") {


ci = "company=" + comp; //change this field name to what the company field is called on your ci form


}



return   ci;


},



type: 'returnCIs'


};


Hi,

Use below encodedQuery in reference qualifier of cmdb_ci.

javascript:'company='+your_company_field_name;

 

Modify your code as below

function onChange(control, oldValue, newValue, isLoading) {

if (isLoading || (g_form.isLiveUpdating && g_form.isLiveUpdating()))

return;

if (newValue == '') {

g_form.setValue('company', '');

return;

}



var caller = g_form.getReference('caller_id', setCompany);

}

function setCompany(caller) {

if (caller){

g_form.setValue('company', caller.company);

}

}

dansieders
Tera Expert

Diego,

Did you end up getting a solution for this?

I too am experience a similar scenario where I have a reference field set by a client script, then before the save, I am attempting to use the reference field to filter another reference field using a script include. 

I also found that setting the field manually seems to update the record (or the current variable), however using the setValue() function does not.

The script include I utilise references the current variable for the filter.

Cheers

Daniel

Muralidharan BS
Mega Sage

You have to get the return in sysid. Get reference will work only with sysid's. 

 

diegobermudez
Tera Expert

I discover this behavior only occurs if List v3 are activated. I opened a case in HI-Portal and support team recognized the issue but notify to me they are not going to provide a solution as List v3 are deprecated in London.