Skip to content

Instantly share code, notes, and snippets.

@cmcdevitt
Last active September 1, 2022 15:03
Show Gist options
  • Select an option

  • Save cmcdevitt/c1785b88b9f1e3411fb268d68b0a0752 to your computer and use it in GitHub Desktop.

Select an option

Save cmcdevitt/c1785b88b9f1e3411fb268d68b0a0752 to your computer and use it in GitHub Desktop.
Script Action :: Script Action Handler via Script Include
/*
==> (1) Script Action <==
Script Actions seem slow and difficult to troubleshoot
So.... Lets move the logic to a Script Include where we can simulate and troubleshoot a lot easer...
*//
//In Script Action example....
var util = new global.ChangeRequestUtil(current,event);// <== Pass in current and event as args when instantiating the SI
util.utilKnows(); //Then call methods without arguments becouse the SI already know about current and event
util.fooBar();
//...
/*
==> (2) Script Include <==
Make Current and Event available to the rest of the Methods via the initialize function
Move as much logic into the SI as possible
*/
var ChangeRequestUtil = Class.create();
ChangeRequestUtil.prototype = {
initialize: function(record,event) {
this.record = record; //Avaible to all methods
this.event = event; //Avaible to all methods
},
utilKnows: function(){
//Example
gs.info("CCCC utilKnows....");
gs.info("CCCC Record " + typeof(this.record) + " " + this.record.number );
gs.info("CCCC parm1 " + typeof(this.event) + " " + this.event.parm1 );
gs.info("CCCC parm2 " + typeof(this.event) + " " + this.event.parm2 );
},
updateCT: function() {
var q = 'change_request=' + this.record.sys_id; //sys_id
var parm_one = this.event.parm1.split(',');
var start_time = parm_one[0];
var end_time = parm_one[1];
var data_center = this.event.parm2;
var ct = new GlideRecord('change_task');
ct.addEncodedQuery(q);
ct.query();
while (ct.next()) {
ct.setValue('planned_start_date', start_time);
ct.setValue('planned_end_date', end_time);
ct.setValue('description', data_center);
ct.update();
}
},
type: 'ChangeRequestUtil'
};
// ==> Test via an Event <==
var parm1 = '2022-08-24 23:00:00" 2022-08-24 19:00:00,2022-08-25 23:00:00" 2022-08-24 19:00:00';
var parm2 = 'My Data Center';
var cr = new GlideRecord('change_request');
cr.get('c83c5e5347c12200e0ef563dbb9a7190');//CHG0040007
gs.eventQueue('change.request.automation',cr,parm1,parm2);
// ==> Test via a Sumulated 'Event' by calling SI directely <==
var events = {};
events.parm1 = '2022-08-24 23:00:00" 2022-08-24 19:00:00,2022-08-25 23:00:00" 2022-08-24 19:00:00';
events.parm2 = 'My Data Center';
var cr = new GlideRecord('change_request');
cr.get('c83c5e5347c12200e0ef563dbb9a7190');//CHG0040007
var util = new global.ChangeRequestUtil(cr,events);
util.updateCT();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment