Unable to Add Reader in Document Group Permission table via Business Rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2023 03:43 AM
Hi Community,
I am facing bit of a weird issue and unable to figure out what causing this, hence looking for the help.
I am using ServiceNow's Document (ds_document) module, where I have two custom created list collector, one of which stores list of users and the other one stores list of groups.
Now in document there are two tables i.e. User Permission (ds_document_user_can_access_mtom) and Group Permission (ds_document_ug_can_access_mtom), where we can add users/groups as Reader/Writer/Owner of the document respectively.
Now I am trying to use a business rule to add the users in the user list collector as readers in user permission table and groups in the group list collector as readers in Group Permission table. PFB the script -
(function executeRule(current, previous /*null when async*/ ) {
var id = current.sys_id;
if (!gs.nil(<user_list_collector_fieldname>)) {
var arr =<user_list_collector_fieldname>.toString().split(',');
for (var i = 0; i < arr.length; i++) {
var rec = new GlideRecord('ds_document_user_can_access_mtom');
rec.initialize();
rec.document =id;
rec.user = arr[i];
rec.permission = 'reader';
rec.insert();
}
}
if (!gs.nil(<group_list_collector_fieldname>)) {
var grarr = <group_list_collector_fieldname>.toString().split(',');
for (var j = 0; j < grarr.length; j++) {
var grrec = new GlideRecord('ds_document_ug_can_access_mtom');
grrec.initialize();
grrec.document = current.sys_id;
grrec.user_group = grarr[j];
grrec.permission = 'reader';
grrec.insert();
}
}
})(current, previous);
Now the script is working fine and adding the users in the User Permission table correctly. But the groups aren't gettiing added in the Group Permission table (Even though via logs I have checked and found the grrec.insert() is returning a sys_id).
This exact script is working fine in Background - Script. And I am able to add the groups via Flow Designer too.
So I am a bit bamboozled why it's not working in BR.
Am I missing something.
Any help will be much appreciated.
Regards,
Debjit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2023 04:13 AM
The exact script can't be working fine in Background as the group list collector and sys_id would have to be hard-coded, or at least different than 'current....' So within the for loop log grarr[j] and whatever you are assigning to grrec.document to confirm the record that is getting created. You can also check the table to see recent records that have been added and likely the document or user_group is empty or otherwise not what you intended.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2023 05:01 AM - edited 12-24-2023 05:07 AM
Hi Brad,
Thanks for the response.
You are right. I am making a slight change in the script where I am adding the following two lines in the script to let the script know what 'current' is in BG - Script.
var current = new GlideRecord('ds_document');
current.get(<sys_id_of_the_record_I_am_testing_on>)
other than that everything else is same.
Also, there's no change in total number of records before and after the script execution which tells me the records did not get created.
Although, Upon adding a log for grrec.insert() I found it is returning a sys_id, which means it is creating a record but getting deleted due to some odd reason. (As searching with that sys_id gives me nothing).
Strangely, no such behavior for user permission even though both have similar config. Here, the records are correctly creating.
Regards,
Debjit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-24-2023 12:56 PM
Interesting. Are you just getting one sys_id in the log, or one for each group selected? Something like this should give you more information.
if (!gs.nil(<group_list_collector_fieldname>)) {
gs.info('BR script: group list = ' + current.group_list_collector_fieldname);
var grarr = <group_list_collector_fieldname>.toString().split(',');
var newsysid = '';
for (var j=0; j<grarr.length; j++) {
gs.info('BR script: group = ' + grarr[j] + 'document = ' + current.sys_id);
var grrec = new GlideRecord('ds_document_ug_can_access_mtom');
grrec.initialize();
grrec.document = current.sys_id;
grrec.user_group = grarr[j];
grrec.permission = 'reader';
newsysid = grrec.insert();
gs.info('BR script: new sys_id = ' + newsysid);
}
}
You could also add a line before the insert to make sure no other Business Rule is deleting the record after it is inserted:
grrec.setWorkflow(false);
and check the delete records table and system log right after the test to see if that sheds any light.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2023 07:28 PM
Upon checking the deleted records and adding the logs as per your suggestion found that the records are getting created successfully. But they got deleted immediately after their creation.
No idea why this is happening as user permission table also has exact similar structure (Similar BR/UI Action/UI Policy/Client Script etc) but this is only happening for Group permission table only, and also only when I am creating records from a Business Rule.!!!
Even setWorkflow(false) did not resolve the issue.
Regards,
Debjit