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

Hi @Shweta Maurya, there was an type error in second else (line 28)  I have corrected it and and also updated line 24 & 29. The below script checks as below:

Check if user exists, if yes, glide record the group member table and query if the user is part of the group.
If user is an group member, then glide record pending worker table and query with empId, DOB and Joining status, if the record is found, then update flag as True else update it as False.

If user is not an group member, then update flag as False.

 

Please correct if my understanding is not correct.

 

 

 

(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 = false;
                grUser.update();
            }
        }
        else {
            grUser.u_flag = false;
            grUser.update();
        }
    }

})(current, previous);

 

Regards,

Sunil

 

Yes the requirement is correct, and I already tried with this script still it is always going to second else False section....which is incorrect....Please guide 

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

Hi @Tai Vu 

 

Thanks for the above suggestion. It is working now.

 

I was running this BR before insert/delete. that was the mistake here