Conditional Scheduled imports

Ganesh291
Tera Contributor

Hello,,

I'm working on Scheduled imports and I want to execute the scheduled import based on condition, can some one share the sample script to evaluate true or false(below image), I want to write a

Script like below (Example condition like for all P1 incidents this scheduled import will be execute), Can I do like this? please share your ideas.

var gr = new GlideRecord('incident');

gr.addQuery('priority',1);

gr.query();

while(gr.next()){

answer = true;

}

find_real_file.png

Regards,

Ganesh.

7 REPLIES 7

Patrick DeCarl1
ServiceNow Employee
ServiceNow Employee

Genesh,



Here is an example script for condition.



//Return 'true' to run the job


var answer = false;




//Get the day of month.


var now = new GlideDateTime();




//Run only on 2nd of month


if(now.getDayOfMonthLocalTime() == 2){


    answer = true;


}




answer;




The condition is not going to have access to the data your importing since it hasn't been imported if that's what you're trying to do with your example.



From Docs.


ConditionWrite the script to be used to evaluate whether an import should run. This field is visible if you selected the Conditional check box.

Thanks for response Patrick. That sample will help me.



Can I do this like below.



var ga = ' ';


var gr = new GlideRecord('incident');


gr.addQuery('priority',1);


gr.query();


while(gr.next()){


ga = 'move';


}


if(ga=='move'){


answer = true;


}else{


answer = false;


}


If you do a GlideRecord query and a while loop, you could be there a while as your list of records grows. You really only want to know if there 1 record that matches the condition.



Something like this might be more efficient than getting ALL incidents, you really only need 1.



(function () {


        var inc = new GlideRecord('incident');


        inc.setLimit(1);


        inc.addQuery('priority', 1);


        inc.query();


        if (inc.next()) {


                  return true;


        }



        return false;


})();


It might need to be answer vs return.