On Change Client script error.

Lucky1
Tera Guru

Hi all,

 

I have created an On change Client script on Incident form, on the field 'Severity'.

I have just written alert(cmdb_ci');

 

So, on change of Severity field, it should show me alert. Right?

But it is showing error like below: 

Lucky1_0-1715944056130.png

 

 

Please help

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @Lucky1,

 

There's a few things going on under the scenes that you may not be aware of.

 

First, to retrieve a field value in a Client script you need to leverage the g_form API such as g_form.getValue('field_name'). In addition, when you're trying to retrieve a reference field (which CI is), you need a further step as below - g_form.getDisplayBox('field_name').value - see below as to how to use this.

 

Use the below onChange Client Script.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

 

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

    var ciDisplayVal = g_form.getDisplayBox('cmdb_ci').value;
    alert('Ci val: ' + ciDisplayVal);

}

 

 

 

 

 

Screenshot 2024-05-17 at 12.33.34.png

View solution in original post

5 REPLIES 5

karthiknagaramu
Kilo Sage

Hi,

 

If you want to display the value of the cmdb_ci field please try

alert(g_form.getValue('cmdb_ci'));

 

Regards,

Karthik

Robbie
Kilo Patron
Kilo Patron

Hi @Lucky1,

 

There's a few things going on under the scenes that you may not be aware of.

 

First, to retrieve a field value in a Client script you need to leverage the g_form API such as g_form.getValue('field_name'). In addition, when you're trying to retrieve a reference field (which CI is), you need a further step as below - g_form.getDisplayBox('field_name').value - see below as to how to use this.

 

Use the below onChange Client Script.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

 

 

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

    var ciDisplayVal = g_form.getDisplayBox('cmdb_ci').value;
    alert('Ci val: ' + ciDisplayVal);

}

 

 

 

 

 

Screenshot 2024-05-17 at 12.33.34.png

Hello Robbie,

 

Thanks a lot. It's working

 

 

Regards,

Lucky

Hi @Lucky1,

 

Can you remove the brackets at the end of the string 'value' as shown below.

 

It should end with .value;

It should not end with .value() 

 

It's a subtle difference but make the difference. See my screen shot and initial response again.

 

Screenshot 2024-05-20 at 15.20.53.png

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie