[SalesForce] access an apex:inputField onchange value in the controller/extension without saving the record

My Cases VF page renders page block sections based on info pulled from the value entered into the Asset lookup field, using an actionSupport tag.

    <apex:inputField value="{!Case.AssetID}">
        <apex:actionSupport event="onchange" action="{!determinePageBlock}" reRender="{!pageBlockList}" immediate="True" />
    </apex:inputField>

<!-- The following is one of several page blocks that will be rendered as determined by  determinePageBlock()-->
    <apex:outputPanel id="QIblock">
            <apex:pageBlockSection title="QI" collapsible="false" rendered="{!(pageBlockID == 'QIblock')}">
        </apex:pageBlockSection>
    </apex:outputPanel>

Here is my extension:

public with sharing class CaseUniversalControllerExtension {

    public final Case caseRec;

    //pageBlockID and pageBlockList are strings used by the actionSupport tag's rerender 
    //parameter. They are IDs of OutputPanels that are wrapped around pageblocksections. 
    public list<string> pageBlockList {get; set;}
    public string pageBlockID {get; set;}

    public CaseUniversalControllerExtension(ApexPages.StandardController stdController) {
        this.caseRec = (Case)stdController.getRecord();
        this.pageBlockList = new list<string>();
        if(caseRec.assetID != null) determinepageBlock();
    }

    public void determinePageBlock(){
        pageBlockList.clear();
        //if an asset isn't selected, stop here and don't show anything.
        if(caseRec.assetID == null) return;

        string assetCSteam = [SELECT CS_Team__c FROM Asset WHERE ID = :caseRec.assetID LIMIT 1].CS_Team__c;

        pageBlockList.add(assetCSteam + 'block');
        pageBlockID = assetCSteam + 'block';

        system.debug('pageBlockList: ' + pageBlockList);

    }
}

My goal is to be able to access the new value of this lookup field within my controller extension, but without having to save the record first.

Currently, my determinePageBlock() method uses a SOQL query to pull the Asset field needed. That means it only has access to whatever Asset was entered when the case was last saved. But what I'm looking for is the new asset that has just been entered into the lookup field while in edit mode, before the save button is hit. That way I can render the appropriate section in edit mode without firing off the validation rules, triggers, etc that come with saving.

Is this possible?

Best Answer

Few changes in your code

<apex:inputField value="{!caseRec.AssetID}">
        <apex:actionSupport event="onchange" action="{!determinePageBlock}" reRender="{!pageBlockList}"  />
</apex:inputField>

remove immediate="True"

now if you select any assert record actionSupport call determinePageBlock method there check caseRec.assetID

Related Topic