Help Needed: Business Rule Not Applying Default Time Sheet Policy to New Users with "timecard_user"

DevonP
Tera Contributor

Hello Community,

 

I'm working on setting up a business rule in ServiceNow to automatically assign a default Time Sheet Policy when a new user is created and given the "timecard_user" role. The idea is to ensure that as soon as a user is added to ServiceNow via Workday, LDAP, or any other method, if they are assigned the "timecard_user" role, the default Time Sheet Policy should be applied to them.

 

However, for some reason, my business rule is not working as expected, and the policy is not being applied. I would appreciate any insights or suggestions on what might be going wrong.

 

Here's the script I've been working with:

javascript:


(function executeRule(current, previous /*null when async*/) {
// Check if the user has been assigned the 'timecard_user' role
var hasTimecardUserRole = current.roles.indexOf('timecard_user') > -1;

// Check if the user is new or if the role has just been added
if (!previous || !previous.roles || (previous.roles.indexOf('timecard_user') == -1 && hasTimecardUserRole)) {
// Apply the default Time Sheet Policy
var policyGr = new GlideRecord('time_sheet_policy');
policyGr.addQuery('name', 'Default Policy'); // Ensure you have a default policy named 'Default Policy'
policyGr.query();

if (policyGr.next()) {
current.time_sheet_policy = policyGr.sys_id;
current.update(); // Save the update to the user record
}
}
})(current, previous);
```

Here’s what I’m trying to achieve:
1. When a new user is created or an existing user is updated with the "timecard_user" role, the system should automatically apply the default Time Sheet Policy.


2. I've ensured that a default policy exists in the system with the name "Default Policy."

 

Despite this, the policy is not being applied to the user when they are assigned the role. Has anyone else encountered a similar issue or have any advice on how I can troubleshoot this further?

 

Thank you in advance for your help!

1 REPLY 1

DevonP
Tera Contributor

Sorry I answered my own question, Nothing wrong, I just had the Default Policy name incorrect. I corrected it and it applied.