Writing to a script processing table

how it works:
- a business rule fires onafter
- a script is written in string format to a special table (scheduled script runner) which is then processed by manually executing a scheduled job following a small delay

Business rule script:

(function executeRule(current, previous /*null when async*/) {
    gs.log('sending attachment to FJ', 'busRule:Send Attachment to Vendor');
   
   
    //--queue a script to update the send flag back to false
    //--scheduling the update prevents interference with the file being sent  
        var GR_schScript = new GlideRecord('u_scheduled_script_runner');
        var s_script='';
        s_script=s_script+'var gr=new GlideRecord("u_task_integration_attachments");\r\n';
        s_script=s_script+'gr.addQuery("sys_id","' + current.sys_id + '");\r\n';
        s_script=s_script+'gr.query();\r\n';
        s_script=s_script+'if (gr.next()){\r\n';
        s_script=s_script+'gr.u_send_attachment = false;\r\n';
        s_script=s_script+'gr.u_attachment_sent = true;\r\n';
        s_script=s_script+'gr.update();\r\n';
        s_script=s_script+'}\r\n';
        GR_schScript.newRecord();
        GR_schScript.u_script_to_run=s_script;
        GR_schScript.u_record_table='u_task_integration_attachments';
        GR_schScript.u_record_sysid=current.sys_id;
        GR_schScript.u_script_to_execute=s_script;
        var sch_scr=GR_schScript.insert();
       
    gs.sleep(50);
    var rec = new GlideRecord('sysauto_script');
    rec.get('name', 'TidyUpTaskIntegrationsAttachments');
    SncTriggerSynchronizer.executeNow(rec);
   
        gs.addInfoMessage(gs.getMessage('attachment.send'));
    })(current, previous);


Scheduled job script:
//--DO NOT CHANGE THE NAME OF THIS SCHEDULED JOB! as executed on demand from business rule--//

var gr = new GlideRecord("u_scheduled_script_runner");
gr.addQuery("u_record_table" , "u_task_integration_attachments");
gr.addQuery("u_status", "pending");
gr.orderBy("u_sequence");
gr.query();
gs.log('START JOB: ROW COUNT: ' + gr.getRowCount(), 'schjob:_attach');
//--use arrays, as the gliderecord gets temperamental with glidescopedevaluator!
var arrSYSIDS_str='';
while (gr.next()){
    arrSYSIDS_str+= gr.sys_id+","; 
}
arrSYSIDS_str= arrSYSIDS_str.substring(0, arrSYSIDS_str.length - 1);
var arrSYSIDS=arrSYSIDS_str.split(',');

for (i=0;i<arrSYSIDS.length;i++){
   eval_script(arrSYSIDS[i]);
}
for (y=0;y<arrSYSIDS.length;y++){
   setRowProcessed(arrSYSIDS[y]);
}

//--tidy up:
var delQuery='u_status=processed^sys_created_on<javascript:gs.beginningOfLastMonth()';
var grDel = new GlideRecord("u_scheduled_script_runner");
grDel.addQuery(delQuery);
grDel.deleteMultiple();

gs.log('START JOB: ROW COUNT: ' + gr.getRowCount(), 'schjob:attach');

function eval_script(sysid){
  var gr = new GlideRecord("u_scheduled_script_runner");
  if (gr.get('sys_id', sysid)){
     var obj_evaluator = new GlideScopedEvaluator();
     var res=obj_evaluator.evaluateScript(gr, 'u_script_to_execute');
     //gs.print(res);
  }
}

function setRowProcessed(sysid){
    var gr = new GlideRecord("u_scheduled_script_runner");
    if (gr.get('sys_id', sysid)){
        gr.u_status='processed';
        gr.update();
    }
}

Script processing table(u_scheduled_script_runner):


note: 'script to execute' field is of type 'script'

Comments

Popular posts from this blog

Running transform maps asynchronously

Post a command to the ECC queue for the mid server to initiate a powershell file copy

GlideRecord setValue