Script help - pass values from script Include to List Collector variable

geet
Tera Guru

Hi All,

I have a script Include which is fetching some values in the array and I want to pass those values in a Reference qualifier.

Here is my script. In the script there is a scratchpad variable I declared and I want that scratchpad to store the "impactedResourceID" and display it in the List Collector field via Reference qualifier.

 

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

    fetchUserResource: function() {
        gs.info("inside fetchUserResource");

        try {
            var requestedFor = this.getParameter('sysparm_reqFor');

            var user = new GlideRecord('sys_user');
            user.addQuery('sys_id', requestedFor);
            user.query();
            if (user.next()) {


                var userLookUpField = gs.getProperty('x_sdm_access_mgmt.user_lookup_attribute');
                var lookUpValue = userLookUpField + ":" + user[userLookUpField];


                gs.debug("Inside Get User Resources: User Lookup value is : " + lookUpValue);

                var inputs = {};
                inputs['user_lookup'] = lookUpValue;
                inputs['requested_for'] = user;

                var result = sn_fd.FlowAPI.getRunner().action('x_sdm_access_mgmt.SDM__get_catalog_api').inForeground().withInputs(inputs).run();
                var outputs = result.getOutputs();
                var resourceObj = new global.JSON().decode(outputs['response']);


                gs.debug("Inside Get User Resources: ResponseBody: " + outputs['response']);
				
                var impactedResourcesID = [];
                impactedResourcesID = this.GetModifiedResources(lookUpValue, resourceObj);
                gs.debug("Inside GetModifiedResources: Modified Resources are: " + impactedResourcesID);
				
				
                
				//g_scratchpad.StrongDM_allowedResouceID = impactedResourcesID+""; // Need Help Here
				
				var strongDM_RestUtil = new x_sdm_access_mgmt.StrongDm_RestAPI_Util();
                var filter = strongDM_RestUtil.createResourceFilter(impactedResourcesID+"");
                
				strongDM_RestUtil.fetchTags("", "", "");
				strongDM_RestUtil.fetchResouces(filter, "", "");



            }
        } catch (ex) {
            var message = ex.getMessage();
            gs.error("Inside Get User Resources: Error " + message);
        }

    },

    /*_________________________________________________________________
   * Description: To Check if Resources metadata is modified or not
   * Parameters: Array of Resocuces
   * Returns: Modified Resources
   ________________________________________________________________*/
    GetModifiedResources: function(lookUpValue, resourceObj) {
        gs.info("inside GetModifiedResources");

        var impatcedResource = [];

        //Looping through resources
        for (var i = 0; i < resourceObj['data'].length; i++) {
            var ResourceID = resourceObj['data'][i]['id'];
            var HashID = resourceObj['data'][i]['hash'];
            var MaxDuration = resourceObj['data'][i]['maxDuration'];

            var resourceId = this.findModifiedResourcesID(ResourceID, HashID);
            if (resourceId)
                impatcedResource.push(resourceId);

        }
        gs.debug("Inside GetModifiedResources:Total Resources modified are - " + impatcedResource);

        return impatcedResource;
    },

    /*_________________________________________________________________
   * Description: To compare Hash ID with ServiceNow resource table
   * Parameters: ResourceID, HashID, impatcedResource (array)
   * Returns:
   ________________________________________________________________*/
    findModifiedResourcesID: function(ResourceID, HashID) {
        gs.info("inside findModifiedResourcesID");

        //Check if Hash ID is modified
        var grResources = new GlideRecord("x_sdm_access_mgmt_resources");
        grResources.addQuery("resource_id", ResourceID);
        grResources.query();
        if (grResources.next()) {
            if (grResources.hash != HashID) {
                gs.debug("Inside GetModifiedResources: Resource is modified: " + HashID);
                return ResourceID;

            }
            return "";
        } else {
            return ResourceID;
        }

    },


    type: 'StrongDM_Catalog_Util'
});

 

This is how the log just before the scratchpad is coming up:
geet_0-1698447417264.png

Please help.

8 REPLIES 8

-O-
Kilo Patron
Kilo Patron

Reference qualifier does not set values.
The sole purpose of a reference qualifier is to limit what a user can select.

A reference qualifier is an encoded query, so if a script is used to set the ref. qual, than that script must return an encoded query string.

So setting the reference qualifier of a list collector just limits what can be selected in the list collector, it is not setting any value.

If one wants to set the value of a list collector, it is done like in case of any other variable: g_form.setValue().

The only special thing is that the value is a list of comma separated sys_ids.

But no matter what you actually want to achieve, the data you are logging won't work.

Whether it is for encoded query or to set the value those should be sys_ids, but those don't look at all like sys_ids.

If my hunch is correct, property id of resourceObj items is what you need, no further lookup (call to function findModifiedResourcesID) is needed.

Also if a reference qualifier is needed, the returned value needs to end up as sys_idIN<list if sys_ids>.

Also you have a typo in impatcedResource.

Hi,

Thanks for your response. I think my statement is confusing.
I want to limit the List Collector view(values) on the basis of which user is selected.
Yes, the values returned in the log are not sys_ids, they are rather the unique ID's  in a custom tables.

geet_0-1698449537722.png

 

If I get the sys_id in the impactedResouces array, how to pass/set it in Refernce qualifier? 
I hope I am not confusing you this time.

 

-O-
Kilo Patron
Kilo Patron

To use a script for a reference qualifier, you need to enter:

 

javascript&colon; 'sys_idIN' + <a function call that returns an array of sys_ids>;

 

into field Reference qual on the definition form of the List Collector variable.

 

You can pass in variable values into the called function by using syntax:

 

current.variables.<variable name>

 

Something like:

 

javascript&colon; 'sys_idIN' + new ScriptInclude().method(current.variables.<variable name>);

 

 

But your Script Include is not suitable for such a call.

It is so built as to expect the parameters through the Ajax processor, e.g:

 

var requestedFor = this.getParameter('sysparm_reqFor');

 

So you would need to modify the Script Include to get its parameter in the function arguments:

 

var StrongDM_Catalog_Util = Class.create();

StrongDM_Catalog_Util.prototype = {

	fetchUserResource: function (requestedFor) {
		gs.info("inside fetchUserResource");

		var sys_ids = [];

		try {
			//var requestedFor = this.getParameter('sysparm_reqFor');
			var user = new GlideRecord('sys_user');

			...
		}
		catch (ex) {
			var message = ex.getMessage();
			gs.error("Inside Get User Resources: Error " + message);
		}

		return sys_ids;
	},

	...

	type: 'StrongDM_Catalog_Util',

};

 

And you need to return something - better be an array of sys_ids.

So that you could write something like this into the Reference qual field:

 

javascript&colon; 'sys_idIN' + new StrongDM_Catalog_Util().fetchUserResource(current.variables.<requested for variable name>);

 

-O-
Kilo Patron
Kilo Patron

Note that the stupid forum replaces semicolons with their entity notation (&colon;) - so change it back.

As described in docs article section Advanced reference qualifier.