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

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.


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?


Matthew Glenn
Kilo Sage

var gt = new GlideRecord("u_incident_report");


gt.addEncodedQuery("active=true^NQclosed_at>=javascript:gs.daysAgoStart(120)");


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";


}


This is how I'd seen it done previously but couldn't find an example to copy. Thanks