Not able to set values to list collector from script include

Tanwir Singh11
Tera Expert

Hi All,

I am filtering out the values from list collector (list_of_devices) which is referencing alm_asset table and another reference is location. So, i want to show only those assets which are from that location.

I have created a client script and a script include. I am able to return the sys_id from script include to client script but not able to set them in list collector. Below is my code:

Script include:

 getSiteAssets: function() {
		var arr_name = [];
                var OtoO = this.getParameter('sysparm_type');

                var ast = new GlideRecord('alm_asset');
                ast.addQuery('location', OtoO);
                ast.query();
                while (ast.next()) {                    
					arr_name.push(ast.getUniqueValue());
				}                
                return arr_name.toString();
            },

 

Client script:

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

    var ga = new GlideAjax('SiteDependentAssets');
    ga.addParam('sysparm_name', 'getSiteAssets');
    ga.addParam('sysparm_type', newValue);
    ga.getXML(parseData);

    function parseData(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
		alert(answer);
        g_form.setValue('list_of_devices', answer);
    }
}

 

Please help.

1 ACCEPTED SOLUTION

inform business that this is not feasible.

Either tell them

1) you will filter the list collector based on location and let user select the location to right bucket

OR

2) you will set the right bucket with the available assets for location selected and let user remove if they wish to

If my response helped you please mark it correct to close the question so that it benefits future readers as well.

Regards
Ankur

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

View solution in original post

24 REPLIES 24

Hey,

i did as you suggested and still not working.

Can you please re-check the field "list_of_devices" definition and share the screen shot. This field should be point to table "alm_asset".


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Yes, please have a look

find_real_file.png

Corey16
Tera Contributor

Ok so your script include looks like it would return something like this

sys_idIN28973897239728988,289738972397289,289738972397289

Which would work great for a reference qualifier. This would limit the options to only the three records returned and that's all the user would be able to select from. 

 

The problem is it sounds like you are trying to set the values for a list collector. In which case you would leave out out the sys_idIN and just return the array.toString(). 

 

When you get back to the client script you dont need to loop ( specially since this is a string or array of chars at this point) but simply need to set the value. 

 

g_form.setValue('list_of_devices',answer);

 

In short just remove the sys_idIN in your script include for this. And dont split and loop in the client script. 

** Warning 

If this function is indeed used as a reference qualifier somewhere else copy it and paste it or may an optional parameter to exclude the sys_idIN to avoid breaking the current usage of that function. 

hey corey,

I removed the sys_idIN and still it's not working. Please have a look at below updated code.

 

Script Include: 

I am returning the display_name because Display value = true for that column.

getSiteAssets: function() {
		var arr_name = [];
                var OtoO = this.getParameter('sysparm_type');

                var ast = new GlideRecord('alm_asset');
                ast.addQuery('location', OtoO);
                ast.query();
                while (ast.next()) 
               {                    
		 arr_name.push(ast.display_name.toString());
		}                
                return arr_name.toString();
            },

 

Client script:

   var ga = new GlideAjax('SiteDependentAssets');
    ga.addParam('sysparm_name', 'getSiteAssets');
    ga.addParam('sysparm_type', newValue);
    ga.getXML(parseData);

    function parseData(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('list_of_devices', answer);
    }