UPDATE 3:
now have this working from an asynch, insert business rule
script:
(function executeRule(current, previous /*null when async*/) {
// Wait for few seconds
for (var i=0; i<10000000; i++); /*may be a better way to do a quick wait*
var parent_req = new GlideRecord('sc_request');
parent_req.get(current.request);
var other_req_items = new GlideRecord('sc_req_item');
other_req_items.addQuery('request', current.request);
other_req_items.addQuery('sys_id', '!=', current.sys_id);
other_req_items.query();
if (other_req_items.next())
parent_req.short_description = 'Multiple Request Items';
else
parent_req.short_description = current.short_description.toString();
parent_req.update();
})(current, previous);
UPDATE 2: this doesn't really seem to work. Instead, see my post "Write to script processing table (extended from DL) to execute stored gliderecord scripts"
ideally, this would be done within workflow. But if for some reason you didn't want to modify the wf...
UPDATE 1: I since managed to figure out that the issue was being caused by using .get within the business rule instead of .query and .next. Reverting to the latter appears to have resolved the issue
hence, a business rule before insert on the sc_request table with the following code seems to do the trick:
(function executeRule(current, previous /*null when async*/ ) {
var grRITM = new GlideRecord('sc_req_item');
grRITM.addQuery('request', current.sys_id);
grRITM.query();
if (grRITM.next()) {
current.short_description = grRITM.short_description;
}
})(current, previous);
below is a workaround solution I came up with, but would recommend against using. Illustrates principle of firing off an on-demand scheduled job which can be of interest, script actions seem to have certain limitations within the scope of an insert/update operation in that these seem to run asynch and I experienced some problems with them targetting the same record
---
older workaround:
the problem of doing this in business rules, avoiding circular updates of course, is that at the point the REQ record is being created, the RITM record appears to be held in memory and hence ritm.short_description will show as undefined
I got around it using a scheduled script execution from the business rule (after insert on REQ) with a 2 second delay:
(probably other/better ways to do this!)
Business rule - note, must be 'after insert' rule on sc_req_item not request:
var rec = new GlideRecord('sysauto_script');
rec.get('name', 'REQ_populate_short_description');
SncTriggerSynchronizer.executeNow(rec);
Scheduled job (on demand): (avoid gs.sleep)
//--IDEALLY, delete this and move the logic into the RITM workflow
gs.log('ENTRY::', 'schJob:REQ_populate_short_description');
do_sleep(2000);
function do_sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds) {
updateREQs();
break;
}
}
}
function updateREQs() {
var sQuery = 'sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()';
var grREQ = new GlideRecord('sc_request');
grREQ.addActiveQuery();
grREQ.addNullQuery('short_description');
grREQ.addEncodedQuery(sQuery);
grREQ.query();
var icount = 0;
while (grREQ.next()) {
var grRITM = new GlideRecord('sc_req_item');
grRITM.addActiveQuery();
grRITM.addQuery('request', grREQ.sys_id);
grRITM.addEncodedQuery(sQuery);
grRITM.query();
if (grRITM.getRowCount() == 1) {
if (grRITM.next()) {
gs.log('RITMs: ' + grRITM.getRowCount() + '; ' + grRITM.short_description + '; ' + grRITM.number, 'schJob:REQ_populate_short_description');
grREQ.short_description = grRITM.short_description;
grREQ.update();
gs.log('REQ SD updated: ' + grREQ.short_description + '; ' + grREQ.number, 'schJob:REQ_populate_short_description');
icount++;
}
}
}
}
gs.log('COMPLETE:: ' + icount + ' records updated', 'schJob:REQ_populate_short_description');
Comments
Post a Comment