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

Filter Reference Results based on another reference field

magoo
Kilo Expert

Hello all,

I am trying to filter one reference fields results based on another reference field.   I found another post that worked for someone else, but that doesnt seem to work for me. ( javascript:'u_account_name='+ current.u_account_name;)

I have two tables, u_national_account_account_nam and u_national_account_tags.


u_national_account_tags has a reference field that looks at u_national_account_account_nam 's field called u_account_name.


On my Catalog item I have a field called "naCustName" that references my table u_national_account_account_nam.   Later on in the request I have another field "naTagNumber" which references the 2nd table u_national_account_tags.   I need that field to only display the tags that are associated with the customer that is listed in the "naCustName" field.


If anyone has any good advice on what I need to do would be appreciated!

Steve

1 ACCEPTED SOLUTION

magoo
Kilo Expert

All,


I ended up finding a article on SN Guru called "Changing the filter on a list collector variable via client script"


My company decided to go with a list collector so they could pick multiple items. This worked, great, here is what I ended up with;



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


    //Apply a filter to the list collector variable


    var collectorName = 'naTagNumber';


    var filterString = 'name!=NULL^u_account_name='+g_form.getValue('naCustName');


 


    //Find the filter elements


    var fil = gel('ep');




    //Reset the filter query


    eval(collectorName + 'g_filter.reset()');


    eval(collectorName + 'g_filter.setQuery("' + filterString + '")');


    eval(collectorName + 'acRequest(null)');


}





View solution in original post

3 REPLIES 3

manikorada
ServiceNow Employee
ServiceNow Employee

You need to have a reference qualifier as : javascript:getMyQuery()




Now have a script include as:


Name : getMyQuery


Client Callable : True


Script :




function getMyQuery() {


  var answer = '';


  var includes = current.variables.naCustName;


  var usr = new GlideRecord('u_national_account_tags');


  usr.addQuery('<<ref_field>>',includes);


  usr.query();


  while (usr.next()) {


    if (answer.length > 0) {


  answer += (',' + usr.sys_id);


    }


    else {


  answer = '' + usr.sys_id;


    }


    }


  return 'sys_idIN' + answer;


  }


Thanks Mani for the reply.   I tried the script include as is and it didn't seem to work. I also thought maybe where you had listed <<ref_field>> that I should put in the reference field name (u_account_name) but that didn't seem to do it either.  



As a side question, if the business decides they want to have the ability to do a list Collector instead (currently deciding which route, but starting to lean this way) and select mult "Tag Numbers", is there a way to still do a reference qualifier? (I don't see any options).


magoo
Kilo Expert

All,


I ended up finding a article on SN Guru called "Changing the filter on a list collector variable via client script"


My company decided to go with a list collector so they could pick multiple items. This worked, great, here is what I ended up with;



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


    //Apply a filter to the list collector variable


    var collectorName = 'naTagNumber';


    var filterString = 'name!=NULL^u_account_name='+g_form.getValue('naCustName');


 


    //Find the filter elements


    var fil = gel('ep');




    //Reset the filter query


    eval(collectorName + 'g_filter.reset()');


    eval(collectorName + 'g_filter.setQuery("' + filterString + '")');


    eval(collectorName + 'acRequest(null)');


}