The CreatorCon Call for Content is officially open! Get started here.

Below BR is not working

Shweta Maurya
Tera Contributor

Requirement-To set value custom filed value as "true" on user table if user found in group. If user not present then set as false. Below is the script.

Currently with below script---It is setting to true if i am adding any other group instead of requested one.

 

(function executeRule(current, previous /*null when async*/ ) {
    var emplyid = current.user.employee_number;
    var dob = current.user.u_date_of_birth;
    //var uid = gs.getUserID;
    gs.log("birth" + dob);
    gs.log("empid" + emplyid);
    // var grmember = [];
    var userSysID = current.user.toString();
   // var groupname = 'snowempiconnect';
 var checkusergroup = new GlideRecord('sys_user_grmember');
    checkusergroup.addQuery('user', userSysID);
    checkusergroup.addQuery('group', '446d1e0ddbb6a410da91026dd3961919');
    checkusergroup.setWorkflow(false);
    checkusergroup.query();
    gs.log("user exist in snowemp group" + checkusergroup.getRowCount());
    gs.log("username" + checkusergroup.group.name);
    while (checkusergroup.next()) {
        var compareuser = new GlideRecord('pending_worker');
        compareuser.addQuery('u_date_of_birth', dob);
        compareuser.addQuery('u_company_id', emplyid);
        compareuser.addQuery('u_joining_status', 'Joined');
        compareuser.query();
        gs.log("pending user" + compareuser.getRowCount());
        gs.log("Pending user name" + compareuser.getValue('u_first_name'));
        if (compareuser.next()) {
            gs.log("checking");
            var gr = new GlideRecord('sys_user');
            gr.addQuery('sys_id', userSysID);
            gr.query();
            if (gr.next()) {
                gr.u_flag = true;
                gr.update();
                gs.info('success---- working');
            } else {

                gr.u_flag = false;
                gr.update();
                gs.info('success---- not working');
            }
        }

    }

})(current, previous);
1 ACCEPTED SOLUTION

Tai Vu
Kilo Patron
Kilo Patron

Hi @Shweta Maurya 

Considering your business rule is already running on the Group Member [sys_user_grmember] table, I'm uncertain why you would need an additional query in this table.

Feel free to try the approach I suggest below:

Business rule after insert/delete

Condition: Group is 446d1e0ddbb6a410da91026dd3961919

Advanced Script:

var grUser = current.user.getRefRecord();
//Remove Member
if(current.operation() === 'delete'){
    grUser.u_flag = false;
    grUser.update();
    return;
}

//New Member
var emplyid = current.user.employee_number.toString();
var dob = current.user.u_date_of_birth.toString();
var userSysID = current.user.toString();
var grWorker = new GlideRecord('pending_worker');
grWorker.addQuery('u_date_of_birth', dob);
grWorker.addQuery('u_company_id', emplyid);
grWorker.addQuery('u_joining_status', userSysID);
grWorker.query();
if(grWorker.hasNext()){
    grUser.u_flag = true;
    grUser.update();
}

 

You might also need another rule in the pending_worker table because the flag depends on the data in this table as well.

 

Cheers,

Tai Vu

View solution in original post

8 REPLIES 8

Vishal Birajdar
Giga Sage

Hello @Shweta Maurya 

 

Can you let us know on which table this business rule is running and what is the condition you have used to trigger this BR...??

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Using table - sys_user_grmember

Business rule running on before insert & update

Condition- when user is 'active'

SunilKumar_P
Giga Sage

Hi @Shweta Maurya, Can you try the below script?

 

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

    var empId = current.user.employee_number;
    var dob = current.user.u_date_of_birth;
    var userSysID = current.user.toString();

    var grUser = new GlideRecord('sys_user');
    if (grUser.get(userSysID)) {
        var checkUserGroup = new GlideRecord('sys_user_grmember');
        checkUserGroup.addQuery('user', userSysID);
        checkUserGroup.addQuery('group', '446d1e0ddbb6a410da91026dd3961919');
        checkUserGroup.setWorkflow(false);
        checkUserGroup.query();
        if (checkUserGroup.next()) {
            var compareUser = new GlideRecord('pending_worker');
            compareUser.addQuery('u_date_of_birth', dob);
            compareUser.addQuery('u_company_id', emplyid);
            compareUser.addQuery('u_joining_status', 'Joined');
            compareUser.query();
            if (compareUser.next()) {
                grUser.u_flag = true;
                grUser.update();
            } else {
                grUser.u_flag = true;
                grUser.update();
            }
        }
        eles {
            grUser.u_flag = true;
            grUser.update();
        }
    }

})(current, previous);

 

Regards,

Sunil

With the above script it is always going to "second else" part. which is incorrect