Union Of conditions

suyoga
Tera Expert

Hey Folks,

 

I need help with creating union of condition, I have a relationships(Related List) on incident table which have criteria something like below:

 

current.addEncodedQuery('caller_id=' + parent.caller_id + '^ORu_behalf_of=' + parent.caller_id + '^ORcaller_id=' + parent.u_behalf_of + '^ORu_behalf_of=' + parent.u_behalf_of + '^sys_id!=' + parent.sys_id + '^sys_created_onONLast 60 days@javascript:gs.beginningOfLast60Days()@javascript:gs.endOfLast60Days()');  
 
Recommendation from servicenow team is to remodel this and have it in UNION and go away with OR condition(underlined) in above condition, I am looking for recommendations on how to go away with OR condition.
1 ACCEPTED SOLUTION

Hey @Ankur Bawiskar , I could not get that to work however I tried realigning of query and that helped reduce the timing, so new criteria looks something like below, this brought down timing to 1/3rd:

current.addEncodedQuery( 'sys_id!=' + parent.sys_id + '^sys_created_onONLast 60 days@javascript:gs.beginningOfLast60Days()@javascript:gs.endOfLast60Days()' + ('caller_id=' + parent.caller_id + '^ORu_behalf_of=' + parent.caller_id + '^ORcaller_id=' + parent.u_behalf_of + '^ORu_behalf_of=' + parent.u_behalf_of ));  

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@suyoga 

did they not provide the recommendation when you created the case with them?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  No we tried but was not able to resolve it, Engineer involved is expert in backend(server side and DB stuff), recommendation provided by them is to have it as UNION as instead of OR.

Ankur Bawiskar
Tera Patron
Tera Patron

@suyoga 

can you try this? I haven't tested it though.

var incidentIds = {}; // Use object for uniqueness

function addIncidents(query) {
    var gr = new GlideRecord('incident');
    gr.addEncodedQuery(query);
    gr.query();
    while (gr.next()) {
        incidentIds[gr.getValue('sys_id')] = true;
    }
}

var dateFilter = '^sys_created_onONLast 60 days@javascript:gs.beginningOfLast60Days()@javascript:gs.endOfLast60Days()';
var excludeSelf = '^sys_id!=' + parent.sys_id;

addIncidents('caller_id=' + parent.caller_id + excludeSelf + dateFilter);
addIncidents('u_behalf_of=' + parent.caller_id + excludeSelf + dateFilter);
addIncidents('caller_id=' + parent.u_behalf_of + excludeSelf + dateFilter);
addIncidents('u_behalf_of=' + parent.u_behalf_of + excludeSelf + dateFilter);


var sysIdList = Object.keys(incidentIds); // Array of unique sys_ids

if (sysIdList.length > 0) {
    current.addQuery('sys_id', 'IN', sysIdList.join(','));
} else {
    // Optionally, add a query that matches nothing if no sys_ids found
    current.addQuery('sys_id', ''); // This will return no records
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar  let me try and test