Lookup Field incorrect on Edit

From time to time, when editing a form, the lookup field does not populate correctly. I generally close the form, then re-open and it does populate with the correct lookup. I do not have any errors on the form. Is this a known issue? Is there a fix?

Hello @cwalter2,

What do you mean with 'field does not populate correctly'? Is it blank or missing options?

Please check the browser console (F12) for the errors when the field is not loaded.

@mnikitina,

The field populates with a seemingly random value from the associated lookup. Here is console from a time that it happened, as well as a screen recording of the behavior I am talking about:



On the screen recording watch the "Submitted To" field.

@cwalter2,

Thank you for the details!

Is there any code related to 'Submitted To' field the on the form?
Comment it out, clear browser cache and try to reproduce the issue.

Does the field behave the same way when you open the form from the List view, not from List or Library control?

There is code related to the 'Submitted To' field.

function fillDefaultSubmittedTo () {
    if (!fd.field('Submitted_x0020_to').value) {
        fd.field('Submitted_x0020_to').value = window.parent.fd.field('DefaultPCOTo').value.LookupId;
    }
}

function filterSubmitted_x0020_to () {
    //alert('change caught');
    fd.field('Submitted_x0020_to').filter = "Project/Id eq '" + projectID + "'";
    fd.field('Submitted_x0020_to').orderBy = {field: 'Title', asc: true};
    fd.field('Submitted_x0020_to').refresh();
    fillDefaultSubmittedTo();
}

Then the following within spRendered:

fd.field('Submitted_x0020_to').ready().then(filterSubmitted_x0020_to);

This form is never opened from List view, and the way I have the code if I were it would be filled with errors. I have default list values set in the parent form. So I carry them over into this form if the field is empty.

I do not use the New form for this list. In the parent form I have a custom list button that creates the list item then opens the edit form. I do this because it is a log that references a master list to find the next number which is passed to this log and updates the master log.

@cwalter2,

The code is causing this behavior. Update the function like this:

     fd.field('Submitted_x0020_to').ready().then(function() {

        fd.field('Submitted_x0020_to').filter = "Project/Id eq '" + projectID + "'";
        fd.field('Submitted_x0020_to').orderBy = {field: 'Title', asc: true};
        return fd.field('Submitted_x0020_to').refresh()
    }).then(function() {
        if (!fd.field('Submitted_x0020_to').value) {
           fd.field('Submitted_x0020_to').value = window.parent.fd.field('DefaultPCOTo').value.LookupId;
           fd.field('Submitted_x0020_to').reloadValue();
        }
    });
1 Like

So I am no encountering the same issue in some other instances that I used to not have. It is a SharePoint Lookup Field, that does have a filter applied. I am using the following code:

function filterDefaultFrom () {
    //alert('change caught');
    fd.field('DefaultFrom').filter = "Project/Id eq '" + fd.itemId + "'";
    fd.field('DefaultFrom').orderBy = {field: 'Title', asc: true};
    return fd.field('DefaultFrom').refresh();
}

fd.field('DefaultFrom').ready().then(filterDefaultFrom);

I added return to fd.field('DefaultFrom').refresh(); and see the same issue. Do I need to change all my filtered lookup code to:

fd.field('LookupFieldInternalName').ready().then(function () {
    fd.field('LookupFieldInternalName').filter = "Project/Id eq '" + fd.itemId + "'";
    fd.field('LookupFieldInternalName').orderBy = {field: 'Title', asc: true};
    return fd.field('LookupFieldInternalName').refresh();
}).then(function() {
    fd.field('LookupFieldInternalName').reloadValue();
});

Hello @cwalter2,

I'm sorry, I've missed your post.

You only need to update the code if you want to apply the filter and set the lookup field value at the same time. For example on form load.

If you only need to apply filter, you don't need the return statement.

Thank you for clarification. I am running into the issue of the lookup returning a different value than what is saved when I open edit. I have noticed the behavior predominately on filtered lookup fields but I do believe I have seen it on non-filtered fields as well. If I close and re-open the correct value appears. It happens randomly and I have not been able to test if I save if there is any impact on the value. There are no errors in console. Any ideas as to what may be happening?

@cwalter2,

I'm assuming that the problem is filtering lookup options. Try commenting out all the custom code on the forms where you detect this behavior and see if it persists or not.

If not, the issue is in the custom filtering, and you need to update the code.