Client script doesn't work as expected when setting an option in the field.

Ariomar de Deus
Tera Contributor

I have a form field and need to have an option already selected when the form loads. So, I created an onload client script as per the code below. However, the code doesn't work. The field that should be set to the option is a reference field. Could you help me if I'm missing something in my code?

 

function onLoad() {
   
    if (!g_form.getValue('item_set')) {
        g_form.setValue('item_set', '408aa0481bd7d910a9bc0fa0f64bcb6d');
    }
}
2 ACCEPTED SOLUTIONS

pavani_paluri
Giga Guru

Hi @Ariomar de Deus ,

 

You are saying the field is choice field, but you are setting the value as sysid like its a reference. Please verify once.

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

View solution in original post

So your client script is at catalog and not variable set? Can you confirm there is no client script at variable set/ catalog item level conflicting with this?

 

Onchange client scripts also work on load if  script is written in OnLoad block.

 

There can be a UI policy conflicting as well.

 

Overall, your script is correct, also make sure if you are checking on portal the UI type is set to All


Raghav
MVP 2023

View solution in original post

4 REPLIES 4

RaghavSh
Kilo Patron

If you want to set it on load , you can add this sys_id in default value.

 

Make sure the sys_id of the valid record from the table the field is referring to.

 

Also once try without the if condition if above 2 don’t work.


Raghav
MVP 2023

I tested removing the if and it didn't work, when I set the option to the default value I can't because the field is inserted in a variable set and regarding the sysID the value is correct.

 

So your client script is at catalog and not variable set? Can you confirm there is no client script at variable set/ catalog item level conflicting with this?

 

Onchange client scripts also work on load if  script is written in OnLoad block.

 

There can be a UI policy conflicting as well.

 

Overall, your script is correct, also make sure if you are checking on portal the UI type is set to All


Raghav
MVP 2023

pavani_paluri
Giga Guru

Hi @Ariomar de Deus ,

 

You are saying the field is choice field, but you are setting the value as sysid like its a reference. Please verify once.

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P