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

Hi Hitoshi,

 

The script only works if I change the availability field manually.
Both your script and others suggested by other colleagues.
Do you have any suggestions of what I can do?

@Lidi2115 As I've answered in the last reply, Flow Designer runs on the server after the request is submitted so it is not able up show a popup on the form running on the client pc.

As I've suggested, alternative is to use SMS notification to show an alert on the user's web page at the top in the menu that user can select to see the content.

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi,

         Below Script should work, if still not working you have to validate some points

1) if you want this pop up on Portal UI Type - Should be all in client script so pop will reflect on portal also,

2) Check dis Value in alert, then use that value in  if (dis == "value")   as there may be a chance backend value is different 

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

    var dis = g_form.getValue("availability");
     alert(dis); // for checking value in dis
    if (dis == "reserved") {
        alert("Reserved room");
    }

}

 

Kindly mark correct and helpful if applicable