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

Hi,

I am little confused with your reply as I am not fetching the users rather trying to fetch the resources that I have stored in a custom table on the basis of the user being selected in the "Request for" field on the catalog item form:
Example: In the below catalog item, if Abel is selected, he will see only those resources in the slushbucket which he is authorized to and that I am fetching from the script include I wrote.

geet_0-1698455268777.png

 

As you suggested, I tried to fetch the sys_id in the impactedResourceID and now trying to understand how to pass them into Reference Qualifier condition:

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); // This log provides the sys_ids now.
				
			//	gs.getSession().putClientData('impactedResourcesID', JSON.stringify(impactedResourcesID));
                
				//g_scratchpad.StrongDM_allowedResouceID = impactedResourcesID+"";
				
				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 + " resources " + grResources.sys_id);
				
                return grResources.sys_id; // Fetching the sys_id here.

            }
            return "";
        } else {
            return grResources.sys_id;
        }

    },


    type: 'StrongDM_Catalog_Util'
});

geet_1-1698455504567.png

Please suggest something.

-O-
Kilo Patron
Kilo Patron
I am little confused with your reply as I am not fetching the users rather trying to fetch the resources that I have stored in a custom table on the basis of the user being selected in the "Request for"

So if you don't pass in that information, how will "the script know" the resources of which user to look up?

 

*) Emphases mine

-O-
Kilo Patron
Kilo Patron

Also I don't think you got the main message of my reply: do learn the difference between filtering the available items to be selected (via reference qualifier) vs. setting the available items to be selected through GlideAjax calls - which you seem to be doing here.

-O-
Kilo Patron
Kilo Patron

Another problem is that you are not providing much context. E.g. your Script Include neither returns a value nor sets the Ajax answer payload. The full solution consist (normally) of a whole lot of client side stuff that you are not detailing. So my answers are in fact shots in the dark based on guesses I took based on the bits of pieces you have published. E.g. a very important information would be the configuration of the "Select resource to request access" variable. Another very important information is what triggers on client side the call to server side (the Script Include) and how does that piece of whatever look? Another very important piece of information is whether the solution must support both CMS and Portal, or just CMS? A.s.o.

Also do note that "resource" has specific meaning just to you. Form mea it says absolutely nothing. It might be a server, or a location, a user. When considering how the functionality of a List Collector should be considered, that is impossibly generic.