Requested for Catalog Variable to display based logged in user company
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 04:39 AM
Hi,
I have a requirement where I have to use requested for catalog variable and display the users based on the logged in user company. I have typical use case.
I have Company A which is a parent company for Company B, Company C, Company D. When the logged in user belong to any one of this I have to show users from all the companies. I also have Company E , Company F , Company G and Company H where I have show user belonging to their respective companies. Can any one help me how to achieve this.
Thanks in Advance.
Jitendra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 05:27 AM
Hello @jitendrag,
Here's an example script that you can use in a catalog client script or UI policy script to populate the users dynamically:
// Assuming 'requested_for' is the variable name for which you want to filter users
var requestedForField = current.variables.requested_for; // Change 'requested_for' to your actual variable name
// Get the logged-in user's company
var loggedInUser = gs.getUser();
var loggedInCompany = loggedInUser.getCompanyID();
// Query users based on company hierarchy
var userGr = new GlideRecord('sys_user');
userGr.addQuery('active', true);
if (loggedInCompany == 'parent_company_id') {
// For parent company (e.g., Company A)
userGr.addEncodedQuery('company=parent_company_id^ORcompany=company_b^ORcompany=company_c^ORcompany=company_d');
} else if (loggedInCompany == 'company_e') {
// For Company E
userGr.addQuery('company', 'company_e');
} else if (loggedInCompany == 'company_f') {
// For Company F
userGr.addQuery('company', 'company_f');
} else if (loggedInCompany == 'company_g') {
// For Company G
userGr.addQuery('company', 'company_g');
} else if (loggedInCompany == 'company_h') {
// For Company H
userGr.addQuery('company', 'company_h');
} else {
// Default behavior if none of the specific conditions match
userGr.addQuery('company', loggedInCompany);
}
// Execute the query
userGr.query();
// Populate the 'requested_for' field options with filtered users
while (userGr.next()) {
requestedForField.addOption(userGr.sys_id, userGr.name);
}Check this script and update it as per your requirement.
Please accept my solution if it works for you and thumps up to mark it as helpful.
Thank you!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:49 AM
Is this is possible for the catalog variable type Requested for as well?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 02:14 AM
Hello @Dnyaneshwaree ,
In the above condition the logged in user can be from child company as well.How to achieve this
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2024 02:56 AM
@Ankur Bawiskar Can you please help me with this?