Prevent closure of RITM is catalog task is open and close if cancelled by customer

SMuir
Mega Guru

We have just started to use catalog task on some request items.

The problem we are having, is that the workflow changes the state as the process progresses which works fine.

But if a task is open and someone selects the Close Request Item UI Action ( Button) The RITM closes with the task still open.

After doing some research, I noticed a few other posts that I can't seem to get to work at all. I did try a business rule as mentioned in previous post but this does not appear to be working for me either.

Is there a way I can adjust the code of the button to check that the task is open and prompt the itil user to close the task that is assigned to them. If the customer closes the RITM, can the tasks be set to closed before the RITM is closed automatically via the workflow.

I am not sure how best to do this and my coding skills are pretty basic. Assistance if someone can help will be appreciated.

function closeRequestedItem(){

var   response = confirm('Are you sure you want to close this requested item?');

      if (!response)

          return false;

//Set the 'State' values to 'Closed', and display mandatory fields

    g_form.setValue('state', 3);

    if (g_form.getValue('comments') == '') {

          //Remove any existing field message, set comments mandatory, and show a new field message

          try {g_form.hideFieldMsg('comments');} catch(e) {}

          g_form.setMandatory('comments', true);

          g_form.showFieldMsg('comments','Please enter comments and reselect Close Requested Item','error');

          return false;   //Abort submission

    }

    //Call the UI Action and skip the 'onclick' function

    gsftSubmit(null, g_form.getFormElement(), 'close_requested_item'); //MUST call the 'Action name' set in this UI Action

}

//Code that runs without 'onclick'

//Ensure call to server-side function with no browser errors

if (typeof window == 'undefined')

    serverResolve();

function serverResolve(){

    current.state = 3;

    current.update();

   

}

1 ACCEPTED SOLUTION

Here goes (works as expected on my sc_req_item table):



function closeRequestedItem(){


var   response = confirm('Are you sure you want to close this requested item?');


      if (!response)


          return false;


//Set the 'State' values to 'Closed', and display mandatory fields


g_form.setValue('state', 3);


 


    if (g_form.getValue('comments') == '') {


          //Remove any existing field message, set comments mandatory, and show a new field message


          try {g_form.hideFieldMsg('comments');} catch(e) {}


          g_form.setMandatory('comments', true);


          g_form.addErrorMessage('Please enter customer visible comments and reselect Close Requested Item','error');


    //current.state = 1; //Set state to open --> commented this out


  // g_form.setValue('state', 1); //Set state to open --> this too


          return false;//Abort submission


    }


//       else{


//       g_form.setValue('state', 3);


//     current.update(); // --> this does not work on the client side


//       }


    //Call the UI Action and skip the 'onclick' function


    gsftSubmit(null, g_form.getFormElement(), 'close_requested_item'); //MUST call the 'Action name' set in this UI Action


}




//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if (typeof window == 'undefined')


    serverResolve();



function serverResolve(){


 


    current.state = 3;


    current.update();


}


//action.setRedirectURL(current); --> comment this out if you want to return to the records list



Do you have any BR running in the back, interfering with the update?



harel


View solution in original post

13 REPLIES 13

HI, thanks for getting back to me.



It still isn't working, I have raised a ticket with Support to see if we have something stopping the update.



I will let you know what the end result is.



Sarah Muir | IT ServiceNow Technician



Norton Rose Fulbright LLP


3 More London Riverside, London, SE1 2AQ, United Kingdom


Tel +44 20 7444 3835 | Mob +44 7738 808219 | Fax +44 20 7283 6500


sarah.muir@nortonrosefulbright.com


NORTON ROSE FULBRIGHT


Law around the world


nortonrosefulbright.com<http://www.nortonrosefulbright.com>



Norton Rose Fulbright and Chadbourne & Parke have joined forces, giving our clients access to more than 4,000 lawyers worldwide.


nortonrosefulbright.com/chadbourne<http://www.nortonrosefulbright.com/chadbourne>


Hi Oharel,



There was a business rule I created changing the state to open, which we wanted and still need. I removed the set state field State to open, leaving it blank.



Then I selected the Close Request item button and it worked.




Sarah Muir | IT ServiceNow Technician



Norton Rose Fulbright LLP


3 More London Riverside, London, SE1 2AQ, United Kingdom


Tel +44 20 7444 3835 | Mob +44 7738 808219 | Fax +44 20 7283 6500


sarah.muir@nortonrosefulbright.com


NORTON ROSE FULBRIGHT


Law around the world


nortonrosefulbright.com<http://www.nortonrosefulbright.com>



Norton Rose Fulbright and Chadbourne & Parke have joined forces, giving our clients access to more than 4,000 lawyers worldwide.


nortonrosefulbright.com/chadbourne<http://www.nortonrosefulbright.com/chadbourne>


Hi Sarah,



That's good to know. Mark you answer as correct and anything else as helpful (if anything was helpful), so it goes out of the unanswered questions list.



harel


Hi there,

 

I am trying to do something similar on a UI action where the user is unable to cancel the request if their are open tasks. But my UI action still overrides checking for tasks, so I attempted moving the check of related tasks up. Any ideas?

 

function areRelatedTasksClosed() {
		var task = new GlideRecord('sc_task');
		
		task.addQuery('request_item', current.sys_id);
		
		task.query();
		
		
		while(task.next()) {
			
			
			if(task.state != 3) //if the tasks are not closed - you can add more states as needed
				
			return false;
			
		}
		
		
		return true;
	}

function cancelRequest(){
	
	if(confirm('Are you sure you want to cancel your Request?')) {
		
		//Call the UI Action and skip the 'onclick' function
		gsftSubmit(null, g_form.getFormElement(), 'cancel_request');
		
	}
	
		return false;
		
	
}

//Code that runs without 'onclick'

//Ensure call to server-side function with no browser errors

if (typeof window == 'undefined')
	
updateTaskAndRITM();

function updateTaskAndRITM(){
	
	current.state = 4; //value for Closed incomplete
	current.work_notes = 'Request Cancelled';
	current.comments = 'This request been cancelled';
	current.update();
	
	var reqItem = new GlideRecord('sc_req_item');
	
	reqItem.get(current.request_item);
	
	reqItem.comments = 'Request Cancelled';
	
	reqItem.work_notes = 'Request Cancelled';
	
	reqItem.state = 4; //value for Closed incomplete
	
	reqItem.update();
	
	new UIActionUtils().approvalsNoLongerRequired(current.sys_id);
	
	current.stage = 'Completed';
	
}

 

I also need to change the workflow stage to completed, as it still sits in awaiting approval and can't get that to work either.

 

Any ideas would greatly be appreciated.

 

Cheers,

Anna