Pass in URL parameters into catalog item - Reference variables

M_iA
Kilo Sage

Hi,

I am trying to pass in 2 parameters from a URL into a catalog item. The variables are reference fields, so have a script and script include. However, I am unable to get the fields to populate.

 

Would someone be able to take a look at the 2 at confirm what I am doing wrong?

 

Variable 1: Customer. This is a reference field looking at customer_account

Variable 2: Configuration item. This is a reference field looking at cmdb_ci

 

V1 - The URL parameter will be a custom field: u_customer_prefix

V2 - The URL parameter will be the name field

 

URL im passing in: /nav_to.do?uri=%2Fcom.glideapp.servicecatalog_cat_item_view.do%3Fv%3D1%26sysparm_id%3D5f22a75e1bbc3300dc5aca217e4bcba1%26sysparm_link_parent%3D2e522b5e1bbc3300dc5aca217e4bcbcd%26sysparm_catalog%3De0d08b13c3330100c8b837659bba8fb4%26sysparm_catalog_view%3Dcatalog_default%26sysparm_view%3Dcatalog_default%26CUST%3DGCAA%26CI%3DGCAATEST

 

On load Client Script:

 

function onLoad() {

    // Based on received URL parameters, some specific form fields could be populated:
    // Param 'CUST' to populate the 'Customer' form field
    // Param 'CI' to populate the 'Configuration Item' form field

    // The parameter value could be an encoded URLstring, using %3d instead of equal sign (=) and %3b instead of semicoon (;)
    // Example: PID=PID0102&PARAMS=DIR%3d18.1.0%3bAPPS%3dMV%2cDS%2cWV%2cMVB%2cDB
    // Attention: The parameters must be added to the URL as encode3d entities, so instead of & use %3d

    var validURLParams = ['CUST', 'CI'];
    var parameterSeparator = "&";

    var variables = [];

    var URLParams = '';

    // This loop is looking for the expected parameters (validURLParams)
    for (i = 0; i < validURLParams.length; i++) {
        URLParams = getParmVal(validURLParams[i]);
        if (URLParams.length) {
            variables.push(validURLParams[i] + '=' + URLParams); // Add the variable found in the URL Params to allow a easy varible sanitization
        }
    }

    // Find the variables and set the values into the respective form field
    if (variables.length) {

        var variableName, variableValue; // Container for the parametere data in the loop.

        for (var j = 0; j < variables.length; j++) {

            var equalSymbolPosition = variables[j].indexOf("=");

            variableName = variables[j].substring(0, equalSymbolPosition); // Parameter name
            variableValue = variables[j].substring(equalSymbolPosition + 1); // Parameter value

            // If it is a valid URL param (one of the expected) populate the form fields
            if (validURLParams.indexOf(variableName.toUpperCase()) != -1) {

                switch (variableName.toUpperCase()) {
                                        case 'CUST':
                        // As customer is a reference field, the value to set must be a sys_id
                        if (variableValue.indexOf('CUST') == 0) {

                            //update the customer field
                            var regex = /\w+$/;
                            var testCust = variableValue.match(regex);

                            var custName = new GlideAjax('hmGetPIDDescription'); //call the Script Include
                            custName.addParam('sysparm_name', 'getCust'); //call the function getCust in the script include
                            custName.addParam('sysparm_custPrefix', testCust); //the parameter to pass to Script include
                            custName.getXMLAnswer(populateCUSTName); // The function populateCUSTName is called over here
                        }
                        break;

                    case 'CI':
                        // As ci is a reference field, the value to set must be a sys_id
                        if (variableValue.indexOf('CI') == 0) {

                            //update the CI field
                            var regex = /\w+$/;
                            var testCI = variableValue.match(regex);

                            var ciName = new GlideAjax('hmGetPIDDescription'); //call the Script Include
                            ciName.addParam('sysparm_name', 'getCI'); //call the function getInfo in the script include
                            ciName.addParam('sysparm_ciName', testCI); //the parameter to pass to Script include
                            ciName.getXMLAnswer(populateCINAME); // The function populateCINAME is called over here

                        }
                        break;
                }
            }
        }
    }

}

//populate the process information based on the PARAMS selected
function populateCUSTName(answer) {
	var CUSTname = answer.evalJSON();
    g_form.setValue('customer', CUSTname[0]);
}

function populateCINAME(answer) {
	var CIname = answer.evalJSON();
    g_form.setValue('ci', CIname[0]);
}

/**
 * Look for a parameter in the current windows' URL to return its value if found and set
 *
 * @Param {String} name The parameter name to search
 * @returns {String} The parameter value in the URL, or an empty string if it was not found.
 */
function getParmVal(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS, 'i');
    var results = regex.exec(window.location.href);

    if (results == null) {
        return "";
    } else {
        return unescape(results[1]);
    }
}

 

 

Script Include:

 

var hmGetPIDDescription = Class.create();
hmGetPIDDescription.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
		getCI: function(){
		var processInfo = [];
		var CI = this.getParameter('sysparm_ciName'); //get the ci name passed from the client script
		var getCI = new GlideRecord('cmdb_ci');
		getCI.addQuery('name', CI);
		getCI.query();
		if(getCI.next()){ //if CI is found	
			processInfo.push(getCI.sys_id.toString());		
		}
		gs.info('test -> CI info is '+processInfo);
		//return processInfo;
		return JSON.stringify(processInfo);
	},
	
		getCust: function(){
		var processInfo = [];
		var CUST = this.getParameter('sysparm_custPrefix'); //get the CUST prefix passed from the client script
		var getCUST = new GlideRecord('customer_account');
		getCUST.addQuery('u_customer_prefix', CUST);
		getCUST.query();
		if(getCUST.next()){ //if CUST is found		
			processInfo.push(getCUST.sys_id.toString());		
		}
		gs.info('test -> Customer info is '+processInfo);
		//return processInfo;
		return JSON.stringify(processInfo);
	},

    type: 'hmGetPIDDescription'
});

 

 

Many thanks for any help

2 REPLIES 2

Ankur Bawiskar
Tera Patron

@M_iA 

why are you doing so much validation in onLoad client script instead of just fetching the url parameter?

how are you redirecting to the catalog item?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

I was reusing a script / script include from an existing process that was built by a contractor!

 

The catalog item in question is used in conjunction with a blue prism integration. So the parameters passed into the URL arent the sys_ids. They are fields that are on the respective tables so needed to lookup the sys_ids