Solved

Can't set Lookup's Select value in JavaScript Office 365 Forms

  • 6 January 2016
  • 6 replies
  • 18 views

Badge +5

Hey everyone,

 

I'm trying to set a default value for my "Leave Type" field which is a lookup to a list of leave types. I've followed all examples I've found online and can't get it working. I got the code working for a normal choice field fine on the form and I can also write the value of the drop down out to console when clicking a button, so everything except setting the drop down seems to work fine.

 

Here's the HTML for my select:

SelectHTML.PNG

 

Here's my leave type field with it's settings

LeaveTypeField.PNG

 

Here's my JavaScript

JavaScript.PNG

 

 

I also tried creating the form from scratch and it doesn't work either... No errors in the console.

 

Any ideas or thoughts appreciated happy.png

 

John LuangcoChris Ben

icon

Best answer by rebecca_gordon 20 January 2016, 04:27

View original

6 replies

Badge +3

Hi Rebecca Gordon

I don't recall having to set a lookup field through JavaScript before so can't provide much of an input.

But Set value of lookup field might be helpful as it seems like you might need to set two things in the Javascript for lookup fields.

Cheers

John

Badge +5

Hi John, thanks for your help

I think the issue is just the form loading, I managed to get it working using a button click after the page is loaded, but neither document ready or the RegisterAfterReady function work... I tried finding an easier way to set the default value for a lookup list, both in SharePoint and Nintex but can't see any option anywhere, do you know of one?

NWF.FormFiller.Events.RegisterAfterReady(function(){      

  NWF$("#"+jsLeaveTypeClientID).val('1');

    NWF$("input[id='" + jsLeaveTypeClientID + "']").val(NWF$("#" + jsLeaveTypeClientID + " option:selected").val());

});

Badge +3

Unfortunately, I'm  not too sure with how it works in Office 365, it's probably worth contacting Nintex to clarify.

I can't access the script that we developed at  the moment, but from my memory, we ended up having both the document ready and register after ready function (I can't remember the order). It seems overkill but for some reason we had to do that.

Badge +5

So, still no luck or answers. I have resorted to dirty old polling JavaScript. This code is tested and working:

function PollLeaveType() {

    var endTime = Number(new Date()) + 20000; // try for 20 seconds

    var interval = 500;

    (function p() {

            // If the condition is met, we're done!

            if(LeaveTypeLoaded()) {

                NWF$("#"+jsLeaveTypeClientID).val('1'); // set to annual leave which is always value 1

                return;

            }

            // If the condition isn't met but the timeout hasn't elapsed, go again

            else if (Number(new Date()) < endTime) {

                setTimeout(p, interval);

            }

            else {

            // don't set the default value, the user will have to set

                return;

            }

    })();

}

function LeaveTypeLoaded()

{

  // "" is the default value for "Please select a value..."

  if (NWF$("#"+jsLeaveTypeClientID).val() == "")

  return true;

  else

  return false;

}

PollLeaveType();

Badge +3

Thank you Rebecca, really helped me out! Hopefully Nintex will add an event to hook into to indicate when these fields are fully loaded soon!

Badge +5

Glad it helped!!!

Reply