how to remove the bookmarks when the user s to remove pps_resource role

harivas
Tera Contributor

How to remove the bookmarks when user to remove pps_resource role, I have added the bookmarks when the user to add the pps_resource role throgh business rule, but iam trying to do for remove the bookmarks when users to remove pps_resource role, can any one help me on this query

business rule: for remove the bookmarks

// Get the user's sys_id
var userSysId = gs.getUserID();

// Query the sys_user_has_role table to check if the user has the pps_resource role
var userHasRole = new GlideRecord('sys_user_has_role');
userHasRole.addQuery('user', userSysId);
userHasRole.addQuery('role.name', 'pps_resource');
userHasRole.query();

// If the user does not have the pps_resource role, delete the bookmarks
if (!userHasRole.next()) {
    var bookmarks = [
        {
            title: 'Projects (Active)',
            url: 'pm_project_list.do?sysparm_query=active%3Dtrue&sysparm_first_row=1&sysparm_view=&sysparm_fixed_query=sys_class_nameINjavascript:getValidProjectClasses()',
            order: 1
        },
        {
            title: 'Demands (Active)',
            url: 'dmn_demand_list.do?sysparm_userpref_module=4701750c833211001ac7f7338ef6224e&sysparm_fixed_query=sys_class_name=dmn_demand',
            order: 2
        },
        {
            title: 'Pipeline and Resource Dashboard Dashboard',
            url: '$pa_dashboard.do?sysparm_dashboard=9c64cea787c8b510efed74c9cebb35fe&sysparm_tab=b37442e787c8b510efed74c9cebb35da&sysparm_cancelable=true&sysparm_editable=undefined&sysparm_active_panel=false',
            order: 3
        }
    ];

    // Loop through the bookmarks and delete them
    bookmarks.forEach(function(bookmark) {
        var gr = new GlideRecord('u_bookmark');
        gr.addQuery('user', userSysId);
        gr.addQuery('title', bookmark.title);
        gr.query();
        while (gr.next()) {
            gr.deleteRecord();
        }
    });
}
I have tried the single business rule but I didn't get so created ne more for remove the book marks

Iam posting  adding bookmarks business rule aswell can I add here only for remove book marks?
 
(function executeRule(current, previous /*null when async*/ ) {

    var grrole = new GlideRecord('sys_user_has_role'); // Gliding the sys_user_has_role table
    grrole.addQuery('role.name', 'pps_resource'); // checking users is having 'pps_resource' role
    grrole.query();

    if (grrole.hasNext()) { // If the user has 'pps_resource' role
        while (grrole.next()) {
            var user = grrole.user.getRefRecord(); // Get the user record

            // Define the bookmarks in the order you want them to appear
            var bookmarks = [{
                    title: 'Projects (Active)',
                    url: 'pm_project_list.do?sysparm_query=active%3Dtrue&sysparm_first_row=1&sysparm_view=&sysparm_fixed_query=sys_class_nameINjavascript:getValidProjectClasses()',
                    order: 1
                },
                {
                    title: 'Demands (Active)',
                    url: 'dmn_demand_list.do?sysparm_userpref_module=4701750c833211001ac7f7338ef6224e&sysparm_fixed_query=sys_class_name=dmn_demand',
                    order: 2
                },
                {
                    title: 'Pipeline and Resource Dashboard Dashboard',
                    url: '$pa_dashboard.do?sysparm_dashboard=9c64cea787c8b510efed74c9cebb35fe&sysparm_tab=b37442e787c8b510efed74c9cebb35da&sysparm_cancelable=true&sysparm_editable=undefined&sysparm_active_panel=false',
                    order: 3
                }
            ];

            // For each bookmark
            for (var i = 0; i < bookmarks.length; i++) {
                var bookmark = new GlideRecord('sys_ui_bookmark'); // Gliding the bookmark table
                bookmark.addQuery('user', user.sys_id);
                bookmark.addQuery('title', bookmarks[i].title);
                bookmark.query();
                if (!bookmark.next()) { // If no bookmark exists for the user with the title
                    bookmark.initialize();
                    bookmark.title = bookmarks[i].title;
                    bookmark.url = bookmarks[i].url;
                    bookmark.user = user.sys_id;
                    bookmark.order = bookmarks[i].order;
                    bookmark.insert(); // Create the new bookmark record
                }
            }
        }
    }

})(current, previous);
 
thanks 
Harivas
1 ACCEPTED SOLUTION

If you are trying to delete records from the sys_ui_bookmark table, why is your script deleting from the u_bookmark table?

 

 // Loop through the bookmarks and delete them
    bookmarks.forEach(function(bookmark) {
        var gr = new GlideRecord('u_bookmark');
        gr.addQuery('user', userSysId);
        gr.addQuery('title', bookmark.title);
        gr.query();
        while (gr.next()) {
            gr.deleteRecord();
        }
 
I think with just taking the correct tables, you don't need any sample code.

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

3 REPLIES 3

Mark Manders
Mega Patron

You are trying to delete records from the 'u_bookmark' table? What table is that? And why not just lookup the bookmark titles when you are deleting them? It's easier to do.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Iam trying to delete the bookmarks in sys_ui_bookmark table, When the remove pps_resource role to user

Can You provide the sample code?

 

If you are trying to delete records from the sys_ui_bookmark table, why is your script deleting from the u_bookmark table?

 

 // Loop through the bookmarks and delete them
    bookmarks.forEach(function(bookmark) {
        var gr = new GlideRecord('u_bookmark');
        gr.addQuery('user', userSysId);
        gr.addQuery('title', bookmark.title);
        gr.query();
        while (gr.next()) {
            gr.deleteRecord();
        }
 
I think with just taking the correct tables, you don't need any sample code.

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark