Validate the State from Script Include (oob one)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello All,
I want to validate the State value given in the JSON payload with the oob script include.
Scenario:
Third-party sends an JSON payload containing the state value of a Change request.
Now, before updating the change request with the 'State' value they sent. I need to validate the state transitions.
For that, there is an oob Script include,
ChangeRequestStateHandler.canMoveTo(toState) to validate if the given value is allowed.
Then if ok, I need to use ChangeRequestStateHandler.moveTo(toState)
So, how can I use them?
I understand I need to pass the state value they sent, in the function parameter. But need a bit clear response from you all.
Note:
There is already a script-include, I have written to validate some other date fields. And I want to validate the State transitions as well here.
In my script include, I am using 'chg' to refer to change request that is present in the system.
And, u_state to use the state field value they sent.
Now, could someone please help me how to use it?
Regards,
Lucky
So, I was told to use changerequeststatehandler.
So, can sone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
add a method like this inside your existing SI
validateAndMoveState: function(chg, u_state) {
if (!chg || !chg.isValidRecord()) {
return { success: false, message: "Change request not found." };
}
// Explicit checks allow a valid state value of 0.
if (u_state === null || u_state === undefined ||
String(u_state).trim() === "") {
return { success: false, message: "State is required." };
}
var handler = new ChangeRequestStateHandler(chg);
// Convert the payload's internal value to the state name.
// Example: "-2" becomes "scheduled" in the standard OOB mapping.
var toState = handler.getStateName(String(u_state).trim());
if (!handler.canMoveTo(toState)) {
return {
success: false,
message: "Transition from " + chg.getValue("state") +
" to " + u_state + " is not allowed."
};
}
if (!handler.moveTo(toState)) {
return {
success: false,
message: "Unable to perform the state transition."
};
}
// moveTo prepares the change on chg; the caller saves it.
return { success: true, message: "State transition accepted." };
},
call it from existing pocessing method like this
var result = this.validateAndMoveState(chg, u_state);
if (!result.success) {
return result; // Stop processing; do not update the change.
}
// Save the state and any other validated field changes together.
var updatedSysId = chg.update();
if (!updatedSysId) {
return { success: false, message: "Change request update failed." };
}
return { success: true, message: "Change request updated." };
