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

I think I would write custom JS code that on dropdown change injects these values to proper fields base using their IDs.

But on the other hand, even if you bind these fields to the dropdown, user should still be able to input values manually.. Fields are not disabled in such case. Or am I missing something? happy.png

Regards,

Tomasz

Userlevel 4
Badge +10

Hi Tomasz Poszytek‌!

...even if you bind these fields to the dropdown, user should still be able to input values manually.. Fields are not disabled in such case...

What I meant to say was, if I did not use text controls but rather just used the calc value in their place, the manual input would not be possible.

...I would write custom JS code that on dropdown change injects these values to proper fields base using their IDs.

I thought to use JS but I am not very good with it. Here is what I have done so far. I have loaded the "Client ID JavaScript variable name" for each of the controls I want to populate with 'jsvar_LastName', 'jsvar_FirstName', and 'jsvar_EmpID' respectively. My function so far is below. What else do I need to add to make this work? Also, how to I get the funsction to trigger upon selection or re-selection of the List Lookup control?

BTW, 'cv_LastName', 'cv_FirstName', and 'cv_EmpID' are the names of the calculated value controls.

//=============================;
//  Employee Data Controls;
//=============================;

function populateEmployeeData() {
     NWF$('#'+ jsvar_LastName).val("#" + cv_LastName);
     NWF$('#'+ jsvar_FirstName).val("#" + cv_FirstName);
     NWF$('#'+ jsvar_EmpID).val("#" + cv_EmpID);
}‍‍‍‍‍‍‍‍

Thanks for responding,

Patrick

Userlevel 7
Badge +17

If you are sure that the "jsvar_" variables hold your values, then to trigger I would suggest to:

  1. Add a JS function to trigger your "populateEmployeeData" function each time your dropdown is changed. To do so:
    NWF$( "#[ID OF YOUR DROPDOWN FIELD]" ).change(function() 
    { 
        populateEmployeeData();
    });‍‍‍‍
  2. Secondly you can try to replace your calculated controls with plain text fields.

Regards,

Tomasz

Userlevel 4
Badge +10

Thanks for responding Tomasz,

It does not seem to be working. Below it the entirety of my code:

NWF$( "#[llu_Western_Employee]" ).change(function() {
    populateEmployeeData();
});

function populateEmployeeData() {
    NWF$('#'+ jsvar_LastName_CAL).val("#" + jsvar_LastName);
    NWF$('#'+ jsvar_FirstName_CAL).val("#" + jsvar_FirstName);
    NWF$('#'+ jsvar_EmpID_CAL).val("#" + jsvar_EmpID);
}

"llu_Western_Employee" is the control name of my list lookup dropdown.

jsvar_LastName_CAL and the other ..._CAL variables are in the "Client ID JavaScript variable name" fields of their respective Calculated Values controls.

"jsvar_LastName", "jsvar_LastName" and "jsvar_LastName" are in the "Client ID JavaScript variable name" fields of their respective Single Line text box controls.

Do you se where my mistake might be?

Thanks,

Patrick

Userlevel 7
Badge +17

No, no happy.png Without brackets happy.png So just: "#llu_Western_Employee", instead of "#[llu_Western_Employee]".

Try this:

NWF$( "#llu_Western_Employee" ).change(function() {
    populateEmployeeData();
});

function populateEmployeeData() {
    NWF$('#'+ jsvar_LastName_CAL).val("#" + jsvar_LastName);
    NWF$('#'+ jsvar_FirstName_CAL).val("#" + jsvar_FirstName);
    NWF$('#'+ jsvar_EmpID_CAL).val("#" + jsvar_EmpID);
}

Regards,

Tomasz

Userlevel 4
Badge +10

Thanks Tomasz!

Ha ha.. what a noob happy.png Sorry about that.

Ok, I made the change with still no success. The calc values populate just fine but the single line text controls stay blank.

Userlevel 7
Badge +17

