how to delete the bookmarks when the role remove to a group

harivas
Tera Contributor

how to delete the bookmarks when the role remove to a group for those group members have to delete the bookmarks. 

I have tried with after delete business rule on sys_group_has_role table, can any one help me on this query

the BR script:

(function executeRule(current, previous /*null when async*/ ) {

    var grGroupRole = new GlideRecord('sys_group_has_role'); // Gliding the sys_group_has_role table
    grGroupRole.addQuery('role.name', 'pps_resource'); // checking if group has 'pps_resource' role
    grGroupRole.addQuery('group', current.sys_id);
    grGroupRole.query();

    if (!grGroupRole.hasNext()) { // If the group does not have 'pps_resource' role
        while (grGroupRole.next()) {
            var group = grGroupRole.group.getRefRecord(); // Get the group record
   gs.info("hello");
   var grUser = new GlideRecord('sys_user_grmember'); // Gliding the sys_user_grmember table to get the group members
    grUser.addQuery('group', current.sys_id);
    grUser.query();

    while (grUser.next()) {
        var user = grUser.user.getRefRecord(); // Get the user record

        // Define the bookmarks that you want to delete
        var bookmarks = [
            'Projects (Active)',
            'Demands (Active)',
            'Pipeline and Resource Dashboard'
        ];

        // 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]);
            bookmark.query();
            while (bookmark.next()) { // If bookmark exists for the user with the title
                bookmark.deleteRecord(); // Delete the bookmark record
            }
        }
    }
     }
}
})(current, previous);

thanks 
Harivas


6 REPLIES 6

SanjivMeher
Kilo Patron
Kilo Patron

Not sure, in which case you would want this to run. But I have tried to refine the query. Let me know if it works.

 

(function executeRule(current, previous /*null when async*/ ) {

    var grGroupRole = new GlideRecord('sys_group_has_role'); // Gliding the sys_group_has_role table
    grGroupRole.addQuery('role.name', 'pps_resource'); // checking if group has 'pps_resource' role
    grGroupRole.addQuery('group', current.sys_id);
    grGroupRole.query();

    while (grGroupRole.next()) {
             var grUser = new GlideRecord('sys_user_grmember'); // Gliding the sys_user_grmember table to get the group members
             grUser.addQuery('group', current.sys_id);
             grUser.query();

             while (grUser.next()) {
        // Define the bookmarks that you want to delete
        var bookmarks = [
            'Projects (Active)',
            'Demands (Active)',
            'Pipeline and Resource Dashboard'
        ];

        // 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', grUser.getValue('user'));
            bookmark.addQuery('title', bookmarks[i]);
            bookmark.query();
            while (bookmark.next()) { // If bookmark exists for the user with the title
                bookmark.deleteRecord(); // Delete the bookmark record
            }
        }
    }
     }
}
})(current, previous);

 


Please mark this response as correct or helpful if it assisted you with your question.

Hi @SanjivMeher 

my req is when I remove the pps_resource role to one group on that time I want to remove the book marks for that group members, for that I have written BR on sys_group_has_role table and
when to run is after delete &
advance script is 

(function executeRule(current, previous /*null when async*/ ) {

    var grGroupRole = new GlideRecord('sys_group_has_role'); // Gliding the sys_group_has_role table
    grGroupRole.addQuery('role.name', 'pps_resource'); // checking if group has 'pps_resource' role
    grGroupRole.addQuery('group', current.group.sys_id);
    grGroupRole.query();

    if (!grGroupRole.hasNext()) { // If the group does not have 'pps_resource' role
        while (grGroupRole.next()) {
            var group = grGroupRole.group.getRefRecord(); // Get the group record
       
            var grUser = new GlideRecord('sys_user_grmember'); // Gliding the sys_user_grmember table to get the group members
            grUser.addQuery('group', group);
            grUser.query();

            while (grUser.next()) {
                var user = (grUser.user.getRefRecord()); // Get the user record

            }
        }
    }

    //Define the bookmarks that you want to delete
    var bookmarks = [
        'Projects (Active)',
        'Demands (Active)',
        'Pipeline and Resource Dashboard'
    ];

    // 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]);
        bookmark.query();
        while (bookmark.next()) { // If bookmark exists for the user with the title
            bookmark.deleteRecord(); // Delete the bookmark record
        }
    }
})(current, previous);






Did you try the above code i provided?


Please mark this response as correct or helpful if it assisted you with your question.

Harsh Vardhan
Giga Patron

Hi @harivas I am not sure why are you doing glide record on sys_group_has_role table ? 

 

Can you try this way . 

 

Write "After" business rule on "sys_group_has_role" table and set "Delete" checkbox as true. 

Add filter condition on  business rule  , Role | Is | pps_resource

 

Screenshot : 

 

After BR bookmarks.JPG

 

BR Script :

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here

    var grm = new GlideRecord('sys_user_grmember');
    grm.addEncodedQuery("group=" + current.group);
    grm.query();
    while (grm.next()) {
        bookmarkAction(grm.getValue('user'));

    }

    function bookmarkAction(user) {
         var bookmarks = [
            'Projects (Active)',
            'Demands (Active)',
            'Pipeline and Resource Dashboard'
        ];


        for (var i = 0; i < bookmarks.length; i++) {
            var bookmark = new GlideRecord('sys_ui_bookmark'); // Gliding the bookmark table
            bookmark.addQuery('user', user);
            bookmark.addQuery('title', bookmarks[i]);
            bookmark.query();
            while (bookmark.next()) { // If bookmark exists for the user with the title
                bookmark.deleteRecord(); // Delete the bookmark record
            }
        }
    }

})(current, previous);

 

Hope it will help you.

 

Thanks,

Harsh