Skip to main content

Writing to a script processing table

Note: see my more recent update on this topic here
---

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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object