Set the "VIP" flag to false when user is Not part of the group.

Rakesh40
Tera Contributor

Hello,

How to set the "VIP" flag to false on user record when user is not part of the group "ALL-VIP-USERS"?

I tried to write the Business rule script which is not working:

 

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

    var grp = new GlideRecord("sys_user_grmember");
    grp.addQuery('group', 'b54fed9593233110b9bff7718bba10ce'); //ALL-VIP-USERS
    grp.query();
    if (grp.next()) {
        gs.info(grp.user);
        var gr_user = new GlideRecord("sys_user");
        gr_user.addQuery("sys_id", grp.user);
        gr_user.query();
        while (gr_user.next()) {
            gr_user.vip = 'true';// To set the VIP true
            gr_user.update();
        }

    } else {
        gr.initialize();
        gr_user.vip = 'false';//  To set the VIP false
        gr_user.update();
    }
})(current, previous);

 Thanks

1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @Rakesh40 

 

Please configure your Business Rule as below and it should work :

 

AmitVerma_0-1704448660381.png

 

AmitVerma_1-1704448691527.png

 

Business Rule Script :

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

	// Add your code here
	var userSysID = current.user;
	var userGR = new GlideRecord('sys_user');
	userGR.addQuery('sys_id',userSysID);
	userGR.query();
	if(userGR.next()){
		var vipStatus = userGR.vip;
		if(vipStatus){
			userGR.vip = 'false';
			userGR.update();
		}
		else
			{
				userGR.vip ='true';
				userGR.update();
			}
		
	}

})(current, previous);

 

Thanks & Regards

Amit Verma


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

View solution in original post

6 REPLIES 6

AshishKM
Kilo Patron
Kilo Patron

Hi @Rakesh40 , 

Please review the logic again, also share the BR running on which table [ sys_user ] or [ sys_user_grmember ].

Second, the script code has If condition, so it will execute for one user ( as per default order by result set ) only, and if we replace the if with while then it will execute for all group (ALL-VIP-USERS) user every time when the said condition trigger. 

 

What's the trigger point for VIP flag set/re-set. Explain the business case here, so we can correct this code. Share the screen shot of BR front page.

 

-Thanks,

AshishKMishra

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

My requirement is :

The VIP tag needs to set to "True" when User is added to the group "ALL-VIP-USERS" and when user is removed from the group "ALL-VIP-USERS", the VIP tag should be set to "False".

 

Screenshot : the BR running on [sys_user_grmember] table.

 

Rakesh40_0-1704442839572.png

 

Hi @Rakesh40 , 

Follow the code shared by @Chetan Mahajan in two separate BR with "when to run" condition for group "ALL-VIP-USERS", so BR will execute only if there is any insert/delete happen in sys_usr_grmember table for this group only. 

 

-Thanks,

AshishKMishra

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Chetan Mahajan
Kilo Sage
Kilo Sage

Hello Rakesh,

                      You can create 2 BR on sys_user_grmember 

                      1. After insert -> condition  Group - is - ALL-VIP-USERS

                          Script :

                           

(function executeRule(current, previous /*null when async*/) {
	var userSysID = current.user;
	
        var userGR = new GlideRecord('sys_user');
	userGR.addQuery('sys_id',userSysID);
	userGR.query();

	if(userGR.next()){
		userGR.vip ='true';
		userGR.update();
	}
})(current, previous);

                

                        2. Before Delete -> Condition Group - is - ALL-VIP-USERS

                            Script : 

                            

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

	var userSysID = current.user;

	var userGR = new GlideRecord('sys_user');
	userGR.addQuery('sys_id',userSysID);
	userGR.query();

	if(userGR.next()){
		userGR.vip ='false';
		userGR.update();
	}

})(current, previous);

 

Please consider marking it as correct and helpful if you find the information beneficial