Unable to Add Reader in Document Group Permission table via Business Rule

DebjitGhosh31
Tera Contributor

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

7 REPLIES 7

Have you posted the entire BR script?  Are there possibly any BR on the ds_document_ug_can_access_mtom table, or any other tables that have a script which references this table and have a deleteRecord or deleteMultiple line?

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @DebjitGhosh31 

Please give a try to the script below and let me know how it works for you and also I have added the logs so that it could be helpful to us to identify the cause.

(function executeRule(current, previous /*null when async*/) {
    
    var id = current.sys_id;
    
    if (!gs.nil(current.<user_list_collector_fieldname>)) {
        var userArr = current.<user_list_collector_fieldname>.toString().split(',');
        
        for (var i = 0; i < userArr.length; i++) {
            var userRec = new GlideRecord('ds_document_user_can_access_mtom');
            userRec.initialize();
            userRec.document = current;
            userRec.user = userArr[i];
            userRec.permission = 'reader';
            
            if (userRec.insert()) {
                gs.addInfoMessage("User added successfully: " + userRec.user + " as reader for document: " + current.number);
            } else {
                gs.addErrorMessage("Failed to add user as reader for document: " + current.number);
            }
        }
    }

    if (!gs.nil(current.<group_list_collector_fieldname>)) {
        var groupArr = current.<group_list_collector_fieldname>.toString().split(',');
        
        for (var j = 0; j < groupArr.length; j++) {
            var groupRec = new GlideRecord('ds_document_ug_can_access_mtom');
            groupRec.initialize();
            groupRec.document = current;
            groupRec.user_group = groupArr[j];
            groupRec.permission = 'reader';
            
            if (groupRec.insert()) {
                gs.addInfoMessage("Group added successfully: " + groupRec.user_group + " as reader for document: " + current.number);
            } else {
                gs.addErrorMessage("Failed to add group as reader for document: " + current.number);
            }
        }
    }

})(current, previous);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,
Aniket

Hi Aniket,

 

Thanks for the response.

Based on your response, I've added the info messages which gives me the following info - 

DebjitGhosh31_2-1703646752653.png

As you can see, The records are getting created successfully. 

Also, as advised by Brad in another response to this question, I've checked the Deleted Records table and I can see only Group records are getting deleted immediately after its creation. Don't know why??!!

 

Bit clueless 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.

 

Regards,

Debjit