Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Can I limit a reference field with a script?

ServiceNowSteve
Giga Guru

We have a custom CI table that I want to reference on a form. What I would like to do is limit the choices to only CI items that are not currently related to another asset.

My fuzzy logic is 

  1. Check the relationship table
  2. If the current CI is listed in that able as a child exclude it from the search results
  3. If not then show it.

Can this be done since I am checking a different table than the one I am referencing or is there another way to go about it?

1 ACCEPTED SOLUTION

Hi Steve,

Your script include should return the sys_ids of the values that need to be shown. So you write a glide record and query and fetch the ones which need to be shown and return that as comma separated.

Here is the sample code of script include which can be used as a reference. This script include accepts a user parameter and return the groups sys_ids on this parameter

var GetCurrentUserGroups = Class.create();
GetCurrentUserGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize:function() {
},
BackfillAssignmentGroup:function(user) {
	gs.info("In the log");
var gp = [];
var a = user; // capture the sysID passed to this function
//sys_user_grmember has the user to group relationship
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user',a);
grp.query();
while(grp.next()) {
//build a comma separated string of groups if there is more than one
gp.push(grp.getValue('group'));
}
gs.log(gp);
// return Groups where assigned to is in those groups we use IN for lists
return 'sys_idIN' + gp;
}, 
type: 'GetCurrentUserGroups'
});

 

You can call the script include the form the reference qualifier like below

javascript:new GetCurrentUserGroups().BackfillAssignmentGroup(current.user);

 

 

So your script include need to be updated to accept the parameter like below and call the script include as shown above.

findCmdbRelCpu:function(ci) 

Mark the comment as a correct answer and also helpful if it helps.

View solution in original post

9 REPLIES 9

Hi Steve,

Your script include should return the sys_ids of the values that need to be shown. So you write a glide record and query and fetch the ones which need to be shown and return that as comma separated.

Here is the sample code of script include which can be used as a reference. This script include accepts a user parameter and return the groups sys_ids on this parameter

var GetCurrentUserGroups = Class.create();
GetCurrentUserGroups.prototype = Object.extendsObject(AbstractAjaxProcessor, {
initialize:function() {
},
BackfillAssignmentGroup:function(user) {
	gs.info("In the log");
var gp = [];
var a = user; // capture the sysID passed to this function
//sys_user_grmember has the user to group relationship
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user',a);
grp.query();
while(grp.next()) {
//build a comma separated string of groups if there is more than one
gp.push(grp.getValue('group'));
}
gs.log(gp);
// return Groups where assigned to is in those groups we use IN for lists
return 'sys_idIN' + gp;
}, 
type: 'GetCurrentUserGroups'
});

 

You can call the script include the form the reference qualifier like below

javascript:new GetCurrentUserGroups().BackfillAssignmentGroup(current.user);

 

 

So your script include need to be updated to accept the parameter like below and call the script include as shown above.

findCmdbRelCpu:function(ci) 

Mark the comment as a correct answer and also helpful if it helps.

haha it's funny that you posted this because I found this exact script include and was trying on my own last night to do this and I still couldn't get it to work.

I will try again and see if I can do it today.

It took some work on my end but I got it to pull the results correctly. Thanks for the help.

morrix7
Kilo Contributor

This helps a lot.

In my case, I was actually looking for groups to where the user didn't belong to, so just in case anybody ever needs it, the only change I did was:
From: 

return 'sys_idIN' + gp;

To : return 'sys_idNOT IN' + gp;

i just had to do something similar.

1) write a script include to grab the data you need

2) you return a string of the format  sysidIN + xxxxx

where xxx is a comma separated string of the sysids you want

3) configure the dictionary for the field with the drop down.

    select advanced

    on the record specification tab referrence qualifier is set to Advanced

    the reference qualifier is:  javascript:  your method

          for example:  javascript:  u_get_my_data()

let me know if you require further assistance.