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.

Populate MRVS onChange

Dash2
ServiceNow Employee
ServiceNow Employee

Hello community,

 

I have a request to auto populate an MRVS with a list of asset records based on the selected location.

 

Below is the script include and the catalog client script I'm having issues with:

Variables:

  • name: a reference variable (alm_asset)
  • serial_number: a single-line text

    Script Include:
var MyAssetScriptInclude = Class.create();
MyAssetScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getAssetsForLocation: function(locationID) {
        var assets = [];
        var grAsset = new GlideRecord('alm_asset');
        grAsset.addQuery('location', locationID);
        grAsset.query();
        while (grAsset.next()) {
            var asset = {
                'name': grAsset.getDisplayValue.toString(),
                'serial_number': grAsset.serial_number.toString(),
                
            };
            assets.push(asset);
        }
        return assets;
    },

    type: 'MyAssetScriptInclude'
});

 

And my onChange catalog client script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var ga = new GlideAjax('MyAssetScriptInclude');
    ga.addParam('sysparm_name', 'getAssetsForLocation');
    ga.addParam('sysparm_location_id', newValue);
    ga.getXMLAnswer(populateMRVS);
}

function populateMRVS(response) {
    var answer = response;
    var mrvs = g_form.getControl('mdf_set');

    try {
        var assets = JSON.parse(answer);

        // Clear existing rows
        g_form.clearOptions('mdf_set');

        // Populate MRVS with assets
        for (var i = 0; i < assets.length; i++) {
            var asset = assets[i];
            g_form.addRow('mdf_set');
            g_form.setValue('mdf_set', 'name', asset.name, i);
            g_form.setValue('mdf_set', 'serial_number', asset.serial_number, i);
          
        }
    } catch (e) {
        // Handle error
        console.error('Error populating MRVS:', e);
    }
}

  I appreciate your assistance on this.

 

@Ankur Bawiskar 

@Brad Bowman  

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Dash2 

please make these changes

var MyAssetScriptInclude = Class.create();
MyAssetScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getAssetsForLocation: function() {
		var locationID = this.getParameter('sysparm_location_id');
		var assets = [];
		var grAsset = new GlideRecord('alm_asset');
		grAsset.addQuery('location', locationID);
		grAsset.query();
		while (grAsset.next()) {
			var asset = {
				'name': grAsset.getUniqueValue(), 
				'serial_number': grAsset.serial_number.toString()
			};
			assets.push(asset);
		}
		return JSON.stringify(assets);
	},

	type: 'MyAssetScriptInclude'
});

Client script

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue === '') {
		return;
	}

	var loc = g_form.getValue('room');

	if(oldValue != newValue){
		var ga = new GlideAjax('MyAssetScriptInclude');
		ga.addParam('sysparm_name', 'getAssetsForLocation');
		ga.addParam('sysparm_location_id', loc);
		ga.getXMLAnswer(populateMRVS);
	}
}

function populateMRVS(response) {
	try{
		var answer = response;
		g_form.setValue('mdf_set', answer);
	} catch (e) {
		// Handle error
		console.error('Error populating MRVS:', e);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

9 REPLIES 9

Harish KM
Kilo Patron
Kilo Patron

Hi @Dash2 in client callable script include we access the below client script value by using

ga.addParam('sysparm_location_id

var location =this.getParameter('sysparm_location_id); inside function in script include but you have defined as parameter,  please remove that and place the below line inside function 

var locationID =this.getParameter('sysparm_location_id); and in location table your location id will match as per the query.

 

Regards
Harish

Dash2
ServiceNow Employee
ServiceNow Employee

So like this?

 

var MyAssetScriptInclude = Class.create();
MyAssetScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getAssetsForLocation: function() {
        var locationID = this.getParameter('sysparm_location_id');
        var assets = [];
        var grAsset = new GlideRecord('alm_asset');
        grAsset.addQuery('location', locationID);
        grAsset.query();
        while (grAsset.next()) {
            var asset = {
                'name': grAsset.getDisplayValue('name'), 
                'serial_number': grAsset.serial_number.toString()
            };
            assets.push(asset);
        }
        return assets;
    },

    type: 'MyAssetScriptInclude'
});

 

Hi @Dash2 yes and your using json object to store values so the code would look like this

getAssetsForLocation: function() {

    var locationID = this.getParameter('sysparm_location_id');

    var assets = [];

    var grAsset = new GlideRecord('alm_asset');

    grAsset.addQuery('location', locationID);

    grAsset.query();

    while (grAsset.next()) {

        var asset = {

            'name': grAsset.getValue('display_name'), 

            'serial_number': grAsset.getValue('serial_number')

        };

        assets.push(asset);

    }

gs.info("values are"+JSON.stringify(assets,null,4));// you can check in logs what output you get

    return JSON.stringify(assets);

},

 

Regards
Harish

Brad Bowman
Kilo Patron
Kilo Patron

locationID will be null/unknown/empty in this case.  Since the SI is called from a Client Script you need to omit the function argument/parameter and add a line like this within the function:

 

var locationID = this.getParameter('sysparm_location_id');

Also, you probably want the Display name of the asset pushed into the object array, so use:

'name': grAsset.display_name.toString(),

When you are getting the expected back to the client - add info logs to the SI and alerts to the Client Script to confirm this, you may need to use setValue or clearValue with the MRVS name instead of clearOptions.  I've only used that for select box type variables.