Business Rules for Requested Items and Catalog Tasks

sarahyelton
Tera Expert

Disclaimer . . . I am NOT a developer but need to perform some developer type tasks that I have no idea how to accomplish.  

I am currently configuring the Service Catalog for our Data Governance Office.   I need to create a few business rules, but am not familiar with scripting.   I need to script these rules since the changes need to be made in a different table from the condition.   We are using execution plans, not workflows.   Please let me know if you can provide me some scripts to try out.   I would be forever grateful!

I need to create a business rule that will close all catalog tasks if the requested item is cancelled.   I have a UI button to cancel the requested item and change the state to "Closed Incomplete".   I want the subsequent catalog tasks to also change to "Closed Incomplete".

I need to create another business rule that will change the state of the requested item to "Work In Progress" if any of the catalog tasks' state changes to "Work In Progress".   I already have a business rule set for updating the state to WIP on the catalog task.

I know that there is an existing business rule to close the requested item if the tasks are closed.   However, this only works if the tasks' state is updated to "Closed Complete" and the requested item is then set to "Closed Complete".   These are the conditions and values I would need to set to meet the office's business requirements:

*   Change the state on the requested item to "Closed Complete" if at least one task is set as "Closed Complete".   All others must be "Closed Skipped" or "Closed Incomplete".

*   Change the state on the requested item to "Closed Incomplete" if all tasks are marked "Closed Skipped".

I know this is a lot in one post, so I appreciate any help you can offer!

Sarah

9 REPLIES 9

Jagarnath . . . this works well except that it is changing the state of any pending/open/WIP task and then updating/closing the RITM if the user marks a task Closed Complete.   I do not want the script to update existing task states, just the parent based on the closure state of the task.   Is this a simple tweak?   Thank you so much!


You rock!   This worked.   Thanks so much!


oharel
Kilo Sage

To answer all of your conditions, including the other tasks states etc.:


changeStates();



function changeStates() {



  // Add your code here



  if(current.state.changesTo(2)) { //if task state changes to WIP


  changeRITMStateToWIP();



  } else if(current.state.changesTo(3)) { //if task state changes to Close Complete


  checkStateofOtherTasks1();



  } else if(current.state.changesTo(4)) { //if task state changes to Close Skipped


  checkStateofOtherTasks();


  }


}



  // change the state of the requested item to "Work In Progress" or Close Complete if any of the catalog tasks' state changes to "Work In Progress" or to Close Complete


function changeRITMStateToWIP() {


  var ritm = new GlideRecord('sc_req_item');


  ritm.addQuery('active', true);


  ritm.addQuery('sys_id', current.request_item);


  ritm.query();



  while(ritm.next()) {


  ritm.state = 2; //state of ritm is changed to WIP


  ritm.update();


  }


}


//If at least one task is set as "Closed Complete" and all others are "Closed Skipped" or "Closed Incomplete", change the state on the requested item to "Closed Complete"



function checkStateofOtherTasks1() {


  var task = new GlideRecord('sc_task');


  task.addQuery('active', true);


  task.addQuery('request_item', current.request_item);


  task.addQuery('state', 'NOT IN', '4,7'); //4 is Close Skipped, 7 is close incomplete


  task.query();



  var count = task.getRowCount();


  while(task.next()) {


  if(count == 0) {


  changeRITMStateToCloseComplete();


  } else if(count > 0) {


  //do nothing


  }


  }


}



function changeRITMStateToCloseComplete() {


  var ritm = new GlideRecord('sc_req_item');


  ritm.addQuery('active', true);


  ritm.addQuery('sys_id', current.request_item);


  ritm.query();



  while(ritm.next()) {


  ritm.state = 3; //state of ritm is Close Complete


  ritm.update();


  }


}



//If all tasks are marked "Closed Skipped", change the state on the requested item to "Closed Incomplete"


function checkStateofOtherTasks() {


  var task = new GlideRecord('sc_task');


  task.addQuery('active', true);


  task.addQuery('request_item', current.request_item);


  task.addQuery('state', 'NOT IN', '4'); //4 is Close Skipped


  task.query();



  var count = task.getRowCount();


  while(task.next()) {


  if(count == 0) {


  changeRITMStateToCloseIncomplete();


  } else if(count > 0) {


  //do nothing


  }


  }


}



function changeRITMStateToCloseIncomplete() {


  var task = new GlideRecord('sc_req_item');


  task.addQuery('active', true);


  task.addQuery('sys_id', current.request_item);


  task.query();



  while(task.next()) {


  task.state = 4; //state of task is Close Incomplete


  task.update();


  }


}


I recommend you fine tune it, for example: change the states to your states (other values, probably).


Let me know if you have any questions



Harel


Harel - I can include the entire script in one business rule, correct?


Yes, sure. Remember to change the states numbers and such to match your instance's.



harel


Make sure to mark as correct/helpful as needed...