Before query business rule on task_sla table not working

Aruna Sree Yela
Tera Guru

Hi,

 

I want to hide incident task slas with SLA definition "N Incident Resolution SLA", I tried to restrict them using before query BR. The if blocks are triggering fine with the info messages, but the query is not applying. Can anyone please help me.

 

Here is the details of BR

 

Table : task_sla

 

ArunaSreeYela_0-1688883803565.png

 

Note: "u_type_of_user" is a field in sys_user table

 

Thanks 🙂

 

 

1 ACCEPTED SOLUTION

Arun_S1
Tera Guru
Tera Guru

@Aruna Sree Yela I am assuming that you do not want to display some SLA records for a certain type of user.

 

1. I created a field in my "sys_user" table called 'u_type_of_user' and provided the choices 'external' & 'internal'. If the logged in user is external, I don't want few SLA records to be displayed. (please correct me if my understanding is wrong).

 

2. Added a new before query Business Rule on the 'task_sla' table with the below script.

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var user_rec=new GlideRecord('sys_user');
	user_rec.get(gs.getUserID());
	if(user_rec.u_type_of_user=='external'){
		current.addEncodedQuery('sla!=35420982d732220035ae23c7ce610393');
	}

})(current, previous);

 

This script will not allow me to see the Task SLA records that belongs to a specific SLA definition if I am an external, else it will be as usual.

 

If your expectation is to modify the query that is executed against the TASK SLA table, then you should use the variable current. In your above script 17-31 needs to be rewritten to use current.addQuery instead of gr.query.

 

Please mark the appropriate response as correct answer and helpful.

 

Thanks!!

View solution in original post

7 REPLIES 7

Arun_S1
Tera Guru
Tera Guru

@Aruna Sree Yela I am assuming that you do not want to display some SLA records for a certain type of user.

 

1. I created a field in my "sys_user" table called 'u_type_of_user' and provided the choices 'external' & 'internal'. If the logged in user is external, I don't want few SLA records to be displayed. (please correct me if my understanding is wrong).

 

2. Added a new before query Business Rule on the 'task_sla' table with the below script.

 

(function executeRule(current, previous /*null when async*/) {

	// Add your code here
	var user_rec=new GlideRecord('sys_user');
	user_rec.get(gs.getUserID());
	if(user_rec.u_type_of_user=='external'){
		current.addEncodedQuery('sla!=35420982d732220035ae23c7ce610393');
	}

})(current, previous);

 

This script will not allow me to see the Task SLA records that belongs to a specific SLA definition if I am an external, else it will be as usual.

 

If your expectation is to modify the query that is executed against the TASK SLA table, then you should use the variable current. In your above script 17-31 needs to be rewritten to use current.addQuery instead of gr.query.

 

Please mark the appropriate response as correct answer and helpful.

 

Thanks!!

Thank you @Arun_S1 , the response was working fine.

 

I have a small change in the condition here.

 

Condition: type_of_user == xyz and not member of any one of the groups that contains xyz, for this I tried the below code. This was working fine if there is only one group that contains xyz, but not for multiple groups.(which means its evaluating if user is member of all xyz groups)

 

var Ngroups = new GlideRecord('sys_user_group');
    Ngroups.addEncodedQuery('nameLIKExyz');
    Ngroups.addQuery('active'true);
    Ngroups.query();
    while (Ngroups.next()) {

        var groups = Ngroups.getValue('name');
        if (user_rec.u_type_of_user == 'xyz' && !(gs.getUser().isMemberOf(groups))) {
         current.addEncodedQuery('sla!=725e8352973321107293bbe3a253af69');
        }
    }
 
Can you please suggest me how can I modify the code.
 
Thanks 🙂

@Aruna Sree Yela still not sure if I understand the requirement correctly.

Below is my assumption

  • I have the field "type_of_user" in the sys_user table and this contains the values "Internal" & "External".
  • There are assignment groups in my instance which starts with the prefix "Internal" & "External"
  • example:- "Internal Hardware", "External Software"
  • If I am an internal and member of one of the internal groups then I should be able to see TASK SLA records which belongs to a specific definition, otherwise it should be hidden.

Solution:

I would suggest you to glide against the Group Members (sys_user_grmember) table instead of the Group (sys_user_group) table.

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var user_rec = new GlideRecord('sys_user');
    user_rec.get(gs.getUserID());
    if (user_rec.u_type_of_user == 'external') {
        var Ngroups = new GlideRecord('sys_user_grmember');
        Ngroups.addEncodedQuery('group.nameSTARTSWITHexternal^group.active=true^user=' + gs.getUserID());
        Ngroups.query();
        if (Ngroups.next()) {
            current.addEncodedQuery('sla!=35420982d732220035ae23c7ce610393');
        }
    }

})(current, previous);

 

The script checks if the user is an external, and a member of any active external groups it will hide the task sla records. In case the user is an internal it will display all the task sla records.

 

Hope this helps.

 

Please mark the appropriate response as correct answer and helpful.

Thanks!!

Hi @Arun_S1 , the condition is like type_of_user is external and not member of groups that contains "internal".

 

There is no group for external user, hence I need to check type is external and not member of internal group.