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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2025 08:21 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2025 08:27 PM
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)
Solution 3: Use UI Policies
- Create a UI Policy
- Condition: g_form.isNewRecord() && window.location.href.indexOf('sc_cat_item') > -1
- Action: Hide/Filter the reference field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-11-2025 08:36 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader