Close a Change Request by REST API

JakubN916150010
Tera Contributor

 

Hi guys,

 

I’m trying to close a Change Request via REST API and I’m struggling with state transition behaviour.

 

I’m using:
PATCH /api/sn_chg_rest/change/{sys_id}

 

Payload:
{
  "close_code": "successful",
  "close_notes": "Closed by API",
  "state": "3"
}

 

The API correctly updates close_code and close_notes, but the state does NOT move to Closed.


Anyone know how to move state to close with API?

Thanks,
Jakub 

3 REPLIES 3

Vijaya_Mnpram
Kilo Sage

@JakubN916150010  We can't move change in any state to Close. OOB, you need to check the 'State Transitions' allowed on the Change Model you are trying to close. 

For example, if there is a Change in 'Normal' Model, then the table (sys_state_model.list OR 'State Models' from All menu) will show you a Model entry for 'Normal'. 

https://<Your instance name>/sys_state_model_list.do?sysparm_query=nameLIKEnormal&sysparm_first_row=...

 

Inside this records, under related list 'State Transitions' you can find the possible movement of states from Enter to Exist. OOB, for the Change to move to Closed, it should be under 'Review' not from any state. 

https://<Your Instance Name>/sys_state_transition.do?sys_id=ddf86df57f000300666bdd620efa91a2&sysparm...

 

First you try to do this Patch update using 'REST API Explorer' in Servicnow. When all is good, go for API. 

Thanks, I understand the Change Model part.

 

State transitions work fine in my case – I can move the Change via API between allowed states (e.g. New -> Assess → Implement → Review).
The problem is only with the Closed state.

 

Directly setting state=Closed is correctly blocked by the Change Model if the Change is not in Review – that’s expected.

 

However, setting active=false via REST API still closes the Change.
In this case the state is changed implicitly by Task Active State Management, which bypasses the Change Model transitions.

 

So active=false works as a workaround, but it’s not a real solution because it circumvents the lifecycle rules.

 

What is the recommended way to enforce Change Model transitions when a Change should be closed via API?

@JakubN916150010  OOB the Business Rules doesn't update the State to 'Closed' even through an API but Active to 'false' can be done by API.

If you use, just the above playload you mentioned, then the Business Rule is forbidding it. 

 

Vijaya_Mnpram_0-1777048837796.png

What exactly are you looking for? You need to close the Changes with API, but it should also respect Models? 

If yes, then you use just the above playload and if the API is not table API, then you can enhance the error message with more details. 

If No, then setting Active to false should do the thing.