Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Once the sc task state is close completed, then the respective ritm will get to close complete.

Srilatha Vadlam
Tera Contributor

Once the request is submitted for a catalog item, the associated Request Item (RITM) has one Service Catalog Task (SC Task). When the SC Task state changes to Closed Complete, the corresponding RITM should also move to Closed Complete.
To close the SC Task, we use the "Close Task" button, which is implemented through a workflow for that catalog item.

Currently, after the request is submitted, I see activities in the workflow overview as shown in the attached image. However, the expected behavior is that all activity comments should appear before the Request Item is marked as Closed Complete. I have attached another image for reference.

 

 

UI Action (button script):condition-"current.state < 3 && current.approval != 'requested'",onclick: changeToEdit()

//Client-side 'onclick' function
function changeToEdit() { // this is function called on the "onClick" field

    if (g_form.getValue('work_notes') == '') {
        var assignmentGroup = g_form.getValue('assignment_group');
        var reqItem = g_form.getValue('request_item');

        var reqItemGr = new GlideRecord('sc_req_item');
        reqItemGr.addQuery('cat_item.sys_id', '2ae447e337ada200ebe32ea843990ede');
        reqItemGr.addQuery('sys_id', reqItem);
        reqItemGr.query();

        if (reqItemGr.next()) {

            var groupsGr = new GlideRecord('u_service_category_mapping');
            groupsGr.addQuery('u_assignment_group_id', 'CONTAINS', assignmentGroup);
            groupsGr.query();
            console.log('groupsGR ' + groupsGr.next().toString());

            if (groupsGr.next()) {

                g_form.setMandatory('work_notes', true);
                alert("You have to write a Work Note comment to close this Task.");
                return false;
            }

        }
    }

    //STRY2683652 - Check implementation task
    var currTask = g_form.getUniqueValue();
    var currRitm = g_form.getValue('request_item');
    var taskMappingGR = new GlideRecord('x_tsigh_ts_connect_task_mapping');
    taskMappingGR.addQuery('task_id', currTask);
    taskMappingGR.addQuery('implementation_task', true);
    taskMappingGR.query();
    if (taskMappingGR.next()) {
        var ga = new GlideAjax('x_tsigh_ts_connect.changeTaskMappingAjax');
        ga.addParam('sysparm_name', 'getChangeTask');
        ga.addParam('sysparm_number', g_form.getUniqueValue());
        ga.getXMLWait();
        var res = ga.getAnswer();
        if (res == 'true') {
            var url = 'com.glideapp.servicecatalog_cat_item_view.do?sysparm_id=bb8bca98131fef005130206ea67441b0' + '&sysparm_type=close' + '&sysparm_ritm=' + currRitm + '&sysparm_task_id=' + currTask;
            window.open(url);
        } else {
            alert(getMessage('Implementation Task Msg'));
        }
    }

    //  
    var missingMandatory = g_form.getMissingFields();
    if (missingMandatory.length > 0) {
        alert("The following mandatory fields are not filled in: " + missingMandatory.join(', '));
        return false;
    } else {
        gsftSubmit(null, g_form.getFormElement(), 'changeToEditAction'); // this refers to the "Action name"
    }

    //STRY2683652 - Check implementation task
    if (res == 'false') {
        g_form.setValue('state', 3);
        //Call the UI Action and skip the 'onclick' function
        gsftSubmit(null, g_form.getFormElement(), 'changeToEditAction');
    }

}

//Code that runs without 'onclick'
if (typeof window == 'undefined')
    setTaskState(); // call Server code

//Server-side
function setTaskState() {
    if (current.close_notes == '') {
        current.state = 3;
        current.update();
    } else {
        //Get the RITM of the current Catalog Task
        var ritm = current.request_item;

        var count = new GlideAggregate('sc_task');
        count.addAggregate('COUNT');
        count.addQuery('request_item', ritm);
        count.addQuery('active', true);
        count.query();
        if (count.next()) {
            var number = count.getAggregate('COUNT');
            //Check if this is the last task
            if (number >= 2) {
                //just close this one
                current.state = 3;
                current.update();
            } else {
                //Update the RITM so the workflow will continue
                var reqItem = new GlideRecord('sc_req_item');
                reqItem.get(ritm); //sctask number

                // current.state = 3;//sctask state=complte
                // current.update();
                if (reqItem.cat_item == 'b27ca47b37eeb600ebe32ea843990e7e') {
                    reqItem.work_notes = "Close notes" + ":\n" + reqItem.variables.adidas_dataprivacy_shadow_closenotes;
                    reqItem.work_notes = "All tasks were closed. Fulfillment completed";
                    reqItem.close_notes = reqItem.variables.adidas_dataprivacy_shadow_closenotes;
                    reqItem.update();
                }
                //  else if(reqItem.cat_item == '0fa6622cdbbef7401d398edf4b96197f'){
                //  //Do nothing
                //  //reqItem.work_notes = "test";
                // }
                else {
                    reqItem.work_notes = "Close notes from final task " + current.number + ":\n" + current.close_notes; //2
                    reqItem.work_notes = "All tasks were closed. Fulfillment completed"; //1
                    //reqItem.work_notes = "debug: " + reqItem.cat_item;
                    reqItem.close_notes = current.close_notes;
                    reqItem.update();
                    // current.state = 3;
                    // current.update();
                }

            }
            //  current.state = 3;
            //  current.update();
        }

        action.setRedirectURL(current);
    }
}
workflow:
In the work flow: Over view, subflow 
SrilathaVadlam_0-1764943798572.pngSrilathaVadlam_1-1764943837142.png

 


Once the request is submitted, I am getting activities like this in the below image. but actually, behavior is all acitivite comments should come before the request item is closed completed. I have attached one more image for your reference.

SrilathaVadlam_3-1764944164445.png

SrilathaVadlam_5-1764944320716.pngSrilathaVadlam_6-1764944537622.png

 

If you have any questions, please ask here.

Thanks in advance.

 



0 REPLIES 0