how to change state value while click on state button on form using script?

gopi25
Tera Contributor

I need code for changing state value while click on state inprogress button which is created on incident form

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hi Gopi,

 

Type UI Action in left navigator and Select System Definition > UI Actions 

Click New

Name - In Progress

Table - Incident

Form Button - Checked

Script

(function(){
    current.state = 2; // set the opened incident state to in progress
    current.update();	// update the opened incident to save the changed state
    gs.setRedirectUrl(current); //redirect back to open incident.
})();

Screenshot for reference

find_real_file.png

 

Please mark this accepted & helpful if it answered your question.

 

Thanks & Regards,
Sharjeel

 

Regards,
Muhammad

View solution in original post

3 REPLIES 3

MrMuhammad
Giga Sage

Hi Gopi,

 

Type UI Action in left navigator and Select System Definition > UI Actions 

Click New

Name - In Progress

Table - Incident

Form Button - Checked

Script

(function(){
    current.state = 2; // set the opened incident state to in progress
    current.update();	// update the opened incident to save the changed state
    gs.setRedirectUrl(current); //redirect back to open incident.
})();

Screenshot for reference

find_real_file.png

 

Please mark this accepted & helpful if it answered your question.

 

Thanks & Regards,
Sharjeel

 

Regards,
Muhammad

The SN Nerd
Giga Sage
Giga Sage

If you need to do it in real-time (to trigger UI Policy or Client Scripts for mandatory fields etc) it would be as follows:

Client = checked

Callable script:

setStateToInProgress

Script

function setStateToInProgress() {

   var inProgressValue = 2;
   g_form.setValue('state',inProgressValue );
   g_form.save(); // Let the UI Policies and client scripts do the work

}

I find doing it this way (rather than Server-side) makes for a better UX.

GlideForm API


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Paul,

I'm attempting a very similar UI Action.  As you point out, I want it to run Client-side so all of our UI Policies and etc are applied, but I can't seem to get it to work.  I click the newly created button and nothing happens.   Any thoughts on what I might be doing wrong?

find_real_file.png