g_form.addOption() method creating a duplicate value after the form is saved

Kiran Pudari
Tera Contributor

Hi

I am new to serviceNow.

I am trying to add an option using g_form.addOption() method. This is adding the option. However, I have few questions related to this. Please help me to understand this concept

1. Choice added from addOption() method is not visible in sys_choice table. Is this expected behaviour ?
2. When I am trying to add an option to impact field on Incident table, it has added the choice but when the user selects the new option and tries to save it then the choice value is creating a duplicate.

1. One value is 10
2. Second Value is 10 – Super Low

May I know how to address this. Please refer to the below screenshot

Here is the ClientScript

function onLoad() {
//Type appropriate comment here, and begin script below
g_form.addOption(‘impact’, ’10’ , ’10 – Super Low’);

}

 

find_real_file.png

Thanks

Kiran

6 REPLIES 6

Amit Verma
Kilo Patron
Kilo Patron

Hi @Kiran Pudari 

 

I tried simulating your issue on my PDI and I was able to. Actually, as per the logic written in your on-load client script, it will add a choice to the impact field irrespective of whether the choice already exists or not. To overcome this, make use of below On-Load Client Script:

 

AmitVerma_0-1740981000194.png

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    var dropDownValueExists = g_form.getOption('impact', '4');
    if (!dropDownValueExists) {
        g_form.addOption('impact', '4', '4 - Super Low','4');
    }
}

 

Output -

 

AmitVerma_1-1740981050112.png

 

Please note that the choice label will not be visible, instead value will be available on save as we are not making the entry in the sys_choice table. To overcome this problem, you can make the label same as the value in the client script as below:

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    var dropDownValueExists = g_form.getOption('impact', '4');
    if (!dropDownValueExists) {
        g_form.addOption('impact', '4 - Super Low', '4 - Super Low','4');
    }
}

 

Refer

https://www.servicenow.com/community/developer-forum/after-form-reload-choice-field-shows-value-inst...

https://www.servicenow.com/community/itsm-forum/how-to-display-choice-label-instead-of-choice-value-...for more details.

 

Thanks and Regards

Amit Verma


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

Shubham_Jain
Mega Sage

@Kiran Pudari 

 

1. Choice added from addOption() is not visible in the sys_choice table. Is this expected behavior?

Yes, this is the expected behavior. The g_form.addOption() method only adds options dynamically on the client-side (UI). It does not persist the choices in the sys_choice table, which is a server-side table storing predefined choices. If you need to permanently add a choice to a field, you should add it manually in the sys_choice table.

 

Duplicate Choice Value Issue

You're facing this issue because "10" is already an existing value in the impact field, and adding another option with the same value (10) but a different label (10 – Super Low) is causing a conflict when the form is saved.

You should use a unique value for the new choice. Example:

function onLoad() {
   g_form.addOption('impact', '10_super_low', '10 – Super Low');
}

 

if you must use the value 10, then you should not add it dynamically. Instead, modify the existing choice in the sys_choice table.

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain