MB26
ServiceNow Employee
ServiceNow Employee

I recently came across a situation where I wanted to open a reference field lookup with some custom filter. The built in drop down filter is nice, but takes a little time to filter what you want. In particular I, as many of you do, use a "Parent Incident" field on the Incident form. Part of the incident process I am trying to follow is to search open incidents with the same Configuration item maybe, or something else on your form, and select an incident to be populated in the "Parent Incident" field.

Mark Stanger gave me the suggestion to use a popupOpenStandard(url, 'lookup'); type function. I created something like the below, which initially worked great. It popped open a window and I could see the filtered results. However the issue I ran into, was when I clicked an incident in the list, it would not populate the "Parent Incident" field.



var url = 'incident_list.do?sysparm_target=incident.u_parent_incident&sysparm_query=active=true';
popupOpenStandard(url, "lookup");


So I dug a little more and found some interesting variations of this popupOpenStandard() function. Using this function in conjuction with the "reflistOpenURL" function, I was able to basically duplicate the Reference Lookup functionality with a custom filter. Here is how I did it...

I wanted to add an additional icon next to the Reference Lookup icon and make it so that when clicked would popup my custom filter window. So I created an onLoad Client Script. You will need to change "thefield" variable to be whatever reference field you want this to execute against. You will then need to modify the "opensearch()" function to contain the fields and the "url" (which is your filter). I added the "taskid" piece to exclude the task you are currently searching from. The "url" is built up just like a reference qualifier.



function onLoad() {
var thefield = 'u_parent_incident'; //Identify the reference field this should be attached to.
var thetable = g_form.getTableName(); //This gets whatever table you are on.
var lookupfield = 'lookup.'+ thetable + '.' + thefield; //Creates the lookup or reference field ID.
var pinclookup = $(lookupfield);

if (pinclookup){
var thetarget = thetable + '.' + thefield;

pinclookup.insert({
after:'<a id="u.searchparentincidents"><img width="16" height="16" border="0" src="images/icons/filter.gif" title="Search Parent Incidents"></a>'});

var userpop = $('u.searchparentincidents');
userpop.observe('click', opensearch);
}

function opensearch() {
//Here you create some variables to get the values of the fields you want in your custom filter.
var theci = g_form.getValue('cmdb_ci'); //Get Value of field to pass into query
var taskid = g_form.getUniqueValue(); //Get current sys_id to exclude it in the query
//This line builds up the URL filter. Build this just like a reference qualifier.
var url = '&sysparm_query=active=true^cmdb_ci=' + theci + '^sys_id!=' + taskid;

var refurl = reflistOpenUrl(thetarget, thetarget, thefield, thetable, 'null', 'false', '');
var refurlquery = refurl + url;
popupOpenStandard(refurlquery, 'lookup');
}

}


The field with icon would look like this. I thought the filter icon was a nice touch.
find_real_file.png

Once Clicked the filter window looks like this. Notice the custom filter at the top.
find_real_file.png

And then when you click a field it populates your "Parent Incident" field like so.
find_real_file.png

Enjoy.
<script></script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-22945975-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

6 Comments