sys_template filter field on variable

sigmachiuta
Kilo Guru

i am trying to restrict a variable to only show specific templates.   For example in the template i am making an incident template, where the category is 'Request'.   In a record producer variable I am trying to add a reference field to only show the templates where the category is 'request'.   but i am not able to add that filter in the ref qual that is in the template.   how can i restrict this variable to only show where the template 'category' is request?

find_real_file.png

find_real_file.png

1 ACCEPTED SOLUTION

LaurentChicoine
Tera Guru

You could add a custom field (checkbox) in the sys_template table to check template that are request, then you could use this in your reference qual.



Otherwise, the template field is not well built for condition and doesn't allow you to filter based on contains or something like that.



As for the query data for the template, here is an example of something I used:



var template = new GlideRecord("sys_template");


template.get(sys_id); //Replace sys_id with a variable containing your sys_id for the template


             


var fieldSets = template.template.split("^"); //First template refers to the GR and second one to the template field


   


for(e in fieldSets){


        if(fieldSets[e] != 'EQ'){ //Removes the EQ which is at the end of the template field value


                  var field = fieldSets[e].split("=")[0];


                  var value = fieldSets[e].split("=")[1];


                  //Then you can either store these field, value pair in an array or use them while you are in the loop


        }


}


View solution in original post

7 REPLIES 7

sigmachiuta
Kilo Guru

Also, what if i wanted to query the data in the template.   For example add in the query category = request?



var rec = new GlideRecord('sys_template');


rec.query();


while (rec._next()) {


  gs.print(rec.number + ' exists');


}


LaurentChicoine
Tera Guru

You could add a custom field (checkbox) in the sys_template table to check template that are request, then you could use this in your reference qual.



Otherwise, the template field is not well built for condition and doesn't allow you to filter based on contains or something like that.



As for the query data for the template, here is an example of something I used:



var template = new GlideRecord("sys_template");


template.get(sys_id); //Replace sys_id with a variable containing your sys_id for the template


             


var fieldSets = template.template.split("^"); //First template refers to the GR and second one to the template field


   


for(e in fieldSets){


        if(fieldSets[e] != 'EQ'){ //Removes the EQ which is at the end of the template field value


                  var field = fieldSets[e].split("=")[0];


                  var value = fieldSets[e].split("=")[1];


                  //Then you can either store these field, value pair in an array or use them while you are in the loop


        }


}


I am not quite following the code example.   In a Business Rule i would want to update the checkbox to true when the template has category = request.   There would be multiple possible fields/values not just the single one i am looking for.     So first wouldn't I be looking at current.template?



  1. var template = new GlideRecord("sys_template");  
  2. template.get(current.template); //Replace sys_id with a variable containing your sys_id for the template  

The code example was more to make use of the template you created via server side script which was what I taught you wanted to do when I saw your initial code:


  1. var rec = new GlideRecord('sys_template');  
  2. rec.query();  
  3. while (rec._next()) {  
  4.   gs.print(rec.number + ' exists');  
  5. }  


If you want to set the checkbox to true it would be a Insert or Update onBefore business rule on sys_template:



if(current.template.indexOf("category=request") > -1){ //Check if template contains the condition


        current.u_name_of_custom_field = true;


}


else{


        current.u_name_of_custom_field = false; //Set back to false otherwise as condition could have been removed


}