Service catalog editors - fetch all itil roles
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2024 07:08 AM
I need to continuously update my editor list of a service catalog i own, to contain all users with itil role.
how can i achieve this ?
Thank you !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2024 07:45 AM
Could you please explain your requirement in detail?
Regard
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2024 11:55 PM
i have a maintain catalog which has to contain as editors all of the itil roles on my instance,
i was thinking maybe use a calculated value or update job so it will add all users who has the itil role but im out of idea how to make it happen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 01:41 AM
Hi @dimabureyko ,
1) First, ensure that the user has the catalog_editor role, as only users with this role can be added to the editors field.
2) Use below script to add ITIL and catalog_editor user in the editor field.
You can place the script below in a scheduled job and set it to run according to your desired timeline.
// Create a new GlideRecord object for the 'sc_catalog' table
var grScCatalog = new GlideRecord('sc_catalog');
// Attempt to retrieve the catalog record by its sys ID
if (grScCatalog.get('e0d08b13c3330100c8b837659bba8fb4')) { // replace the sys ID as per your record
// Array to store users who have both the itil and catalog_editor roles
var usersWithItilAndCatalogEditorRole = [];
// Create a new GlideRecord object for the 'sys_user' table to query users
var grUser = new GlideRecord('sys_user');
// Add a query to find users who have the 'itil' role
grUser.addQuery('roles', 'itil');
// Add a query to find users who have the 'catalog_editor' role
grUser.addQuery('roles', 'catalog_editor');
// Ensure that only active users are included in the results
grUser.addQuery('active', true);
// Execute the query
grUser.query();
// Iterate through the resulting user records
while (grUser.next()) {
// Add the unique user ID to the array of users with both roles
usersWithItilAndCatalogEditorRole.push(grUser.getUniqueValue());
}
// Update the 'editors' field in the catalog record with the list of users
grScCatalog.editors = usersWithItilAndCatalogEditorRole.join(',');
// Save the changes to the catalog record
grScCatalog.update();
// Log a message indicating the update was successful
gs.info('The editors field has been updated with users who have the itil and catalog_editor roles.');
} else {
// Log a message indicating that the catalog record was not found
gs.info('Catalog record not found.');
}
If this resolves your issue, kindly mark it as Correct If you find my response
helpful, please mark it as Helpful based on its impact.
Best regards,
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2024 12:26 AM
Hello @dimabureyko ,
o achieve this, you can use a background script like the one I provided below, which will update the editors field on your service catalog record to include all users with the itil role. You have options for automation: either create a scheduled job to run this script periodically (daily, weekly, etc.) based on how often users are added, or set up a business rule on the sys_user_has_role table to trigger whenever users with the itil role are added.
Here's the Backgorund script to update the editors field:
var grScCatalog = new GlideRecord('sc_catalog');
if (grScCatalog.get('e0d08b13c3330100c8b837659bba8fb4')) { // replace the sys ID as per your record
var usersWithItilRole = []; // Array to store users with the itil role
// Query for users who have the 'itil' role
var grUserRole = new GlideRecord('sys_user_has_role');
grUserRole.addQuery('role.name', 'itil');
grUserRole.query();
while (grUserRole.next()) {
usersWithItilRole.push(grUserRole.user.toString());
}
// Update the 'editors' field with the itil users list
grScCatalog.editors = usersWithItilRole.join(',');
grScCatalog.update();
gs.info('The editors field has been updated with users who have the itil role.');
} else {
gs.info('Catalog record not found.');
}
- Scheduled Job: Ideal if you want the updates to happen at regular intervals, such as once a day or once a week.
- Business Rule on
sys_user_has_role
: Create an After Insert rule to update theeditors
field whenever a user gains theitil
role, ensuring your list is always up-to-date in real-time.
If you need further assistance with setting up the schedule or business rule, feel free to ask!
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Regards,
Aniket