We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

State flows for change request for Emergency change

Debasis Pati
Kilo Sage

Hello All,

So i want to restrict my emergency change should not move to assess state from new state if there are no related incident or problem or vr record present.

I want to achieve it through state flow is it possible?
If yes can you please guide @Ankur Bawiskar any idea?

 

10 REPLIES 10

Hello @Ankur Bawiskar ,
In state flows now we can write script just like business rule also i mentioned wrong in emergency we do not have assess we  have authorize state.

also please see the below
Chnage-state-flow.png

(function executeManualScript(current) {

    // Only apply to Emergency changes
    if (current.type != 'emergency') {
        return true;
    }

    var hasIncident = false;
    var hasProblem = false;
    var hasVR = false;

    // -------------------------------
    // INCIDENT CHECKS
    // -------------------------------

    // 1. Incidents linked via change_request
    var inc1 = new GlideRecord('incident');
    inc1.addQuery('change_request', current.sys_id);
    inc1.query();
    if (inc1.hasNext()) {
        hasIncident = true;
    }

    // 2. Incidents linked via caused_by_change
    var inc2 = new GlideRecord('incident');
    inc2.addQuery('caused_by_change', current.sys_id);
    inc2.query();
    if (inc2.hasNext()) {
        hasIncident = true;
    }

    // -------------------------------
    // PROBLEM CHECK
    // -------------------------------
    var prob = new GlideRecord('problem');
    prob.addQuery('change_request', current.sys_id);
    prob.query();
    if (prob.hasNext()) {
        hasProblem = true;
    }

    // -------------------------------
    // VR CHECK (Vulnerability Response)
    // -------------------------------
    var vr = new GlideRecord('sn_vul_vulnerability');
    vr.addQuery('change_request', current.sys_id);
    vr.query();
    if (vr.hasNext()) {
        hasVR = true;
    }

    // -------------------------------
    // BLOCK TRANSITION IF NONE FOUND
    // -------------------------------
    if (!hasIncident && !hasProblem && !hasVR) {
        gs.addErrorMessage("Emergency Change cannot move to Assess without a related Incident, Problem, or VR record.");
        return false;
    }

    return true;

})(current);

@Debasis Pati 

you should use script include function and then call it in Manual condition string

If that evaluates as true then that UI action will be seen

new ScriptInclude().functionName(current)

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar ,
Yes i do not want this button.
This is what my exact issue is when i created this state flow a ui action is visible on the form.
I do not need this actuyally.
with state flows why a ui action is getting created?

 

@Debasis Pati 

with State flow UI action is auto-created, it's OOTB behavior

Based on condition it will show/hide

For your requirement you can add manual condition and call script include function and make it return true/false

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Hello @Ankur Bawiskar ,
My state transition in change is not manual it is all automated state transition.
I want to put this restriction before it moves to authorize  state so thats why instead of manual i am thinking of writing the same in automatic.