How do I put an OR condition in this script?

Wayne Richmond
Tera Guru

This is for a map page. I want to show active incidents report and incident reports that were closed no more than 120 ago. I've got it working using two sets of conditions, however, I'd like to combine them into one. Is there a way?

// Active Security Incidents

var gt = new GlideRecord("u_incident_report");

gt.query('active', true);

gt.query();

while(gt.next()) {

    var item = map.addItem(gt);

    item.latitude = gt.u_latitude;

    item.longitude = gt.u_longitude;

    item.dialog_title = gt.getDisplayValue();

    item.icon = "shop_icon.png";

    item.icon_width = "16";

    item.icon_height = "16";

}

// Closed Security Incidents not older than 120 days

var gr = new GlideRecord("u_incident_report");

gr.addQuery('closed_at', '>', gs.daysAgoStart(120));

gr.query();

while(gr.next()) {

    var item = map.addItem(gr);

    item.latitude = gr.u_latitude;

    item.longitude = gr.u_longitude;

    item.dialog_title = gr.getDisplayValue();

    item.icon = "shop_icon.png";

    item.icon_width = "16";

    item.icon_height = "16";

}

1 ACCEPTED SOLUTION

awessel
Kilo Guru

You can make use of the addOrCondition function. Here's how you'd do that:



gr.addQuery('active', true).addOrCondition('closed_at', '>', gs.daysAgoStart(120));



When using this function, it's important to keep in mind that this must be done to a query and not directly to the GlideRecord. That's why in my example above I called the function immediately after the addQuery function.


View solution in original post

9 REPLIES 9

Matthew Smith
Kilo Sage

Im new to scripting so like Matthew suggests above, I would use the addEcodedQuery function. If you are unsure how to do this then simlpy go to u_incident_report.list and use the condition builder to filter your list, once happy just right click the last condition (hihglight blue) and select copy query. It'll give you something to similar to this:



active=true^NQclosed_atRELATIVEGE@dayofweek@ago@120


Wayne Richmond
Tera Guru

Thanks Adam, this seems to work perfectly. I don't fully understand your explanation if I'm honest. I noticed you changed the variable to gs. in your example: gs.addQuery('active', true).addOrCondition('closed_at', '>', gs.daysAgoStart(120)); whereas I was using gr and gt. Is this related to what you're talking about or just a typo?


That was actually a typo. It should be gr. I'll fix that.


Let me try to better explain.



addQuery is a function of the GlideRecord class. So when you do gr = new GlideRecord that function is now available to the gr object.



The addOrCondition function is not a member of the GlideRecord class. It is a member of the QueryCondition class. That is the class of what is returned by the addQuery function. That is why you can call that function immediately after the addQuery function.



If you tried to do gr.addOrCondition you would get an error as that function is not a part of the GlideRecord class. Hope that helps. It's a little confusing if you're new to script and/or not familiar with object-oriented programming.


You can find more details on all of this by reviewing the GlideRecord API which can be found here. We're getting a little "inside baseball" but the detail is there if you're interested.