How do I write UI action script which changes the state of a Request?

lightblu
Mega Expert

Hi all,

I would like some documentation/help regarding ServiceNow StateFlow scripts. The script below is executing on the "Ready for Work" UI action for a default Request. It is changing the state from Approved (13) to Ready(10). I do not understand how the script changes the state from Approved (13) to Ready (10): It seems to me like the 32 character GUID/sys_id is one for an instance of the Ready (10) state, but I have no idea how to tell that it is for the Ready (10) state.

find_real_file.png

I ultimately would like to write the script for a custom UI action which changes the current state to a new state, exactly like the UI action above is doing. But I have no idea how to specify how to specify the right GUID for the new state.

Thanks!

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi,



The sys_id in the state flow is referencing a specific state flow definition.



State Flows - ServiceNow Wiki



If you want to write your own UI action, de-activate this one and use the condition field to say "When would I like this to appear on the form" and the script to say "What would I like to do when the user clicks it". If you want it to appear when the state is 10 and change the state to 13, it would look as simple as this:



Condition: current.state == '10'


Script:


current.state = 13;


current.update();


action.setRedirectURL(current); // stay on this form



UI Actions - ServiceNow Wiki


View solution in original post

8 REPLIES 8

Hi,



The API reference is all found on developer.servicenow.com.



ServiceNow Developer


Click Developers (on the left)


Then a menu choice on the top left says API. Take your pick.


Thanks!



I'm guessing in my original question, "current" was a record/instance of Request. Where do I find that in the API? I see mostly Glide-whatever stuff.


Current refers to the record you are working on with it's current values. It is most often a GlideRecord instance (e.g. an incident record or a user record.) The object is available in many server side scripts (business rules, UI actions, workflow scripts, etc.)



Server side scripting fundamentals


Hi Chuck,



Hope you can help me.


i used scripts in "4.1 Defining the Trigger" Displaying a Custom Dialog - ServiceNow Wiki



When i reopen a hr case , add comment and save , it's changing the state from Resolved to "work in progress" and saving the comment from pop up box in the description field. However, when i impersonate an end user and tried to reopen a case, its just updating the comment but not changing the state to work in progress. Is it due to my admin access?


When i add "new StateFlow().processFlow(current, 'c77bfec04f682200143327201310c7d7', 'manual'); "   to client script, above " g_form.setValue("state", '18');", its not doing anything , not even updating the input comment into description field.


Is there any way to use this script "new StateFlow().processFlow(current, 'c77bfec04f682200143327201310c7d7', " 'manual'); in client script?



find_real_file.png



in the client script :-



function validateComments() {


    //This script is called when the user clicks "OK" in the dialog window


    //Make sure there are comments to submit


    var comments = gel("dialog_comments").value;


    comments = trim(comments);


    if (comments == "") {


          //If comments are empty, alert the user and stop submission


          alert("Please enter your comments before submitting.");


          return false;


    }



    //If there are comments, close the dialog window and submit them


    GlideDialogWindow.get().destroy(); //Close the dialog window


    g_form.setValue("description", comments); //Set the "description" field with comments in the dialog


    g_form.setValue("state", '18');


  g_form.save();


}