Clearing a variable once it's dependent variable changes not working

Amol Pawar
Tera Guru

Hi Experts,

I have a requirement for a record producer as mentioned below:

I have created two variables in a record producer. The first variable is a Reference type and the second variable is also a reference type variable. For the second variable, I'm using a custom table and a reference qualifier to populate values from the first variable's reference table.

When the first variable value is changed, the second variable value should also be changed. But the second variable's value is not changing. I1.png

 I have created an onChange client script but it's not working.

Let me know if I'm missing anything.

I2.png

 

Thanks in advance,

Amol

1 ACCEPTED SOLUTION

Pratiksha2
Mega Sage

Hello @Amol Pawar ,
Please try below code:

 function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
if (newValue) {
g_form.setValue('infosys_entity ', ' ');
}
}

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Pratiksha

View solution in original post

5 REPLIES 5

Harish KM
Kilo Patron
Kilo Patron

Hi @Amol Pawar 

since it is reference field clearValue will not work, you need to use setValue

use g_form.setValue('fieldname',''); // clear the value

 

additional note you can place this line inside if at line 2

 if (isLoading || newValue === '') {
     

g_form.setValue('fieldname',''); // clear the value when first variable is empty

 

   }
Regards
Harish

Pratiksha2
Mega Sage

Hello @Amol Pawar ,
Please try below code:

 function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
if (newValue) {
g_form.setValue('infosys_entity ', ' ');
}
}

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Pratiksha

Aniket Chavan
Tera Sage
Tera Sage

Hello @Amol Pawar ,

When working with reference-type variables, it's recommended to use the setValue function with an empty value for effective clearing, rather than relying on the clearValue function. 

You can refer the below code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue) {
        g_form.setValue('infosys_entity', '');  // Clear the value of 'infosys_entity'
    }
}


Kindly mark the answer
✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Aniket

Amit Verma
Kilo Patron
Kilo Patron

Hi @Amol Pawar 

 

You can use Auto-Populate feature to achieve this without writing a single line of code. Refer below post and make the modifications accordingly.

 

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.