- 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 04:13 AM
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...??
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 04:21 AM
Using table - sys_user_grmember
Business rule running on before insert & update
Condition- when user is 'active'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 04:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2024 05:08 AM
With the above script it is always going to "second else" part. which is incorrect