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


If you have any questions, please ask here.
Thanks in advance.