Skip to main content

Update the request short description to the ritm short description, outside of workflow

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

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }