Display of list of location change upon updated of requested for

harshamovva
Tera Contributor

I have the below script include script which retreives a list of locations based on the Requested by user on a catalog form.

getrequestedbylocationlist: function(u_requested_by) {
        var obj = [];
        var usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', u_requested_by);
        usr.query();

        if(usr.next()){
            var loc = new GlideRecord('cmn_location');
            loc.addQuery('parent.sys_id', usr.location.parent.sys_id);
            loc.query();

            while(loc.next()){
                obj.push(loc.sys_id.toString());
            }
            obj.push('<defaultlocation1sys_id>');
             obj.push('<defaultlocation2sys_id>');
            obj.push('<defaultlocation3sys_id>');

        }
       
        return 'sys_idIN'+obj;
    },
And on the location Reference variable I am using the reference qualifier "javascript&colon; new CatalogtUtils().getrequestedbylocationlist(current.variables.u_requested_by)". Now I want to update the reference qualifier list to display the list of locations upon change of requested for another variable, so i updated the script include and reference qualifier as below but it doesnt seem to work, Any help on this would be appreciated.
getrequestedbyandforlocationlist: function(u_requested_by, requested_for) {
        var obj = [];
        if(u_requested_by){
            var usr = new GlideRecord('sys_user');
        usr.addQuery('sys_id', u_requested_by);
        usr.query();

        if(usr.next()){
            var loc = new GlideRecord('cmn_location');
            loc.addQuery('parent.sys_id', usr.location.parent.sys_id);
            loc.query();

            while(loc.next()){
                obj.push(loc.sys_id.toString());
            }
            obj.push('<defaultlocation1sys_id>');
             obj.push('<defaultlocation2sys_id>');
            obj.push('<defaultlocation3sys_id>');

        }
        }
        if(requested_for) {
        var nusr = new GlideRecord('sys_user');
        nusr.addQuery('sys_id', requested_for);
        nusr.query();

        if(usr.next()){
            var nloc = new GlideRecord('cmn_location');
            nloc.addQuery('parent.sys_id', nusr.location.parent.sys_id);
            nloc.query();

            while(nloc.next()){
                obj.push(nloc.sys_id.toString());
            }
         
            obj.push('<defaultlocation1sys_id>');
             obj.push('<defaultlocation2sys_id>');
            obj.push('<defaultlocation3sys_id>');

        }
        }
       
       
        return 'sys_idIN'+obj;
    },
Reference qualifier: javascript&colon; new PoV_CatalogtUtils().getrequestedbyandforlocationlist(current.variables.requested_by,current.variables.requested_for)
1 ACCEPTED SOLUTION

Sachin_Nasa
Tera Guru

Hi @harshamovva ,

 

I noticed one issue in your updated Script Include. In the requested_for block, you're querying nusr, but your condition is still checking if (usr.next()) instead of if (nusr.next()). Because of that, the second query never processes correctly.


Also, after updating either the Requested By or Requested For variable, make sure the Location reference field is refreshed (for example, using g_form.clearValue('location') and g_form.refreshReference('location') in an onChange Catalog Client Script).

 

Otherwise, the reference qualifier may not be re-evaluated automatically.
If you've already done that and it's still not working, please share the onChange Catalog Client Script you're using for the Requested For variable.

 

If you found this helpful, please mark it as Helpful and Accept as Solution so it can reach others too.
Thanks & Regards,
Sachin Narayanasamy

View solution in original post

4 REPLIES 4

Sachin_Nasa
Tera Guru

Hi @harshamovva ,

 

I noticed one issue in your updated Script Include. In the requested_for block, you're querying nusr, but your condition is still checking if (usr.next()) instead of if (nusr.next()). Because of that, the second query never processes correctly.


Also, after updating either the Requested By or Requested For variable, make sure the Location reference field is refreshed (for example, using g_form.clearValue('location') and g_form.refreshReference('location') in an onChange Catalog Client Script).

 

Otherwise, the reference qualifier may not be re-evaluated automatically.
If you've already done that and it's still not working, please share the onChange Catalog Client Script you're using for the Requested For variable.

 

If you found this helpful, please mark it as Helpful and Accept as Solution so it can reach others too.
Thanks & Regards,
Sachin Narayanasamy

Thank you for this.

Anytime! @harshamovva 

Thanks & Regards,
Sachin Narayanasamy

Tanushree Maiti
Tera Patron

Hi @harshamovva 

 

Can you try this. If it works - update it as per your requirement:

 

  1. Update Refrence Qualifier of Location Reference variable
  • Right-click the Location field on your form and click Configure Dictionary.
  • Update  the Reference Qualifier
  •  In the Reference qual field, enter the following JavaScript to filter locations associated with the selected user's location:
    javascript&colon; 'sys_idIN' + new YourScriptIncludeName().getLocations(current.variables.u_requested_by);

2. Create the Script Include

  • Navigate to System Definition > Script Includes and create a new record.
  • Name it: GetUserLocations
  • Check Client callable.

 

var GetUserLocations = Class.create(userId);

GetUserLocations.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getLocationData: function() {

        var userGr = new GlideRecord('sys_user');

        if (userGr.get(userId)) {

            return userGr.location;

        }

        return '';

    },

    type: 'GetUserLocations'

});

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti