- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 03:47 AM - edited 01-24-2024 03:49 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 08:35 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 07:05 AM - edited 01-24-2024 07:07 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 07:47 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 08:35 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2024 08:36 PM
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