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

kristenankeny
Tera Guru

I have a similar "cancel", though mine pulls up a UI page. You'll want to do a call to a script include, then in your client callable script include, you can do something like this:



cancelRITM: function(){


  var c = this.getParameter('sysparm_comment');


  var r = this.getParameter('sysparm_item');


  var ri = new GlideRecord('sc_req_item');


  ri.addQuery('sys_id',r);


  ri.query();


  if(ri.next()){


  ri.state = 7;


  ri.comments = c;


  ri.update();


  }


  var t = new GlideRecord('sc_task');


  t.addQuery('request_item',r);


  t.addQuery('state','IN','1,-5,2');


  t.query();


  while(t.next()){


  t.state = 7;


  t.update();


  var a = new GlideRecord('sysapproval_approver');


  a.addQuery('sysapproval',t.sys_id);


  a.addQuery('state','IN','requested,no requested');


  a.query();


  while(a.next()){


  a.state = 'not_required';


  a.update();


  }


  }


  var contextId = '';


  var wc = new GlideRecord('wf_context');


  wc.addQuery('id',r);


  wc.addQuery('parent','');


  wc.query();


  if(wc.next()){


  contextId = wc.sys_id.toString();


  }


  var ga = new WorkflowCancelKillRITM();


  ga.cancelContext(contextId);


  var adhoc = new GlideRecord('sysapproval_approver');


  adhoc.addQuery('sysapproval',r);


  adhoc.addQuery('state','IN','requested,no requested');


  adhoc.addQuery('u_approval_level','');


  adhoc.query();


  while(adhoc.next()){


  adhoc.state = 'not_required';


  adhoc.update();


  }


  },



This is my call to the script include:


var ajax = new GlideAjax('GNW_CatalogUtilsAJAX');


  ajax.addParam('sysparm_name','cancelRITM');


  ajax.addParam('sysparm_comment',comments);


  ajax.addParam('sysparm_item',ritm);


  ajax.getXML();



Earlier in the script, I set the variables "comments" and "ritm". "GNW_CatalogUtilsAJAX is my script include's name.


kristenankeny
Tera Guru

You can script something like this for the RITM state update (we do this in the workflow, though you could do through business rule as well - you would change the 'request_item' to look at the current task's ritm):



var tot = 0;


var comp = 0;


var incomp = 0;


var skip = 0;




var t = new GlideRecord('sc_task');


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


t.query();


while(t.next()){


  tot += 1;


  if(t.state == 3){


  comp += 1;


  }


  else if(t.state == 4){


  incomp += 1;


  }


  else if(t.state == 7){


  skip += 1;


  }


}


if(tot == comp){


  current.state = 3;


}


else if(tot == skip){


  current.state = 7;


}


else if(tot == (skip + comp)){


  current.state = 3;


}


else{


  current.state = 4;


}


current.update();


oharel
Kilo Sage

Hi Sarah,



A simple BR to cancel tasks when the RITM is cancelled would be:


Table: sc_req_item


When: after update


Condition: current.state == 4 //state cancel of the RITM


Script:


var task = new GlideRecord('sc_task');


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


task.addQuery('active', true);


task.query();



while(task.next()) {


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


  task.active = false;


  task.update();


}


As you are doing on catalog task table rather than requested item   table , you could bit little bit tricky. Use the below script in order to achieve your requirement .



This is an after update business rule.



var table=current.getTableName();


  var id=current.sys_id+"";


  if(current.state.changesTo(3)){


  closeritm(current.request_item.getRefRecord(),3);


  var gr= new GlideRecord(table);


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


  gr.addQuery('sys_id','!=',id);


  gr.query();



  while(gr.next()){


  //gs.addInfoMessage(gr.state);


  gr.state=4;


  gr.update();


  }


  }



  else if (current.state.changesTo(7)){



  var gr3= new GlideRecord(table);


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


  gr3.addQuery('sys_id','!=',id);


  gr3.addQuery('state', '!=', 7);


  gr3.query();


  if(gr3.getRowCount()==0){



  closeritm(current.request_item.getRefRecord(),4);



  }


  }



  function closeritm(ritm,state){


  ritm.state = state;


  ritm.update();


  }