Skip to main content

transform map to bulk close catalog tasks without sending notifications

Transform map to close ageing tasks

this transform will:

- close RITM
- cancel RITM workflow
- close associated sc_tasks
- close associated REQ (assuming no other open RITM)
- close associated open approvals

and will by-pass business rules and notifications

only runs if RITM is active and there are open tasks present

obviously requires thorough testing and UAT!

import sheet format (excel)

transform map

ensure 'run business rules' flag is unchecked

transform script

(function transformRow(source, target, map, log, isUpdate) {'trMap:BULK CLOSE sc task NO NOTIFICATIONS:: in main script... ' + source.u_request_item);

    if (action == "insert") {

        ignore = true;

        log.error('ACTION ABORTED: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: insert operation aborted, check number ' + source.u_request_item);


    //--make sure tasks are open before proceeding

    var grTasks = new GlideRecord('sc_task');


    grTasks.addQuery('request_item', target.sys_id);


    if (grTasks.getRowCount() < 1) {

        log.error('ACTION ABORTED: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: no active tasks on the RITM ' + source.u_request_item);


})(source, target, map, log, action === "update");

onbefore script

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {


if ( == false) {

        ignore = true;

        log.error('ACTION ABORTED: onbefore: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: target record is already closed ' + source.u_request_item);

    } else {



})(source, map, log, target);

function killWorkflows(RITM_sysid) {

    //--as run business rules is switched off to bypass notifications, need to shut down the workflow

    //--this will also prevent wf running / new tasks being created when the RITM is closed

    var grWF = new GlideRecord('wf_context');



    grWF.addQuery('id', RITM_sysid);


    while ( {


        grWF.state = 'cancelled'; = false;

        var cancel_sysid = grWF.update();
'onbefore script: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: cancelled workflow context ' + cancel_sysid + '; ' + target.number);




function kill_executing_activities(wf_context){

var grWFA= new GlideRecord('wf_executing');


grWFA.addQuery('context', wf_context);

grWFA.addQuery('state', 'waiting');


while ({


var cancel_sysid= grWFA.update();'onbefore script: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: cancelled workflow executing activity ' + cancel_sysid + '; ' + target.number);



onafter script

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

    //--close any related records:

    var sSourceCN = source.u_close_note.toString();

    var sCloseNote = "sc task bulk closed via transform map [BULK CLOSE sc task NO NOTIFICATIONS]";

    if (sSourceCN != "") {

        sCloseNote = sSourceCN;


    var sys_id_ritm = target.sys_id;

    //--close any active tasks:

    closeTasks(sys_id_ritm, sCloseNote);

    //--close any active approvals:


    //--close parent REQ:

    closeREQ(target.sys_id, target.request);

    updateWorkNotes(sys_id_ritm, sCloseNote); //--for some reason, struggling to get the field map to work!

})(source, map, log, target);

function closeTasks(RITM_sysid, close_note) {

    var grTasks = new GlideRecord('sc_task');



    grTasks.addQuery('request_item', RITM_sysid);


    while ( {

        grTasks.close_notes = close_note;

        grTasks.state = "7"; //--closed cancelled = false;

'onafter script: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: task closed ' + grTasks.number + '; ' + target.number);



function closeREQ(RITM_sys_id, REQ_sysid) {

    //--only shut down the parent REQ if no other RITMs associated (bundled RITMs could still be open)

    var grRITMs = new GlideRecord('sc_req_item');


    grRITMs.addQuery('sys_id', '!=', RITM_sys_id);

    grRITMs.addQuery('request', REQ_sysid);


    if (grRITMs.getRowCount() < 1) {

        var grREQ = new GlideRecord('sc_request');


        grREQ.addQuery('sys_id', REQ_sysid);


        if ( {

            grREQ.request_state = 'closed_cancelled';

   = false;


  'onafter script: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: request closed ' + grREQ.number + '; ' + target.number);




function closeApprovals(RITM_sysid) {

    var grAppr = new GlideRecord('sysapproval_approver');



    grAppr.addQuery('sysapproval', RITM_sysid);

    grAppr.addQuery('state', 'requested');


    while ( {

        grAppr.state = 'cancelled'; = false;

'onafter script: trMap:BULK CLOSE sc task NO NOTIFICATIONS:: approval closed ' + grAppr.sys_id + '; ' + target.number);



function updateWorkNotes(RITM_sysid, work_note) {

    var grRITM = new GlideRecord('sc_req_item');

    if (grRITM.get('sys_id', RITM_sysid)) {

        //grRITM.comments='COMMENT: '+work_note;

        grRITM.work_notes = 'RITM closed via transform map, task close note: ' + work_note;





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