Restrict Response Task on SIR

AfifaH
Tera Contributor

My use case is  that I have to  restrict record visibility on Response Task(sn_si_task). If my response task share common parent  i.e SIR (sn_si_incident)then only make tasks visible to logged user who is also Assigned to user of the Response Tasks

For Eg: There are 3 SIRS :SIR 1 ,SIR 2and SIR 3.

6 Response Task : SIT 1 , SIT 2  , SIT 3 ,SIT 4, SIT 5 and SIT 6

For Response Tasks SIT 1:  Parent is SIR 1 an Assigned to: Adam

For Response Tasks SIT 2:  Parent is SIR 1 an Assigned to: John

 

For Response Tasks SIT 3:  Parent is SIR 2 an Assigned to: Adam

For Response Tasks SIT 4:  Parent is SIR 2 an Assigned to: Elizabeth

 

For Response Tasks SIT 5:  Parent is SIR 3 an Assigned to: Emma

For Response Tasks SIT 6:  Parent is SIR 3 an Assigned to: Emilie

When I Log in as Adam and navigate to Response Task( sn_si_task) .I should be able to see SIT 1,SIT 2 ,SIT 3 and SIT 4 because SIT 1, SIT 3 are assigned to Adam and Elizabeth and John share common parent with Adam for SIT 2 and SIT 4 

 

I wrote a before query BR on  Response Task (sn_si_task ).The issue is it is only able to query those tasks that are assigned to logged in user and excludes tasks sharing common parent with logged in user.Please find below the code:

 

 

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

    // Get the current user (assuming you can retrieve this in your context)
    var currentUser = gs.getUserID();
    var par = [];
    var parents = new GlideRecord('sn_si_task');
    parents.addQuery('assigned_to', currentUser);
    parents.query();
    while (parents.next()) {
        par.push(parents.sys_id.toString());

    }
    gs.info("Parent SysIDs are " + par);
    current.addQuery('assigned_to', currentUser).addOrCondition('parent.sys_id', 'IN', par);

})(current, previous);

 

 

Am I missing something?Kindly assit
Thank you in advance.

3 REPLIES 3

Satishkumar B
Giga Sage
Giga Sage

Hi @AfifaH 

 

‘parent’ field is not querried correctly. Please check the below updated code:

 

(function executeRule(current, previous /*null when async*/) {
var currentUser = gs.getUserID();
var sirIds = [];


var sirGr = new GlideRecord('sn_si_incident');
sirGr.addQuery('parent.assigned_to', currentUser);
sirGr.query();

while (sirGr.next()) {
sirIds.push(sirGr.parent.toString());
}

var taskGr = new GlideRecord('sn_si_task');
taskGr.addQuery('assigned_to', currentUser); // Tasks directly assigned to the current user
taskGr.addQuery('parent', 'IN', sirIds); // Tasks where the parent is in the list of SIRs assigned to the current user
taskGr.query();

while (taskGr.next()) {

gs.info('Response Task ID: ' + taskGr.sys_id);
}

})(current, previous);

 

———————————————-
Please consider marking my reply as Helpful👍 and/or Accept Solution, if applicable. Thanks!

AfifaH
Tera Contributor

I was able to query the record properly.Please find below the code:

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

    // Get the current user (assuming you can retrieve this in your context)
    var currentUser = gs.getUserID();
    var par = [];
    var parents = new GlideRecord('sn_si_task');
    parents.addQuery('assigned_to', currentUser);
    parents.query();
    while (parents.next()) {
        par.push(parents.parent.toString());

    }
    
    current.addEncodedQuery("assigned_to="+currentUser+"^NQparent.sys_idIN"+par.join(','));

})(current, previous);

  

AfifaH
Tera Contributor

I was able to query the record properly.Please find below the final code:

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

    // Get the current user (assuming you can retrieve this in your context)
    var currentUser = gs.getUserID();
    var par = [];
    var parents = new GlideRecord('sn_si_task');
    parents.addQuery('assigned_to', currentUser);
    parents.query();
    while (parents.next()) {
        par.push(parents.parent.toString());

    }
   
    current.addEncodedQuery("assigned_to="+currentUser+"^NQparent.sys_idIN"+par.join(','));

})(current, previous);