I might be lost, correct me please if I really am happy.png

So you have 6 fields:

  1. 3 calculated, having IDs:
    1. jsvar_LastName_CAL
    2. jsvar_LastName_CAL
    3. jsvar_LastName_CAL
  2. Then you have 3 plain "input type=text" fields:
    1. jsvar_LastName
    2. jsvar_LastName
    3. jsvar_LastName

Now you want to set the values for the NON CAL fields. When your dropdown changes, where do you store the values that you want to put in those fields? In the CAL fields? If so, then your populateEmployeeData() function should look like this:

function populateEmployeeData() {
        NWF$('#'+ jsvar_LastName).val(("#" + jsvar_LastName_CAL).val());
        NWF$('#'+ jsvar_FirstName).val(("#" + jsvar_FirstName_CAL).val());
        NWF$('#'+ jsvar_EmpID).val(("#" + jsvar_EmpID_CAL).val());
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can as well try to use that sort of selector. I recall I had sometimes issues (dunno why) when just calling fields using their ID from Nintex:

function populateEmployeeData() {
        NWF$('#'+ jsvar_LastName + " input").val(("#" + jsvar_LastName_CAL).val());
        NWF$('#'+ jsvar_FirstName + " input").val(("#" + jsvar_FirstName_CAL).val());
        NWF$('#'+ jsvar_EmpID + " input").val(("#" + jsvar_EmpID_CAL).val());
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Regards,

Tomasz

P.S.

If that still doesn't work try to log out ( using for example console.log(); or alert(); command ) values so that you will be sure, that fields which you want to use as the source really have the value you are going to assign to the other field.

Userlevel 4
Badge +10

Hi Tomasz Poszytek,

Neither of those worked. I do appreciate your patience with a JavaScript beginner such as myself. I notices the last two snippets had the variable "jsvar_LastName" up from while the one before that had "jsvar_LastName_CAL" (the calculated value controls in the beginning of the lines. Was that intentional?

 NWF$('#'+ jsvar_LastName).val(("#" + jsvar_LastName_CAL).val());

-vs-

NWF$('#'+ jsvar_LastName_CAL).val("#" + jsvar_LastName);

Also, my apologies but I am not sure how to implement the console.log(); or alert(); command; Can you show how I would add them to my code in such a way that they would render the values of the variable and where in the console I would look for the variables?

Thanks,

Patrick

Userlevel 7
Badge +17

Hey! Don't worry, we all learnt that way happy.png

Yes, these two snippets are different because, as I asked you, I'm not certain where you have the values that you want to assign to your text fields.

Generally speaking, the structure of the jQuery command is:

NWF$("#"+ID OF THE FIELD WHERE YOU WANT TO SET A VALUE).val(VALUE TO BE SET);

So you need to replace text "ID OF THE FIELD WHERE YOU WANT TO SET A VALUE" with the ID of the text field AND the text "VALUE TO BE SET" with the value you receive after changing your dropdown, probably already being assigned to the CALCULATED field of yours.

Second thing - to log, or to alert, simply put the line:

console.log(VALUE TO BE LOGGED);

or

alert(VALUE TO BE ALERTED);

console.log will put you a value in the browser console (you can invoke it using F12 key - it names vary - developer console, firebug etc...).

alert will display a monit.

Its really up to you happy.png The reason why I mentioned that is to check, whether you have values in fields, which you'd like to use as the source for your text fields. I'd try the below code:

var LastNameTxt;
var FirstNameTxt;
var EmpIDTxt;

NWF$( "#llu_Western_Employee" ).change(function() {
    LastNameTxt = NWF$('#'+ jsvar_LastName_CAL).val();
    FirstNameTxt = NWF$('#'+ jsvar_FirstName_CAL).val();
    EmpIDTxt = NWF$('#'+ jsvar_EmpID_CAL).val();
    alert (LastNameTxt + " " + FirstNameTxt + " " + EmpIDTxt);
    populateEmployeeData(LastNameTxt, FirstNameTxt, EmpIDTxt);
});

function populateEmployeeData(LastName, FirstName, EmpID)
{
    NWF$('#'+ jsvar_LastName).val(LastName);
    NWF$('#'+ jsvar_FirstName).val(FirstName);
    NWF$('#'+ jsvar_EmpID).val(EmpID);
}

If that alert won't display any values, then that will mean you should check what kind of fields are the one holding your CAL values.

BTW: in one of your first posts you mentioned, that your calculated controls have the following IDs:

Patrick Kelligan napisał(-a):

BTW, 'cv_LastName', 'cv_FirstName', and 'cv_EmpID' are the names of the calculated value controls.

Have you surely changed them into "_CAL"?

Regards,

Tomasz

Userlevel 4
Badge +10

I will dig into this Tomasz!

... in one of your first posts you mentioned, that your calculated controls have the following IDs:...

...Have you surely changed them into "_CAL"?...

Yes... I was using the control name (cv_LastName) and not the "Client ID JavaScript variable name". I fixed this midway as I think the js will only respond to the jsvar name. I alos made this mistake with the control name "llu_WesternEmployee". Can I assume that I should be using a jsvar name like "jsvar_ListLookup"?

Thanks,

Patrick

Userlevel 4
Badge +10

Ok ‌, I ran the code above and it did not populate the text boxes. Here is what the console shows...

Is this what you were looking for or should I be looking elsewhere? Also, could my js code be firing before the calc value controls calculate the lookup function?

Userlevel 7
Badge +17

Would it be possible that you export your form and attach it here? happy.png

Userlevel 4
Badge +10

Attached.

Userlevel 7
Badge +17

I will look into later this evening. I have only O365 env. with me right now.

Just one question - what kind ow Nintex you are using? For SP2013 or 2016?

Userlevel 4
Badge +10

SP2013. Also, not sure if this would tell you anything but I ran the following code with literal string values and it did not work. Thanks Tomasz!

NWF$( '#jsvar_ListLookup' ).change(function() {
     populateEmployeeData();
});

function populateEmployeeData() {
     NWF$('#'+ jsvar_LastName).val(("#" + 'MyLastName').val());
     NWF$('#'+ jsvar_FirstName).val(("#" + 'MyFirstName').val());
     NWF$('#'+ jsvar_EmpID).val(("#" + 'MyEmpID').val());
}‍‍‍‍‍‍‍‍‍
Userlevel 7
Badge +17

As always, the details happy.png

This code works:

var LastNameTxt;
var FirstNameTxt;
var EmpIDTxt;

NWF$('#'+ jsvar_ListLookup).change(function() {
    LastNameTxt = NWF$('#'+ jsvar_LastName_CAL).val();
    FirstNameTxt = NWF$('#'+ jsvar_FirstName_CAL).val();
    EmpIDTxt = NWF$('#'+ jsvar_EmpID_CAL).val();
    alert (LastNameTxt + " " + FirstNameTxt + " " + EmpIDTxt);
    populateEmployeeData(LastNameTxt, FirstNameTxt, EmpIDTxt);
});

function populateEmployeeData(LastName, FirstName, EmpID)
{
    NWF$('#'+ jsvar_LastName).val(LastName);
    NWF$('#'+ jsvar_FirstName).val(FirstName);
    NWF$('#'+ jsvar_EmpID).val(EmpID);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can simplify it a bit:

NWF$('#'+ jsvar_ListLookup).change(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());
});

Regards,

Tomasz

Userlevel 4
Badge +10

Hi Tomasz,

I tied copy paste both options and neither worked. I did not get any errors or form failures but the input boxes below did not populate with either code block.

By the way, what is the functional difference in the first code snippet and the simplified one that you posted?

Thanks and regards,

Patrick

Userlevel 7
Badge +17

The second one is merged into one function, without additional variables etc...

When you used the first option, have you seen alert when changing value in a dropdown?

In the end - I'm really surprised as I made it working using your xml. Does it work in Preview mode for you?

Regards,

Tomasz

Userlevel 4
Badge +10

Tomasz,

So the function should be identical then for both. Cool.

I do not see any alerts pop up. Should it have been a separate pop up window/box?

Nothing in preview either.

-EDIT-

Is it possible that the code is ruining before the calculated values get their data loaded?

Thanks!

Userlevel 4
Badge +10

Hey Tomasz,

Did you by any chance tweak or alter the form I posted to get the code to work? Especially the simplified code seems so straight forward, I cannot understand why it worked for you and not me.

Regards,

Patrick

Userlevel 7
Badge +17

To be honest I have no idea either why it doesn't work for you. I'm reffering to the version with "alert" inside.

Is "jsvar_ListLookup" still the JavaScript ID you have set in the dropdown configuration? And how do you set the dropdown's value? Is it loaded and "fixed" on form load, or do you pick a value from a dropdown list?

Regards,

Tomasz

Userlevel 4
Badge +10

Tomasz,

Yes, "jsvarListLookup" is still the Client ID JavaScript variable name for my dropdown. On form load, the dropdown reads "Please select a value...". Then a user will select a value from the dropdown. The dropdown looks up its list of values from a list on another sub-site.


        

Userlevel 7
Badge +17

I don't have an idea..

The key here is that nothing happens when you change the value in dropdown. Normally it should. You do it after the form is loaded and after values are loaded. It looks like if, for some reason, javascript is not firing.

Do another test. Add this code:

setTimeout(function() {
     alert ("Just a simple alert displaying text message");
     console.log("Just a simple alert displaying text message");
     alert ("The dropdown's ID is: " + jsvar_ListLookup + " and it's value...");
     console.log("The dropdown's ID is: " + jsvar_ListLookup + " and it's value...");
     alert (NWF$('#'+ jsvar_ListLookup + ' option:selected').text());
     console.log(NWF$('#'+ jsvar_ListLookup + ' option:selected').text());
}, 5000);

This code, after 5 seconds since the page load (notice, that all custom javascript code in Nintex Forms is loaded after the page is loaded happy.png), should show you three alerts, and log three messages to the console, one after another - first is just a plain text. Second should show you the ID of your dropdown. The third should display it's current value, what in your case should be "Please select a value...". 

Launch the form and wait. If nothing happens, then please look into the console - maybe there is something? An error or whatever happy.png

Once again the console. Press F12 in IE and then not the "Debugger" tab what you showed some posts ago, but "Console" (I know my screenshot is in polish, but the order of tabs is the same happy.png):

Please tell me something happened happy.png

Regards,

Tomasz

Userlevel 7
Badge +17

Oh, and please copy-paste all the code you have in your custom javascript form setting field. Just to be sure no errors are there. If that is somehow sensitive, you can share it with me via priv.

Regards,

Tomasz

Userlevel 4
Badge +10

It went just as you said. Here are the screens of the messages that your code pushed out...

...then, I got the option to select a value and the selection did not change the fields or re-trigger the alerts.

Here is the console content while opening the form with the console open...

As for the code, I only use your code (just as I copied and pasted from above) in this example. Here it is, copied from the Custom JavaScript input box:

setTimeout(function() {
     alert ("Just a simple alert displaying text message");
     console.log("Just a simple alert displaying text message");
     alert ("The dropdown's ID is: " + jsvar_ListLookup + " and it's value...");
     console.log("The dropdown's ID is: " + jsvar_ListLookup + " and it's value...");
     alert (NWF$('#'+ jsvar_ListLookup + ' option:selected').text());
     console.log(NWF$('#'+ jsvar_ListLookup + ' option:selected').text());
}, 5000);

Thank you SO much for struggling through this with me!

Best regards,

Patrick Kelligan

Reply