Set value of a condition field

Kostya
Tera Guru

Hi,

i need help with setting a condition field (you know: the filter condition field that depends on a collection field).
I am implementing new application where I have a new ticket type and have a kind of template with a condition field (name: u_condition; type: condition: depends on: u_collection).

I am able to read the value of that filed by default g_form.getValue('u_condition').
But it is not possible to set the value of the other condition field by g_form.setValue('u_condition', '=', condition)

My script is:

function setConditionFromConditionTample() {
   var template = g_form.getReference('u_filter_template');

   if(template !== null) {
       if(g_form.getValue('u_table') == template.u_collection) {
           alert(template.u_condition);                                                                                                     // alert: active=true^EQ
           g_form.setValue('u_condition_string', template.u_condition); // nothing happen here
           return true;
       } else {
           alert('Selected template has to match with filter table. Select another template or contact your administrator, please.');
       }
   } else {
       alert('Filter template could not be loaded. Please contact your administrator.');
   }

   return false;
}


***********

the variable
template.u_condition
does have the value like
active=true^EQ
. But the condition field doest updated.

Any expirience with that?

Kind regards
Konstantin
5 REPLIES 5

Kostya
Tera Guru

For information,

the server side function setValue is working fine for condition field. F.e.


var gr = new GlideRecord('u_cn_customer_notification');

if(gr.get('04ebfddaec9b7440c34881651fb5c145')) {
gr.setValue('u_condition', 'active=false');
gr.update();
}


is working as expected. So i think it is a problem with g_form API


lajakkal
Mega Contributor

KonstantinThank you! Thank you! Thank you! This has helped me in another scenario that I was trying very hard to make it work!!


anubhavritolia
Giga Sage

Hi @Kostya 

 

g_form.getReference() function takes 2 parameters. Refer below link for that and search for it:

https://docs.servicenow.com/bundle/tokyo-application-development/page/app-store/dev_portal/API_refer...

 

Please find updated code below:

 

function setConditionFromConditionTample() { 
var template = g_form.getReference('u_filter_template', filterTemplate);
}

function filterTemplate(template) {
   if(template !== null) {
       if(g_form.getValue('u_table') == template.u_collection) {
          alert(template.u_condition);      // alert: active=true^EQ
          g_form.setValue('u_condition_string', template.u_condition);
           //return true; 
       } else {
           alert('Selected template has to match with filter table. Select another template or contact your administrator, please.');
       }
   } else {
       alert('Filter template could not be loaded. Please contact your administrator.');
   }
  // return false;
}

 

Commented return for now but if required based on your requirement, you can use it on your code and try.

 

 

Hello,

I have the same problem but I get an error that the sys ID is not valid in the condition.

What could be me wrong with this code? I want to set a condition that filters the Fix Available is true.

(function(outputs, steps, stepResult, assertEqual) {
  var gr = new GlideRecord("sn_vul_action_select_vi");
	if (gr.get(steps('27a02cb7973c7590481abce3f153qwe1').first_record) ){
		gr.setValue('condition', 'fix_available=true');
		gr.update();
	}
})(outputs, steps, stepResult, assertEqual);