Check if user is in the Catalog item view or Request item from a Script Include

ayman_h
Kilo Sage
Kilo Sage

Hi,

I have a dynamic reference qualifier for a reference field that calls a Script Include to populate it. I would like it to remove specific values for the field in the Catalog item view (on the portal) and not on the Request item. Is there a way to check from a Script include if its loaded from a Catalog item view?

 

If not, can anyone suggest a separate option?

 

Thanks,

Ayman

2 REPLIES 2

Community Alums
Not applicable

Hi @ayman_h ,

Yes, you can check if the Script Include is being called from the Service Catalog by examining the gs.getSession().isInteractive() method and using gs.getSession().isLoggedIn() to differentiate between views.

You can modify your Script Include to detect if it's being executed in the Service Portal. The Service Portal sets a special URL parameter (sysparm_requested_for or sysparm_id). You can check for these parameters in your Script Include.

 

 

var CustomReferenceQualifier = Class.create();
CustomReferenceQualifier.prototype = {
    initialize: function() {},

    getFilteredValues: function() {
        var isServicePortal = gs.getSession().isInteractive(); // Returns true for Service Portal
        var gr = new GlideRecord('sys_user'); // Replace with your table
        
        gr.addActiveQuery(); // Example filter for active users
        if (isServicePortal) {
            gr.addQuery('user_name', '!=', 'admin'); // Example: Exclude 'admin' in Portal view
        }
        
        gr.query();
        var results = [];
        while (gr.next()) {
            results.push(gr.sys_id.toString());
        }
        return results;
    },

    type: 'CustomReferenceQualifier'
};

 

 

 

Solution 2: Using a Client Script

If you want to filter values only in Service Portal, you can use a Client Script instead of modifying the Script Include.

Client Script (Type: Catalog Client Script)

function onLoad() { if (window.location.href.indexOf('sc_cat_item') > -1) { g_form.setValue('your_reference_field', ''); // Example: Clear reference field } }
 

Solution 3: Use UI Policies

  1. Create a UI Policy
  2. Condition: g_form.isNewRecord() && window.location.href.indexOf('sc_cat_item') > -1
  3. Action: Hide/Filter the reference field

 

 

  

Ankur Bawiskar
Tera Patron
Tera Patron

@ayman_h 

you can pass the URL to script include and see if it contains portal or not

gs.action.getGlideURI().toString()

If my response helped please mark it correct and close the thread so that it benefits future readers.

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