vaishali231
Tera Guru

hey @LavanyaB8479798 

 

 Approach 

1. Variable Sets do NOT block access

Even though Requestor For and Location are in different variable sets, they are still part of the same catalog item form.

So you can safely reference:

g_form.getValue('requestor_for');

No special handling is required just because they are in separate variable sets.

 

2. Use Advanced Reference Qualifier 

Instead of relying heavily on GlideAjax, the recommended approach is:

  • Use Advanced Reference Qualifier on the Location variable
  • Move all filtering logic into a Script Include

This keeps the solution:

  • Server-side
  • Performant
  • Reusable across catalog items

3. Centralize Logic in Script Include

Create a Script Include (non-client callable) that builds the encoded query.

Code:

var LocationFilterUtil = Class.create();
LocationFilterUtil.prototype = {
   getLocationQuery: function(requestorId) {
       var query = [];
       // Base filters
       query.push("cmn_location_source=xxxxx");
       query.push("cmn_location_type=xxxx");
       // Stockroom condition (only locations with stockroom)
       var locs = [];
       var ga = new GlideAggregate('alm_stockroom');
       ga.groupBy('location');
       ga.query();
       while (ga.next()) {
           locs.push(ga.location.toString());
       }
       if (locs.length)
           query.push("sys_idIN" + locs.join(','));
       // Requestor-based filtering
       if (requestorId) {
           var user = new GlideRecord('sys_user');
           if (user.get(requestorId) && user.location) {
               query.push("parent=" + user.location); // adjust if hierarchy differs
           }
       }
       // Role-based filtering
       if (gs.hasRole('itil')) {
           query.push("parentISEMPTY"); // Region level
       } else {
           query.push("parentISNOTEMPTY"); // Country level
       }
       return query.join('^');
   },
   type: 'LocationFilterUtil'

};

4. Apply in Reference Qualifier

On the Location variable:

javascript: new LocationFilterUtil().getLocationQuery(current.variables.requestor_for);

5. Handle Dynamic Changes (onChange)

If Requestor For can change after load, add a Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   g_form.setValue('location', '');
   g_form.refreshReference('location');

}

This ensures the Location field refreshes with updated filters.

*************************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh