Before Query Business rule

jparman
Tera Guru

Hi All,

 

I have a requirement to limit what the user can see in the incident table. I have created a Before query BR that works well at first but they added a requirement where they want to also show incidents with assignment group = industrial digital and Incident type = Bug fix.

jparman_0-1697596658006.png

 

Here is my current query in the BR:

jparman_1-1697596800006.png

 

I also tried to have an encoded query instead,

var qry = 'caller_idDYNAMIC' + gs.getUserID() + '^NQu_incident_type=bug_fix^assignment_group=d505c5b387126990f95c1f0a7bbb3574^NQassignment_group=d505c5b387126990f95c1f0a7bbb3574^caller_id.department.business_unit=f995ab90dbecf0501470f467f49619ca^u_incident_type=inc_change_request^cmdb_ci.sys_idIN6cba7731db00f0100b476f54059619ed,^ORcmdb_ci.nameSTARTSWITHSalesforce';
current.addEncodedQuery(qry);

 

I got the correct results but there is an issue with that where the links to ticket is not properly working and being redirected to a different incident when using NQ. ( KB0564887 Using operator (^NQ) in encoded queries causes incorrect reference links in the list view )

 

Is there a way that I can implement this using addquery from the BR?

Here is what i have right now but its not working.

 

current.addQuery('caller_id',gs.getUserID())
addOrCondition('u_incident_type','bug_fix')

.addCondition('assignment_group','d505c5b387126990f95c1f0a7bbb3574') //industrial digital assignment group
.addOrCondition('assignment_group.u_team','IN',indGrps)
.addCondition('caller_id.department.business_unit','IN',indBU)
.addCondition('u_incident_type','IN',incTypes)
.addCondition('cmdb_ci','IN',incCI).addOrCondition('cmdb_ci.name','STARTSWITH','Salesforce')

 

Thanks in advance!

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

I don't think you're going to get there with addQuery due to the combination of ANDs and ORs.  Here's an alternate approach that will work, unless your number of incidents causes the list to load prohibitively slow.  The idea is to use your encoded query in a GlideRecord on the incident table.  This normally wouldn't be necessary since the before query BR is doing just that, but since it's giving you the correct results, and you can't use them in the list then we'll re-create the list.  You can break up the qry assignment to use your script variables in place of hard-coding sys_ids, like you did with caller_id.  The key to this working is that in the While block of the returned query results you push each sys_id to a previously-defined array: 

while (grInc.next()) {
    incArr.push(grInc.sys_id.toString());
}

 Then you add the array to the 'current' query:

current.addQuery('sys_id', 'IN', incArr.join(','));

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

I don't think you're going to get there with addQuery due to the combination of ANDs and ORs.  Here's an alternate approach that will work, unless your number of incidents causes the list to load prohibitively slow.  The idea is to use your encoded query in a GlideRecord on the incident table.  This normally wouldn't be necessary since the before query BR is doing just that, but since it's giving you the correct results, and you can't use them in the list then we'll re-create the list.  You can break up the qry assignment to use your script variables in place of hard-coding sys_ids, like you did with caller_id.  The key to this working is that in the While block of the returned query results you push each sys_id to a previously-defined array: 

while (grInc.next()) {
    incArr.push(grInc.sys_id.toString());
}

 Then you add the array to the 'current' query:

current.addQuery('sys_id', 'IN', incArr.join(','));

Hi @Brad Bowman  ,

 

It worked! Thank you!