Move the state from Scheduled to New in change management

Prasnajeet1
Giga Guru

Dear Expert

 

I have a requirement to move the change state from scheduled to New if user click on the button "Revert to New" and change state will move to new and whatever impacted service was there previously in the change ticket will removed from the related list. Also if any task is in closed state, then that should be open .  I am not sure how to start with the requirement and what type of script i have to write here. I found a UI action "Revert to New" but this is appearing only in Assess state and it is only moving the change ticket state from Assess to New not from Authorize to new or scheduled to new.

 

Can some please help with the logic and if possible script as well.

Thank in advanced

2 ACCEPTED SOLUTIONS

Hi Prasnajeet,

If you are looking to implement this change for normal changes then yes you are looking at the right ChangeRequestStateModel script.  You will also find scripts that end with _standard, _emergency, etc.

 

As far as the rollbackChange function is concerned, the way that I have that set up it needs to be part of a script include.  The name of the script include is irrelevant.  Just change the line

var changeFunctions = new IntegraChangeScripts();

To have the name of your script instead of IntegraChangeScripts (don't forget to include the parentheses after the name).  If you are putting the function into an existing script include make sure that the Client Callable box for that script is checked.  I set up a demo script include so that you can see how things should look.

var DemoScriptInclude = Class.create();
DemoScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    rollbackChange: function(theChangeNumber) {
        var theChange = new GlideRecord("change_request");
        theChange.get("number", theChangeNumber);
        theChange.setValue("state", -5);
        theChange.update();

        var gLock = new GlideRecordLock(theChange);
        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }

        var theApprovals = new GlideRecord("sysapproval_approver");
        theApprovals.addQuery("sysapproval", theChange.sys_id);
        theApprovals.query();
        while (theApprovals.next()) {
            theApprovals.setValue("state", "not_required");
            theApprovals.update();
        }

        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }
    },
    type: 'DemoScriptInclude'
});

One other thing to be sure to do is have the comma after the closing curly brace at the end of the function.  If you leave that out and don't know to look for it, debugging will drive you crazy.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

Hi Prasnajeet,

 

The error that you are getting is the ServiceNow way of saying that you haven't set up Scheduled to New as an allowable move in the script include ChangeRequestStateModel_standard.

 

As far as limiting the states under which the UI action is visible, you need to update the Condition box in the UI Action definition to include:

current.state == -4 || current.state == -3 || current.state == -2

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

6 REPLIES 6

johnfeist
Mega Sage
Mega Sage

Hi Prasnajeet,

Change is a tricky one to work with.  You need to keep in mind that the definition of states being able to transition is defined on a change type basis.  These definitions are found in Script Includes with names starting out ChangeRequestStateModel.  So if you have a change in a state other than Assess and moving a change of that type from the current state back to new is not defined, the state change will not happen.

 

When setting up so that changes could be reverted from multiple stages, I made some changes to the OOTB UI Action.  The script now looks like this:

revertToNew(current);
action.setRedirectURL(current);

function revertToNew(changeRequestGr) {
    var changeFunctions = new IntegraChangeScripts();
    changeFunctions.rollbackChange(changeRequestGr.number);
    gs.sleep(1000);
    changeFunctions.rollbackChange(changeRequestGr.number);
}

Here's the function that gets called in that script

  rollbackChange: function(theChangeNumber) {
        var theChange = new GlideRecord("change_request");
        theChange.get("number", theChangeNumber);
        theChange.setValue("state", -5);
        theChange.update();

        var gLock = new GlideRecordLock(theChange);
        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }

        var theApprovals = new GlideRecord("sysapproval_approver");
        theApprovals.addQuery("sysapproval", theChange.sys_id);
        theApprovals.query();
        while (theApprovals.next()) {
            theApprovals.setValue("state", "not_required");
            theApprovals.update();
        }

        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }
    },

There are  multiple calls and waits due to some timing issues that we encountered.

 

You will notice that the script is setting any approvals on that change to no longer required.  You can use a similar logic to query change_task for any tasks that are part of the change and reset the state by looping through the query.  The logic is the same for impacted services.

 

 

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

HI Jhonfiest

 

Thank you very much for quick response. I search the script include with the name you mentioned that is "ChangeRequestStateModel" I got little different script include that is "ChangeRequestStateModel_normal".

 

The modified the UI action script same as your code script but the function part script which you have mentioned, where I will put. In the script include or in the UI action only. In case of script include, where I  have to put it. Actually I am not so strong in script so looking for some more support here.

Hi Prasnajeet,

If you are looking to implement this change for normal changes then yes you are looking at the right ChangeRequestStateModel script.  You will also find scripts that end with _standard, _emergency, etc.

 

As far as the rollbackChange function is concerned, the way that I have that set up it needs to be part of a script include.  The name of the script include is irrelevant.  Just change the line

var changeFunctions = new IntegraChangeScripts();

To have the name of your script instead of IntegraChangeScripts (don't forget to include the parentheses after the name).  If you are putting the function into an existing script include make sure that the Client Callable box for that script is checked.  I set up a demo script include so that you can see how things should look.

var DemoScriptInclude = Class.create();
DemoScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    rollbackChange: function(theChangeNumber) {
        var theChange = new GlideRecord("change_request");
        theChange.get("number", theChangeNumber);
        theChange.setValue("state", -5);
        theChange.update();

        var gLock = new GlideRecordLock(theChange);
        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }

        var theApprovals = new GlideRecord("sysapproval_approver");
        theApprovals.addQuery("sysapproval", theChange.sys_id);
        theApprovals.query();
        while (theApprovals.next()) {
            theApprovals.setValue("state", "not_required");
            theApprovals.update();
        }

        gLock.setSpinWait(50);
        if (gLock.get()) {
            new Workflow().restartWorkflow(theChange, false);
        }
    },
    type: 'DemoScriptInclude'
});

One other thing to be sure to do is have the comma after the closing curly brace at the end of the function.  If you leave that out and don't know to look for it, debugging will drive you crazy.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hi Johnfiest

 

Thanks for supporting me. 

My requirement is also to implement this logic for change type as STANDARD, EMERGENCY, NORMAL and URGENT and the UI action "revert new" I have to visible in ASSESS, AUTHORIZATION and SCHEDULED state. Above script is working for normal change and emergency change but when working standard change I am getting below error message and it is now allowing me to change the state to New.

"Change model 'Standard' prevented state transition from Scheduled to New".

I think I am very close to my requirement. Only two thing i have to implement now. First I have to visible the UI action in ASSESS, AUTHORIZED and SCHEDULED only and above code should work for change type standard and urgent as well. Please suggest how I can do that.