Select the Devices into list collector based on Requested For field.

Rajesh Bandila
Tera Contributor

Hi Everyone,

 

Requiremnt: I have created the catalog item. Once the "Requested For" is selected, the "Assigned Asset" of this "Requested For" will be auto-populated in the "Test" field.

 

I have written the Below client callable script include and On-change Catalog Client script. However, it is not working as expected. Could you please help me on this?

 

Script Include:

var GetAssetDetails = Class.create();
GetAssetDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getAssets: function() {
        var requestedFor = this.getParameter('sysparm_requestedFor');
        var assetList = [];
        var gr = new GlideRecord('alm_asset');
        gr.addQuery('assigned_to', requestedFor);
        gr.query();
        while (gr.next()) {
            assetList.push(gr.sys_id.toString());
        }
        return new JSON().encode(assetList);
    },
    type: 'GetAssetDetails'
});
 
Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
var ga = new GlideAjax('GetAssetDetails');
    ga.addParam('sysparm_name', 'getAssets');
    ga.addParam('sysparm_requestedFor', g_form.getValue('requested_for'));
    ga.getXMLAnswer(function(response) {
        if (response) {
            var assetSysIds = JSON.parse(response);
            if (assetSysIds.length > 0) {
           g_form.setValue('test', assetSysIds.join(','));
            } else {
           g_form.setValue('test', '');
            }
        } else {
            alert('No response from server');
        }
    });
}
 
Thanks,
Rajesh


3 REPLIES 3

Jim Coyne
Kilo Patron

What kind of variable is "test"?

 

I'm assuming the Catalog Client Script is running on change of the "requested_for" variable.  Is that correct?

Hi @Jim Coyne , Thanks for your quick response 

 

“Test” field type is “list collector”.

 

yes, it’s running on change catalog client script for requested for field.

Oh, I see it in the title of the post now, sorry about that.  And sorry for the delay in responding.  Did you ever figure it out?

 

Here's an updated Script Include:

var U_GetAssetDetails = Class.create();
U_GetAssetDetails.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getListOfAssetSysIdsForUser: function() {          //more specific function namethat details the return value
        var user = this.getParameter("sysparm_user");  //more generic parameter name for use by other scripts
        var assetList = [];
		if (user) {
			var gr = new GlideRecord("alm_asset");
			gr.addQuery("assigned_to", user);
			//probably should add more filters here

			gr.query();
			while (gr.next()) {
				assetList.push(gr.getUniqueValue());
			}
		}

		//convert to JSON and return the data
        return JSON.stringify(assetList);  //use the new "stringify" method instead
    },

    type: 'U_GetAssetDetails'
});

 

It looks like you've created the Script Include in the Global scope.  If so, you need to be careful of the name so that ServiceNow does create a Class with the same name in a new release.  It is not a problem if it is in a custom Scoped App as it is yours and you control what is in it.  I don't like the "U_" prefix, but it protects your class somewhat, although not a guarantee.  You could use you client code as a prefix instead.

 

You might want to have one or a few Script Includes that contain your functions otherwise you'll end up with a lot of them.  Group the functions into Script Includes by what they do or tables they look at.

 

And I've changed the name of the function to make it clear as to what it will return.  And take a look at the comments I've added.

 

Updated Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	//remove this section if the Requested for Variable is auto-populated
	if (isLoading) {
        return;
    }

	//clear the Asset Variable when the Requested for field is changed
	g_form.setValue("test", "");

	//get the list of assets if a user was selected
	if (newValue) {
		g_form.showFieldMsg("test", "Loading list of assets...");  //let them know assets are being searched
		var ga = new GlideAjax("U_GetAssetDetails");
		ga.addParam("sysparm_name", "getListOfAssetSysIdsForUser");
		ga.addParam("sysparm_user", newValue);
		ga.getXMLAnswer(function(response) {
			//change the response into a comma-separated string
			//which the List Collector uses
			var result = JSON.parse(response).join(",");
			if (result) {
				g_form.setValue("test", result);  //will clear the field message automatically
			} else {
				g_form.hideFieldMsg("test", true);
			}
		});
	}
}

 

Again, take a look at the comments within the code.  This will work in a Service Portal, but not the regular UI:  needs a bit of tweaking the change the contents of the List Collector