Client script - on change

Lidi2115
Tera Contributor

Hi to everyone.

I'm trying to create a script client that emits an alert when the availability field record changes to reserved.

Can someone help me?

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var dis = g_form.getValue("availability");
    if (newValue == "reserved") {
        alert("Reserved room");
    }

}
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Lidi2115,

onChange() script is executed when the end-user opens the form or when the form is opened. It won't execute after the form is closed. That is, if the form is closed and flow designer changes the field, onChange() script won't execute.

If there is a need to get notified when a value is changed in Flow Designer, use notification to send email or send a message.

https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/administer/notification/reference...

There is a "Send Notification" step in Flow Designer.

https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/administer/flow-designer/referenc...

View solution in original post

12 REPLIES 12

Allen Andreas
Administrator
Administrator

Hello,

You'd need to select this field in the client script settings (the form) and then check the value for the availability field to ensure "reserved" is a possible choice.

You're declaring var dis, but then not using it anywhere?

So check the field values and see what newValue consist of, you can use an alert for that too for testing:

alert(newValue);

and make sure it's availability not u_availability...etc.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Allen,

 

alert(newValue);

It's showing up when I open the form.

I need it to appear after I save the form and the availability field is changed from empty to reserved by the flow I made.

Did I explain correctly?

Availability is a choice field, but the flow designer changes it to reserved automatically.

 

Hello,

Please share exactly what you have as you've only included a small piece. If this is executing when you  open the form then it's not an onChange client script that you're using or you've removed a portion of the onChange script that cover isLoading().

Help us help you by clearly specifying what you're trying to accomplish and what you have so far.

I don't understand what you mean when you say:

"I need it to appear after I save the form and the availability field is changed from empty to reserved by the flow I made."

then you say...

"Availability is a choice field, but the flow designer changes it to reserved automatically."

Your post doesn't make sense as the flow is executed at the server level after the update/save has happened and doesn't have access to the client (the user's browser) to then show an alert.

Please try and take a step back and let us know what the end goal is. Perhaps that's easier, instead of you telling us you need an onChange client script with an alert?

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

sachin_namjoshi
Kilo Patron
Kilo Patron

Update your script like below.

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var dis = g_form.getValue("availability");
    if (dis == "reserved") {
        alert("Reserved room");
    }

}

 

Regards,

Sachin