Set a Lookup field by javascript

  • 31 March 2016
  • 5 replies
  • 21 views

Userlevel 4
Badge +11

Hi,

I'm using Nintex Forms 2013 on an-premises environment and I'm finding some issues while I try to set a Lookup field using javascript.

In my form I have a button who calls a javascript function where I do my stuff to get the value to be selected in the lookup field and then I'm ready to set the field.

The field has a css class "geoArea".

I've tried the following lines of code

NWF$('.geoArea select option[title="My option title"]').prop("selected",true);

NWF$('.geoArea select').val(optValue); //optValue is a variable with the numeric value of the option I want to select

With both lines I'm able to apparently set to field to the value I want but when I save the form, the value is lost, so if I open it again the field is blank again.

If I select the value manually in the field, the value is save correctly.

Do you know if is there anything else I have to set to save my value when I save the form?

Thanks!!

Giacomo


5 replies

Userlevel 5
Badge +14

have a look here how to do that Set default value to lookup field stopped working after Nintex Forms 2013 latest update

Userlevel 4
Badge +11

Hi Marian,

thanks for your suggestion but it looks like the answers provided there don't solve my issue..

I've tried the setLookupFields approach but it doesn't set any value in my form and Wayne's method is the same I'me using (I've just definded the desired value before in the optValue variable).

Last method changes the value in the form but when I save, the value doesn't get saved..

Giacomo

Userlevel 4
Badge +11

Inspecting more in deep Kalpesh Vaghela​ method, I've found that in my case I had to do the following to correctly set the field:

var vmForApplication = ko.dataFor(NWF$(".geoArea select")[0]);

vmForApplication.selectedItem(optValue);

Giacomo

Userlevel 5
Badge +14

calling it from a button (resp. once the form is already rendered) you need to use selectedItems, you are right.

if you needed to set default value during the form is initially rendered you would have to use initialValue.

Badge +3

Hi all,

maybe someone faces the same issue.

None of the suggested solutions worked for me. With my case the issue was that the Nintex form (SP 2013 on prem, v2.11.2.2) was adding _hid to the end of the lookup field ID in selected form variable.

In my case I had lookup field varibable name llMatGroup and in run time it resolved to

ctl00_ctl42_g_14f1e992_4ae6_4530_9330_633fac4197ac_ctl00_ListForm2_formFiller_FormView_ctl70_b7c065b5_8752_425c_8c69_e663c40875f5_hid

but the actual ID of the field in DOM was

ctl00_ctl42_g_14f1e992_4ae6_4530_9330_633fac4197ac_ctl00_ListForm2_formFiller_FormView_ctl70_b7c065b5_8752_425c_8c69_e663c40875f5

When I changed this in the code all worked with normal

NWF$('#' + llMatGroup).val(idValueOfDesiredOption);

and then I used

NWF$('#' + llMatGroup).change();

to make sure selection is saved to the list

NO IDEA why this has happened and I am possibly the only one this ever happened or will happen to but I'll share this anyways.

Jan

Reply