5

Hello and thanks in advance for any help!

I have a VF page with 2 lookup inputFields and a picklist (built as an inputField with dynamically-populated picklist choices). There is an actionsupport with event="onchange" attribute on the picklist. When the "WorkOrder.Job_Type__c" picklist changes, the "Account__c" lookup inputField is populated by the controller (via SOQL), and then the second, "Installer Contact" lookup inputField is populated by a SOQL query using information from the Account__c record.

<apex:inputField value="{!WorkOrder.Job_Type__c}">
    <apex:actionSupport event="onchange" action="{!updateInstaller}" rerender="installer, installerContact" />
</apex:inputField>

<apex:inputField value="{!WorkOrder.Account__c}" id="installer">
    <apex:actionSupport event="onchange" action="{!updateInstallerContact}" rerender="installerContact" />
</apex:inputField>

<apex:inputField value="{!WorkOrder.Installer_Contact__c}" id="installerContact" >
</apex:inputField>

I've hit a bug in the following situation:

  1. When the Account__c inputField is populated by the controller, and the Installer_Contact__c inputField is subsequently populated by the controller as well, and then:
  2. I change the Job_Type__c picklist value, and if there happens to be more than one record with the same name as the current Installer_Contact__c record in the inputField, everything about the Installer_Contact__c inputField disappears except for the magnifying glass, and no error messages show up apart from in the debug logs (message is "VF_PAGE_MESSAGE | Multiple items found, etc.")

Job_Type__c's actionSupport onchange action is never called in this case.

The specific Installer_Contact__c record retrieved by the controller's SOQL query is not being recognized; only the name is being read (as a String or text), causing the 'multiple items' error.

I am guessing that this is due to the controller populating the Installer_Contact__c inputField as a lookup (using the record Id), but when the picklist changes and the Installer_Contact__c inputField is being reevaluated right before Job_Type__c's actionSupport action is called, the input is seen as text type.

How can I get the inputField to recognize the specific Installer_Contact__c record that is already populating it, without the user having to click on the magnifying glass and choose a record?

Here is the code in the controller:

public PageReference updateInstaller() {
    //if you're going from self install to non, change installer field to blank
    if (!installerIsCustomer() && lastPickWasCustomerInstall) {
        m_workorder.Account__c = null;
        m_workorder.Installer_Contact__c = null;
    //going from non to self-install, change installer to project account
    } else if (installerIsCustomer() && !lastPickWasCustomerInstall) {
        m_workorder.Account__c = m_site.Account__c;
        updateInstallerContact();
    }
    return null;
}


public PageReference updateInstallerContact() {
    //if self install and installer is the same as account owner, install contact is not custom
    if (!installerContactIsCustom) {
        Contact[] installerContacts = [SELECT Name, Id FROM Contact WHERE AccountId = :m_workorder.Account__c AND Owner__c = true LIMIT 1];
        if (installerContacts.size() > 0) {
            m_workorder.Installer_Contact__c = installerContacts[0].Id;
        } else {
            m_workorder.Installer_Contact__c = null;
        }
    }
    return null;
}

Thanks for any help!!!

2

It looks like this is related to the bug posted here: https://success.salesforce.com/issues_view?id=a1p30000000sY09AAE

The problem is that lookup fields have issues when the rerender attribute in an onchange actionSupport directly references the lookup field. The simplest workaround is to wrap the field in an outputPanel and rerender that instead.

<apex:inputField value="{!WorkOrder.Job_Type__c}">
    <apex:actionSupport event="onchange" action="{!updateInstaller}" rerender="installer,installerContact" />
</apex:inputField>

<apex:ouputPanel id="installer">
    <apex:inputField value="{!WorkOrder.Account__c}">
        <apex:actionSupport event="onchange" action="{!updateInstallerContact}" rerender="installerContact"/>
    </apex:inputField>
</apex:outputPanel>

<apex:outputPanel id="installerContact">
    <apex:inputField value="{!WorkOrder.Installer_Contact__c}"/>
</apex:outputPanel>
1

Something to Try:

I'm genuinely surprised that I've not seen this situation arise before. By populating the lookup with the Id you really shouldn't be seeing this problem yet somehow you are.

One thing to try would be to use immediate="true" on the actionSupport element that triggers the contact update:

<apex:actionSupport event="onchange" immediate="true" action="{!updateInstallerContact}" rerender="installerContact" />

This would skip any validation of values and potentially that check that's returning multiple contacts.

A Non-Answer Alternative:

A possible alternative would be to provide a picklist of valid contacts for the account chosen, and use that rather than a lookup field for the contact. Whether that is feasible or not depends entirely on the business case, should the user be able to choose any contact or just one from the account? Assuming there's not too many to choose from a picklist could work.

  • Thank you for both of those options! I tried adding the 'immediate = "true"' attribute, but it didn't work. The bug persisted in that case. – Delphine Sep 6 '16 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.