- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Good morning All. I'm trying to add a newly created user record to a company and group upon insert. My script is not working as intended. Could I get a code review and suggestions?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi Ray,
You can add some additional logging to see what is happening throughout the script. I'm going to guess that the first if condition is never met, and may be throwing an error in the system log. The Email field on the user table is the type of email, which is extended from a string type, but is different, so you're probably trying to do a string function (indexOf) on a non-string field. Beyond this, in the company GlideRecord, you don't want to insert as this would create record on the company table. If you are running this Business Rule before Insert, just take out this line. If you are running it after Insert for some reason, replace it with current.update(), though this should be avoided.
(function executeRule(current, previous /*null when async*/ ) {
try {
// Check if the email contains '@astreya.com'
if (current.email && current.email.toString().indexOf('@astreya.com') !== -1) {
gs.info('User BR: inside if1');
// Add user to the "Astreya Partners LLC" company (Assuming it's in the company field)
var company = new GlideRecord('company');
if (company.get('name', 'Astreya Partners LLC')) {
current.company = company.sys_id;
gs.info('User BR: company updated');
}
// Add user to the "Astreya Partners LLC Users" group (Assuming group is named 'Astreya Partners LLC Users')
var group = new GlideRecord('sys_user_group');
if (group.get('name', 'Astreya Partners LLC Users')) {
var userGroup = new GlideRecord('sys_user_grmember');
userGroup.initialize();
userGroup.user = current.sys_id;
userGroup.group = group.sys_id;
userGroup.insert();
gs.info('User BR: user added to group');
}
//logging for tracking
gs.info('User ' + current.name + ' has been added to Astreya Partners LLC company and group.');
} else {
gs.info('User email does not match the required domain.');
}
} catch (err) {
gs.error('Error while adding user to company and group: ' + err);
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi @Ray Mason
Below is the updated script:
(function executeRule(current, previous /*null when async*/) {
try {
var email = (current.email || '').toLowerCase();
if (email.endsWith('@astreya.com')) {
// Add to Company
var company = new GlideRecord('core_company'); // 'company' is actually 'core_company' table
if (company.get('name', 'Astreya Partners LLC')) {
current.company = company.sys_id; // Will be saved automatically in before-insert
}
// Add to Group
var group = new GlideRecord('sys_user_group');
if (group.get('name', 'Astreya Partners LLC Users')) {
var userGroup = new GlideRecord('sys_user_grmember');
userGroup.addQuery('user', current.sys_id);
userGroup.addQuery('group', group.sys_id);
userGroup.query();
if (!userGroup.hasNext()) { // Avoid duplicate membership
userGroup.initialize();
userGroup.user = current.sys_id;
userGroup.group = group.sys_id;
userGroup.insert();
}
}
gs.info('User ' + current.name + ' added to Astreya Partners LLC company and group.');
} else {
gs.info('User email does not match @astreya.com domain.');
}
} catch (err) {
gs.error('Error while adding user to company and group: ' + err);
}
})(current, previous);
Wrong use of company.insert():You’re retrieving the company record with company.get() and then inserting it again.
This is incorrect — get() retrieves an existing record; calling insert() would attempt to create a duplicate company record.
You should just set current.company = company.sys_id and then current.update() if needed.
Timing issue with current.update() in a before-insert BR
If this is a before-insert business rule, you can just set current.company and not call update() — the value will be saved with the user insert.
If it’s after-insert, you’ll need to call update() to persist changes.
Better check for case-insensitive email domain
Right now you’re checking indexOf('@astreya.com'). This is case-sensitive. Emails may come in uppercase or mixed case.
Use .toLowerCase().endsWith('@astreya.com') for a safer match.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a month ago
Hi Ray,
You can add some additional logging to see what is happening throughout the script. I'm going to guess that the first if condition is never met, and may be throwing an error in the system log. The Email field on the user table is the type of email, which is extended from a string type, but is different, so you're probably trying to do a string function (indexOf) on a non-string field. Beyond this, in the company GlideRecord, you don't want to insert as this would create record on the company table. If you are running this Business Rule before Insert, just take out this line. If you are running it after Insert for some reason, replace it with current.update(), though this should be avoided.
(function executeRule(current, previous /*null when async*/ ) {
try {
// Check if the email contains '@astreya.com'
if (current.email && current.email.toString().indexOf('@astreya.com') !== -1) {
gs.info('User BR: inside if1');
// Add user to the "Astreya Partners LLC" company (Assuming it's in the company field)
var company = new GlideRecord('company');
if (company.get('name', 'Astreya Partners LLC')) {
current.company = company.sys_id;
gs.info('User BR: company updated');
}
// Add user to the "Astreya Partners LLC Users" group (Assuming group is named 'Astreya Partners LLC Users')
var group = new GlideRecord('sys_user_group');
if (group.get('name', 'Astreya Partners LLC Users')) {
var userGroup = new GlideRecord('sys_user_grmember');
userGroup.initialize();
userGroup.user = current.sys_id;
userGroup.group = group.sys_id;
userGroup.insert();
gs.info('User BR: user added to group');
}
//logging for tracking
gs.info('User ' + current.name + ' has been added to Astreya Partners LLC company and group.');
} else {
gs.info('User email does not match the required domain.');
}
} catch (err) {
gs.error('Error while adding user to company and group: ' + err);
}
})(current, previous);