You are on page 1of 4

/*

*-----------------------------------------------------------------------------------------------------------------

* BEGIN SCRIPT INCLUDE Script

*-----------------------------------------------------------------------------------------------------------------

*/

/*

* Script assembled by using examples and solution provided by:

* SNGuru - Advanced Reference Qualifier Using a Script Include

* https://www.servicenowguru.com/scripting/script-includes-scripting/advanced-reference-qualifier-
script-include/

* Script tested in Maxthon Cloud Browser v. 4.4.1.5000

* Script author : Jef De Coster for Devoteam Belgium

*/

/*

*Script specifics:

*getAdvancedQualifier will get and set the advanced qualification on any reference Field

*Set Advanced Qualification Filter on any Reference Field.

*Using 3 variables: "targetTable","filterField","filterValueField to pass thorugh,

*Will build Qualifier to use in advanced qualifier of a reference field to filter

*against filterField CONTAINS filterValueField

* Can be called as for example:


* javascript:new
dvt_ReferenceFieldFunctions().getAdvancedQualifier(targetTable,filterField,filterValueField)

* Get the advanced qualifier for referenced table cmdb_ci_hardware, using assigned_to filterfield
against caller_id value on current form:

* javascript:new
dvt_ReferenceFieldFunctions().getAdvancedQualifier("cmdb_ci_hardware","assigned_to","caller_id");

* In this example the advanced qualifier of a variable on Record Producer will be set only showing
the hardware for the caller.

*/

var dvt_ReferenceFieldFunctions = Class.create();

dvt_ReferenceFieldFunctions.prototype = {

initialize: function() {

},

// First real function that will get the Advanced Qualifier

getAdvancedQualifier : function(targetTable,filterField,filterValueField) {

//Variables

// answer will contain return value

var answer = '';

// includes is used to get the Value from the variables pool from variable

var includes = current[filterValueField];

//gs.addInfoMessage("includes value =["+includes+"]"); //used for debugging

// Check if Current.filterValueField isn't "undefined"

// This is the case if we are using this function from a Service Catalog Item

// (eg. Record Producer,...)

if (!includes) {

// In case the value is undefined we'll search for a variable


// This way this is usable on Service Catalog Item

includes = current.variables[filterValueField];

// GlRec will be used as the gliderecord on the table

var GlRec = new GlideRecord(targetTable);

// Set Query to initiate against the target Table

// Uses filterField to set against which field to query

// Uses includes to use as Value to query against

GlRec.addQuery(filterField,'CONTAINS',includes);

// Execute Query

GlRec.query();

// Loop through recordset an get the sys_id(s)

while (GlRec.next()) {

if (answer.length > 0) {

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

else {

answer = '' + GlRec.sys_id;

// return the result

return 'sys_idIN' + answer;

},

type: 'dvt_ReferenceFieldFunctions'
};

/*

*-----------------------------------------------------------------------------------------------------------------

* END SCRIPT INCLUDE Script

*-----------------------------------------------------------------------------------------------------------------

*/

You might also like