SncTriggerSynchronizer.executeNow(schedJob)

Naveen Velagapu
Mega Guru

hi all,

i am trying to execute a sceduled import from background using SncTriggerSynchronizer.executeNow(schedJob).

i am holding the return value of above in a variable and printing it

var schedJob = new GlideRecord('scheduled_import_set');

  if(schedJob.get('name', 'naveen_test')){

  var a =SncTriggerSynchronizer.executeNow(schedJob);

gs.print(a+'naveen');

}

each time it is printing different sys_id , but unable to identify to which table that sys_id belongs to.

i queried dynamically for all tables that have that sys_id , but i couldnt get any positive result.

any idea to which table that sys_id belongs to ???

20 REPLIES 20

In my case I didn't even had a variable to get the return value. I saw the example in the wiki and they don't do it either:


Special cases in job schedules



Telmo


Kalaiarasan Pus
Giga Sage

The sys id could belong to this table - sys_trigger.



When you execute any schedule job, a entry is added to this table. The record gets deleted once the job is executed.


So you need to be quick to check that table once you run that script.


Nope , that does not belong lo sys_trigger 🙂


Thanks Kalaiarasan, that was exactly what I needed.



In my case I wanted to run a scheduled report from a script, and then wait for the report to complete before moving on.   If anyone knows a way to run these synchronously please advise, but your tip did the trick.


In case it helps anyone else, here's the POC script I used to validate the concept of using sys_trigger to wait for my report to finish.



var rec = new GlideRecord('sysauto_report');


if (rec.get('name', 'Month end Time Card report')) {


  var trigger_id = SncTriggerSynchronizer.executeNow(rec);



  var gr = new GlideRecord("sys_trigger");


  var rows = 1;



  gr.addQuery('sys_id', trigger_id);



  do {


  gs.sleep(50);


  gr.query();


  rows = gr.getRowCount();


  gs.print(rows);


  } while (rows > 0);


}



Of course that's just a quick and dirty POC.   Here's a more fully baked version:


if (runReport('Month end Time Card report', 60000) > 0) {


    // paste your post-processing script here


    var gr = new GlideRecord("time_card");


    gr.addQuery('week_starts_on', "<", gs.beginningOfThisMonth());


    gr.addQuery('state', "Approved");


    gr.query();


    gs.print(gr.getRowCount());


    /*while (gr.next()) {


          gr.state = "Processed";


          gr.update();


    }*/


}






/* function runReport


* argument:


*     report_name:   name of the scheduled report to be run


*     time_out:         ms to wait for the report to run, before bailing


* returns:


*   -2   Report not found


*   -1   Unable to run report


*     0   timeout condition waiting for report to complete


*   >0   the number of ms it took to complete the report


*/




function runReport(report_name, time_out) {


    //gs.log("starting runReport");


    var report = new GlideRecord('sysauto_report');




    // Does the report exist?


    if (!report.get('name', report_name)) {


          // report not found.   return error condition


          gs.log("Report " + report_name + " not found.   Exiting...");


          return -2;


    }




    // Report exists.   Let's run it.


    var trigger_id; // get a handle on the scheduled report


    if (typeof SncTriggerSynchronizer != 'undefined')


          trigger_id = SncTriggerSynchronizer.executeNow(report);


    else


          trigger_id = Packages.com.snc.automation.TriggerSynchronizer.executeNow(report);




    // If the report didn't run, quit gracefully


    if (JSUtil.Nil(trigger_id)) {


          gs.log("Unable to run " + report_name + ".");


          return -1;


    }




    // Report is running.   Monitor progress


    var start_time = new Date();




    var gr = new GlideRecord("sys_trigger");


    gr.addQuery('sys_id', trigger_id)


    do {


          gs.sleep(500);


          gr.query();


    } while ((gr.getRowCount() > 0) && (new Date() - start_time < time_out));




    // Did we timeout waiting for completion?


    if ((new Date() - start_time >= time_out)) {


          gs.log("Timed out running " + report_name);


          return 0;


    }




    // Report ran successfully.   return runtime (ms)


    gs.log ("Report " + report_name + " ran in " + (new Date() - start_time) + " ms.");




    return (new Date() - start_time);


}


























Best regards,


Kevan


Naveen Velagapu
Mega Guru

ctomasi any idea on this ??