Skip to main content

In the on-prem version of forms there is a lookup function in the calculated value control. I'm trying to replicate that functionality in O365 forms but am having a hard time (probably because my .js skills are lacking). Basically I have an employee people picker field on my form (that can be changed). I need to get a field from another list using employee as the filter (employee is unique in the other list so only 1 value is returned). I am open to any ideas how to do this but my current "solution" is as follows:

I created a hidden lookup control on the form that only returns the value I need (it recalculates when employee is changed so all is well there). My goal is to set a text box or calculated value based on the lookup control so I can use it in the form. The problem is that the value of the lookup control defaults to 'select a value' but I need the other value. I'm assuming it is just stored as an array and [0] is 'select a value' and [1] is the value I need but I don't know how to surface that data in .js. Help is appreciated... Here's a few things I have tried based on other posts and my knowledge of scripting in other languages:

#All were ran after document ready but I also threw in a delay just in case that wasn't long enough for the lookup to load

(NWF$('#'+lkpControl)[0]) #I would expect my value in index 0 or 1 but I get nothing.

ko.dataFor(NWF$('#'+lkpControl)[0]); #I've seen people on the forums use this to set data in a lookup but I couldn't get it to return anything. I tried index 0 and 1.

NWF$('#'+lkpControl).val('1') #I see this as the solution all over the forums but it sounds like it worked in older versions but not now. I tried it with index 0 and 1 and it didn't return anything.

Hey rstrole – assuming you've got a dropdown that will consistantly have two values ('select a value' and your legitimate value):

If you set the "Client ID JavaScript variable name" on the drop-down to 'lkpControl' then the following jQuery will return the text for the second option (zero based index) as you're wanting.

NWF$('#' + lkpControl + ' option:eq(1)').text();

With this in mind, you could create a function like the below which accepts the JavaScript ClientID variable(s) for your dropdown and text-box and it would update the latter as needed.

var Test = {

    populateTextBox: function (textBoxID, dropdownID) {

        var obj = NWF$('#' + textBoxID);

        obj.val(NWF$('#' + dropdownID + ' option:eq(1)').text());

    },

}

You might also ensure that values can be retrieved from a hidden dropdown lookup control. You may need to instead "hide" this dropdown behind an existing element on the page. Good luck!


Patrick, Thanks for the response!

I actually made it work in a kind of hokey way. What I wound up doing was setting a hidden single line text box with the values from the hidden lookup (you can see the values if you use a hide rule but not if you set the controls to visible = no). Then I used a calculated value control on the form to scrape out the 'please select a value...' (with the replace function) leaving me with the value I need:

NWF$(document).ready(function() {

     var lkpAvailable = NWF$('#'+lkpHoursAvail); // hidden lookup control

     var txtAvailable = NWF$('#'+txtHoursAvail);  // hidden text box

     lkpAvailable.change(function(){

            txtAvailable.val(lkpAvailable.text()).trigger('focusout'); // trigger on focusout so the calculated value control updates

             // calculated value control is set to '(replace(txtHoursAvail,'Please select a value...',''))-0'

            // the minus zero is just so it returns zero if employee is blank (no index 1] value in the lookup)

     });

});

I will go back and try option:eq(1) because it would clean it up a bit. I would be able to remove the hidden text box and the replace function in the calculated control. Thanks again! I'll post back once I get a chance to mess with it.


no problem, happy to help rstrole! If the option:eq(1) solution ends up working for you, please mark the response as the correct question if you don't mind. happy.png


Reply