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

oharel
Kilo Sage

Hi Sarah,


Your last lines, changed:



//Code that runs without 'onclick'


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


if (typeof window == 'undefined')


deliverItemServer();



function deliverItemServer() {


action.setRedirectURL(current);


if(areApprovalsClosed() && areRelatedTasksClosed()) { //check the two functions before closing. If they return true, close the item


current.state = 9;


current.update();



function areApprovalsClosed() { //check there are no pending approvals


var approval = new GlideRecord('sysapproval_approver');


approval.addQuery('sysapproval', current.sys_id);


approval.query();



while(approval.next()) {


if(approval.state == 'requested') //add more states as needed


return false;


}



return true;


}




function areRelatedTasksClosed() { //return true if there are no open tasks.


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;


}



harel


Thanks for the reply, but this did not seem to work.



I have managed to get the state of the request item to Change the State to open if Customer visable comments are no entered.


Enter the customer visible content and select the Close Request item button again.


State then changes to closed. Perfect!



Now here is where the problem starts.


Reload the form and the state is still set to open. I'm not sure why. Can someone advise me whats causing this? From what I can see it is not updating. When I select a manual update the state still changes to Open and should be closed.



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


    g_form.setValue('state', 1); //Set state to open


          return false;//Abort submission


    }  


    else{


    g_form.setValue('state', 3);


  current.update();


    }


    //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();


   


}


Hi Sarah,



I tested your code and it seems fine.


A couple of comments:


1. You may want to remove g_form.setValue('state', 1);   because this will set the state to 1, not to its previous state. Also, you have return false; after it, so it will not set the state to 1 anyway I think.


2. You can also remove


      else{


      g_form.setValue('state', 3);


    current.update();


      }



because you're setting it on the server side. Also, current is not valid on the client side.


3. Last, add action.setRedirectURL(current); at the very end, if you want it to stay on the same page.



harel


Thanks,



When you reload the form does it stay in the closed state? Or go back to open? When I go to Open requests the RITM is still open, even though the form is saying closed.



Can you send me an example of what you meant in your last comments.



Here is what I have updated based on what you advised. We want to exit the form and go back to the list view.




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


//g_form.setValue('state', 1); //Set state to open


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();


}


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>