Set request stage as closed incomplete and closed skipped based on rim state
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2023 01:55 PM - edited ‎03-27-2023 02:34 PM
I have question
I have created run script in workflow which will set state value on request based ritm state. I,e if ritm state is closed incomplete request state as closed incomplete, if there is two ritms and if one completed and second one incomplete d req state will be partially completed ( custom state). My issue the stage remain always closed complete regard less of req state.
I want to set stage field on request form closed incomplete when state becomes incomplete. Closed skipped when state is closed skipped.
Below is RUN SCRIPT IS USED
updateRITMstatusandReq();
function updateRITMstatusandReq() {
var ritms_count = new GlideRecord("sc_req_item");
ritms_count.addQuery("request", current.sys_id); //get all RITMs associated with this request
ritms_count.query();
while (ritms_count.next()) {
//set the RITM status based on task
var ritm_status = setRITMstatusBasedOnTask(ritms_count.getUniqueValue());
if (ritm_status == '3') { //If Tasks are complete, then set RITM state and stage as Complete.
ritms_count.state = ritm_status; //set the RITM status based on task
ritms_count.stage = 'Completed'; //new step to set the stage
ritms_count.update(); //update the RITM status
} else if (ritm_status == "no_tasks") {
//do nothing, let the RITM values remain as it is.
} else {
ritms_count.state = ritm_status; //set the RITM status based on task
ritms_count.stage = 'Request Cancelled'; //set the stage as "Request Cancelled"
ritms_count.update(); //update the RITM status
}
}
//Once all RITMs status are updated, the call below Function to update the Req status based on RITM
updateReqStatus();
gs.info("REQ Workflow completed");
}
function updateReqStatus() {
//once all RITMs status are updated as per corresponding tasks, then update the Request status as per RITMs
var ritms = new GlideRecord("sc_req_item");
ritms.addQuery("request", current.sys_id); //get all RITMs associated with this request
ritms.query();
if (ritms.getRowCount() > '1') { //if there are more than 1 RITM associated then, check if any one RITM is 4 or 7 status
while (ritms.next()) {
if (ritms.state.toString() == '4' || ritms.state.toString() == '7') {
current.state = '8'; //partially completed as more than 1 RITM was present
current.stage = 'closed_complete';
return '';
}
}
current.state = '3'; //set to closed complete as no RITMs are in 4 or 7 status
current.stage = 'closed_complete';
return '';
} else {
//if RITMs count is 1, the set REQ status same as RITM
if (ritms.next()) {
current.state = ritms.state; //Request status same as RITM status
current.stage = 'closed_complete';
return '';
}
}
}
function setRITMstatusBasedOnTask(ritm_id) {
var ritm_status_temp = '3'; //defualt Closed Complete Status
//check for corresponding task status.
var catalog_task_count = new GlideRecord("sc_task");
catalog_task_count.addQuery("parent", ritm_id);
catalog_task_count.query();
if (catalog_task_count.getRowCount().toString() > '0') { //check if RITM has catalog task
while (catalog_task_count.next()) {
if (catalog_task_count.state.toString() == '4' || catalog_task_count.state.toString() == '7') { //If any one of the catalog task is closed Incomplete or Closed skipped, then return to set RITM status as 4 or 7
ritm_status_temp = catalog_task_count.state; //set the task status to RITM
return ritm_status_temp;
}
}
return ritm_status_temp;
} else {
//RITM does not have any catalog tasks associated
return "no_tasks";
}
}
and I have also created script include which I want change as well
var SetReqStatusScript = Class.create();
SetReqStatusScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {
updateRITMstatus: function () {
var ritms_count = new GlideRecord("sc_req_item");
ritms_count.addQuery("request", current.sys_id); //get all RITMs associated with this request
ritms_count.query();
while (ritms_count.next()) {
//set the RITM status based on task
var ritm_status = this.setRITMstatusBasedOnTask(ritms_count.getUniqueValue());
if (ritm_status == '3') { //If Tasks are complete, then set RITM state and stage as Complete.
ritms_count.state = ritm_status; //set the RITM status based on task
ritms_count.stage = 'Completed'; //new step to set the stage
ritms_count.update(); //update the RITM status
}else{
ritms_count.state = ritm_status; //set the RITM status based on task
ritms_count.stage = 'Request Cancelled'; //set the stage as "Request Cancelled"
ritms_count.update(); //update the RITM status
}
}
//Call Function to update the Req status based on RITM
this.updateReqStatus();
},
updateReqStatus: function() {
//once all RITMs status are updated as per corresponding tasks, then update the Request status as per RITMs
var ritms = new GlideRecord("sc_req_item");
ritms.addQuery("request", current.sys_id); //get all RITMs associated with this request
ritms.query();
if (ritms.getRowCount() > '1') { //if there are more than RITM associated then, check if any one is 4 or 7 status
while (ritms.next()) {
if (ritms.state == '4' || ritms.state == '7') {
current.state = '8';//partially completed as more than 1 RITM was present
current.stage= 'closed_complete';
}
}
}else{
//if RITMs count is 1, the set REQ status same as RITM
if(ritms.next()){
current.state = ritms.state; //Request status same as RITM status
current.stage= 'closed_complete';
}
}
},
setRITMstatusBasedOnTask: function(ritm_id) {
var ritm_status_temp = '3'; //defualt Closed Complete Status
var ritm_stage_temp = 'Completed';
//check for corresponding task status.
var catalog_task_count = new GlideRecord("sc_task");
catalog_task_count.addQuery("parent", ritm_id);
catalog_task_count.query();
while (catalog_task_count.next()) {
if (catalog_task_count.state == '4' || catalog_task_count == '7') { //If any one of the catalog task is closed Incomplete or Closed skipped, then return to set RITM status as 4 or 7
ritm_status_temp = catalog_task_count.state;//the task status to RITM
//ritm_stage_temp == 'Request Cancelled';
return ritm_status_temp;
}
}
return ritm_status_temp;
},
type: 'SetReqStatusScript'
});