Filtering a reference field list using g_filter does not seem to work

MPD
Giga Contributor
I am attempting to filer a reference field's list using g_filter but it does not seem to work in my system nor can I find any documentation.   I am attempting this using a developer instance (Fuji)

function onLoad() {

//Apply a default filter to the list

var collectorName = 'mobile_device'; // Should this be the referenced field name ?

var filterString = 'u_mobile_devices^install_status!=Assigned'; //reference field table name ^ fieldname operator value ??

//Hide the list collector until we've set the filter

g_form.setDisplay(collectorName, false);

setCollectorFilter();

function setCollectorFilter(){

//Test if the g_filter property is defined on our list collector.

//If it hasn't rendered yet, wait 100ms and try again.

if(typeof(window[collectorName + 'g_filter']) == 'undefined'){   //I get an error here regarding g_filter

setTimeout(setCollectorFilter, 100);

return;

}

//Find the filter elements

var fil = gel('ep');

//Hide the filter elements by un-commenting the following lines

fil.rows[0].style.display = 'none';

fil.rows[1].style.display = 'none';

fil.nextSibling.rows[0].style.display = 'none';

// Reset the filter query

window[collectorName + 'g_filter'].reset();

window[collectorName + 'g_filter'].setQuery(filterString);

window[collectorName + 'acRequest'](null);

//Redisplay the list collector variable

g_form.setDisplay(collectorName, true);

}

}



Message was edited by: Madhuri Gudiseva

7 REPLIES 7

mike_allgire
Giga Guru

var collectorName = 'mobile_device'; // Should this be the referenced field name ?



Yes. That should be the variable name



var filterString = 'u_mobile_devices^install_status!=Assigned'; //reference field table name ^ fieldname operator value ??



This should only be the filter string that will be set on the list collector. From your example, it would be: install_status!=Assigned. Go to the source table and filter a list for what you want to see, then you can just copy the filter by right clicking the breadcrumbs and paste it in the string here.



if(typeof(window[collectorName + 'g_filter']) == 'undefined'){   //I get an error here regarding g_filter


Can you provide the error?



Make sure you follow this accurately. I have used this many times.


Changing the Filter of a List Collector Variable via Client Script - ServiceNow Guru


MPD
Giga Contributor

Thanks Mike,





I did find an error in my filter as shown below but it still does not work. The Mobile Device reference field just disappears and never reappears. That table is extended from Config Item and only has three records, one of which the value is Assigned, (value 9 in the choice list). If I comment out the setDisplay methods and leave the script to execute onLoad it's as if nothing executed, I see all three fields in the mobile device list.





I attempted to change the script to onChange so that the code would only run after I populated a specific field and that returned the error below








I did determine that nothing executes after var fil = gel('ep'). I simply moved the set display true above that line and the field appeared. If I place it below that line it does not see below **






Thanks for taking the time ... really appreciate your feedback.










function onLoad() {



//Apply a filter to the list collector variable



var collectorName = 'mobile_device';



var filterString = 'install_status!=9'; // Choice list correction value should be 9 not Assigned





//Hide the list collector until we've set the filter



g_form.setDisplay(collectorName, false);



setCollectorFilter();





function setCollectorFilter(){



//Test if the g_filter property is defined on our list collector.



//If it hasn't rendered yet, wait 100ms and try again.



if(typeof(window[collectorName + 'g_filter']) == 'undefined'){



setTimeout(setCollectorFilter, 100);



return;



}



//Find the filter elements



var fil = gel('ep'); //g_form.setDisplay(collectorName, true); above this line the field is visible, below this line the field is not displayed **



//Hide the filter elements by un-commenting the following lines



fil.rows[0].style.display = 'none';



fil.rows[1].style.display = 'none';



fil.nextSibling.rows[0].style.display = 'none'; //Filter description text





//Reset the filter query



window[collectorName + 'g_filter'].reset();



window[collectorName + 'g_filter'].setQuery(filterString);



window[collectorName + 'acRequest'](null);





//Redisplay the list collector variable



g_form.setDisplay(collectorName, true);



}



}


MPD
Giga Contributor

I did find an error in my filter as shown below but it still does not work.   The Mobile Device reference field just disappears and never reappears.     That table is extended from Config Item and only has three records, one of which the value is Assigned, (value 9 in the choice list).   If I comment out the setDisplay methods and leave the script to execute onLoad it's as if nothing executed, I see all three fields in the mobile device list.



I attempted to change the script to onChange so that the code would only run after I populated a specific field and that returned the error below


find_real_file.png


I did determine that nothing executes after var fil = gel('ep').   I simply moved the set display true above that line and the field appeared.   If I place it below that line it does not see below **



Thanks for taking the time ... really appreciate your feedback.





function onLoad() {


//Apply a filter to the list collector variable


var collectorName = 'mobile_device';


var filterString = 'install_status!=9'; // Choice list correction value should be 9 not Assigned



//Hide the list collector until we've set the filter


g_form.setDisplay(collectorName, false);


setCollectorFilter();



function setCollectorFilter(){


  //Test if the g_filter property is defined on our list collector.


  //If it hasn't rendered yet, wait 100ms and try again.


  if(typeof(window[collectorName + 'g_filter']) == 'undefined'){


    setTimeout(setCollectorFilter, 100);


    return;


  }


  //Find the filter elements


  var fil = gel('ep');   //g_form.setDisplay(collectorName, true); above this line the field is visible, below this line the field is not displayed **


  //Hide the filter elements by un-commenting the following lines


  fil.rows[0].style.display = 'none';


  fil.rows[1].style.display = 'none';


  fil.nextSibling.rows[0].style.display = 'none'; //Filter description text


 


  //Reset the filter query


  window[collectorName + 'g_filter'].reset();


  window[collectorName + 'g_filter'].setQuery(filterString);


  window[collectorName + 'acRequest'](null);


 


  //Redisplay the list collector variable


  g_form.setDisplay(collectorName, true);


}


}


I think I may have found your issue; comment out the following lines (or remove them) as you can achieve this using the no_filter attribute to the variable now. If I keep these lines in the code, the list collector does not show up.


  //Find the filter elements


  var fil = gel('ep');   //g_form.setDisplay(collectorName, true); above this line the field is visible, below this line the field is not displayed **



  //Hide the filter elements by un-commenting the following lines


  fil.rows[0].style.display = 'none';


  fil.rows[1].style.display = 'none';


  fil.nextSibling.rows[0].style.display = 'none'; //Filter description text