Set control vlookup field

I was able to set up a data source for fd.control lookup field and populate the required single choices for cities - Brno, Praha,... Then, on change, I set the Sharepoint list field "single line of text" with the chosen value. If I choose Brno from lookup, I set another SharePoint field with this choice. Then I hit the save and everything is cool.
When I open EditForm, the control lookup does not hold the value, so I need to get field value from the "single line of text" - for example "Brno" and set fd.control("lookup") with this value.
I tried your documentation and everything amongst the community forum, and nothing worked.

  1. fd.control("Lookup").value = fd.field("Provoz").value - not working

  2. I also have it wrapped in:

    fd.control("Lookup").ready().then(function (control) {
    control.value = fd.field("Provoz").value;
    Also not working.

Values are shown in the developer console correctly, but only the single line of text shows value "Brno", but the lookup does not, even the object says it has this value, but not showing.

Can you help me, please?


Dear @Stepan,
The issue here is that the lookup field is set with ID, not with Text value. You might need an additional field to hold value as ID number, then set it the same way on load.

Also, you can just utilize Save To property of the control, so it's automatically saved without the need for extra JavaScript code. It will be stored as an object with both ID and Text value, and you can still copy text value to another field with JavaScript, if you want to.

1 Like

Dear @Nikita_Kurguzov ,
I have figured it out as you said with/without JS code.
I've created a new single line of text and used "Save To" feature to save object to this field (newForm). In Editing mode I parsed the object to JSON from the "value" field like this:

//Getting the value from the field
var lookupID = fd.field("vLookupID").value;
//returned this - {"id":25,"text":"Brno"}
var jsonProvoz = JSON.parse(lookupID);
//returned with JSON and setting another field with only property "text"
fd.field("txtProvoz").value = jsonProvoz.text;

Anyway, when I want to set the field value for the specific field which is not present on the form (newForm/Edit), I cannot do it. I have to put it on the canvas and then hide it with JS code. Is it all right?

Thank you so much for the help.

Dear Stepan,
You can't do it by setting the value, as the control/field needs to be rendered in order to do it, but you have an option to update this field directly with the use of pnpjs, something like this should work:

pnp.sp.web.lists.getByTitle('List Name').items.getById(fd.itemId).update({
   MyField: 'New value'
}).then(function() {

More on working with items using pnpjs here - List Items - PnP/PnPjs

1 Like

Dear Nikita,
That should be working in the edit form because via javascript I know the list item ID, but I come to an issue when I use pnp in newForm. The Promise returns an error because pnp have not obtained item ID. So my solution now is to check if the fd.control("vLookup") field is ready, and then I have attached the event listener (on change) on this field, and if the field is changed I set the value for "single line of text" from fd.control field. As you mentioned above, It must be rendered so I got these fields present in the form, but they got hidden.

1 Like