Skip to main content

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


now have this working from an asynch, insert business rule


(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 ( 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 ( { 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 ( { 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 ( { 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');


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'" + 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!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object