Set a Field Value with a List Lookup Control


Userlevel 4
Badge +10

Hi Folks,

I have successfully configured a list lookup and when a value is selected, I am getting the expected values from the source list item in a series of calculated value controls. My next task is to get some of my list fields/form controls updated with the values that are showing up in the calculated values.

Screenshot

In the image above, I select my name from the dropdown (list lookup control). That loads my last name, first name, and employee ID three calculated value controls currently visible up in the header. I want the values calculated to appear in the appropriate fields below the list lookup control.

I know I could just use the CV controls and bind them to the list fields but I need the user to be able to optionally use manual input if the person is not found I the list.

What is the most straight forward way to do this?

Thanks and Regards,

Patrick Kelligan


43 replies

Userlevel 7
Badge +17

Weird...

If that worked out, now paste the following code and let's see what will happen once you change the value in a dropdown:

NWF$('#'+ jsvar_ListLookup).change(function() {
     alert ("Am I alerting from option 1?");
     console.log("Who am I in 1: " + NWF$(this));
});

NWF$('#'+ jsvar_ListLookup + ' select').change(function() {
     alert ("Am I alerting from option 2?");
     console.log("Who am I in 2: " + NWF$(this));
});

Regards,

Tomasz

Userlevel 4
Badge +10

I tried the new snippet by itself AND pasted just after the setTimeout function. Nothing happened in the text controls either time.

-EDIT-

And... nothing shows up in the console upon change.

Userlevel 7
Badge +17

Oh.. this remote debugging is though sad.png And I'm sorry for the delay, however I didn't get any notification once you replied. This behavior sounds to me like if for som reason the change of your dropdown is not being caught by the "change" event. Have you got any errors in console by a chance?

‌ - is anyone eager to assist me and Patrick in this debugging?

Regards,

Tomasz

Userlevel 4
Badge +10

Hi Tomasz,

Yes... this is challenging. Do you have "Lync" or some way to screen-share?

Regards,

Patrick

Userlevel 7
Badge +17

Hey, we can try a call happy.png I chose 4pm CET as it should be around 10am EDT (this is your time, right?) so you should be right after your first coffee happy.png

You have the details in your corporate inbox. 

Regards,

Tomasz

Userlevel 7
Badge +17

To summarize the issue after the call - it turned out that the code worked just fine, however it required some delay to get the data from the calculated fields as the lookup itself takes seconds to process the data.

Moreover I suspect, that the dropdown control is redraw while the form loads, so just declaring the "change" function that should bind event receiver to the control is not sufficient as the control is afterwards replaced by a new one (that suspicion is built upon the fact, that the same code pasted in the console works, but generated by the form doesn't).

So what I did is that I wrapped the code in a "document.ready" block, what solved the issue. In the end, the working code looks as following:

NWF$(function() {
     NWF$('#'+ jsvar_ListLookup).on('change', function()
        {
          setTimeout(function() {
               NWF$('#'+ jsvar_LastName).val(NWF$('#'+ jsvar_LastName_CAL).val());
               NWF$('#'+ jsvar_FirstName).val(NWF$('#'+ jsvar_FirstName_CAL).val());
               NWF$('#'+ jsvar_EmpID).val(NWF$('#'+ jsvar_EmpID_CAL).val());
          }, 2000);
            });
});

Regards,

Tomasz

Userlevel 4
Badge +10

Thanks Tomasz!

One more bit of advice I would ask. If the form was filled out manually (for example, the person listed was not in the drop down), after the form is submitted, if it is reopened, the blank list lookup clears out the fields. What would be the best way to prevent the code from firing if the form has already been submitted/saved?

Thanks and regards,

Patrick

Userlevel 7
Badge +17

I'd try the following the following (before setting the values check, if your calculated field does have a value):

NWF$(function() {
     NWF$('#'+ jsvar_ListLookup).on('change', function()
        {
          setTimeout(function() {
               if (NWF$('#'+ jsvar_LastName_CAL).val())
               {
                    NWF$('#'+ jsvar_LastName).val(NWF$('#'+ jsvar_LastName_CAL).val());
                    NWF$('#'+ jsvar_FirstName).val(NWF$('#'+ jsvar_FirstName_CAL).val());
                    NWF$('#'+ jsvar_EmpID).val(NWF$('#'+ jsvar_EmpID_CAL).val());
               }
          }, 2000);
            });
});

Regards,

Tomasz

Userlevel 4
Badge +10

Thanks Tomasz!

No, this still overwrites the existing data.

Userlevel 7
Badge +17

Anyway, the weird thing is, why this code overwrites your data if you don't manually change value of the dropdown? Can you add an "alert" action to both - check the value of the CALC field on form load and to check whether the code inside "change" is actually being executed, so that whether it is the reason your values get overwritten. Try this:

NWF$(function() {
     NWF$('#'+ jsvar_ListLookup).on('change', function()
     {
          setTimeout(function() {
               alert ("And the value is... " + NWF$('#'+ jsvar_LastName_CAL).val());
               if (NWF$('#'+ jsvar_LastName_CAL).val())
               {
                    NWF$('#'+ jsvar_LastName).val(NWF$('#'+ jsvar_LastName_CAL).val());
                    NWF$('#'+ jsvar_FirstName).val(NWF$('#'+ jsvar_FirstName_CAL).val());
                    NWF$('#'+ jsvar_EmpID).val(NWF$('#'+ jsvar_EmpID_CAL).val());
               }
          }, 2000);
     });
});

It's started driving me nuts happy.png

Userlevel 7
Badge +17

Anything happend? Is alert shown once the form is loaded?

Regards,

Tomasz

Userlevel 4
Badge +10

Sorry Tomasz,

I ended up having to run my dog to the vet today so I was not able to test it. Will try for tomorrow.

Userlevel 4
Badge +10

Hi Tomasz,

It works. I tried the new code with the alert and it worked fine so I re-tried the previous code and it worked also. Not sure what I did wrong the first time but Thanks for the huge assist on this!!

Thanks and Regards,

Patrick

Userlevel 7
Badge +17

I'm really glad I could've helped you happy.png

Regards,

Tomasz

Userlevel 1
Badge +2

Hello Tomasz

Thanks for working through this in this forum--it's very helpful.

I have a nearly identical scenario, with one difference.  All of the fields (the lookup as well as the fields to be pre-populated based on the lookup) are in a repeating section.  Would this solution work for that as well?

Drew

Userlevel 4
Badge +10

Hi ‌,

Did you ever resolve this issue?

Userlevel 1
Badge +2

Sorry Patrick, I never did.

Userlevel 4
Badge +10

Hi Tomasz Poszytek‌! It has been a while since we posted on this question but I needed the "if" line from your post above in a different piece of work that I created.

if (NWF$('#'+ jsvar_LastName_CAL).val())
    {
        // Code here
    }

The question I had was how this actually works. Does this line test for a NULL value? Lets suppose you want to test for a non-NULL value; how would you write this?

Thanks and Regards,

Patrick

Reply