0

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?

  • 1
    your code seems to be fine the only thing which I can see is immediate="True" which is not binding that value to controller side. Remove this attribute and I think you good to go with your logic. – Himanshu Nov 21 '15 at 5:39
3

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

  • Thanks for your answer. It looks like just removing the immediate="True" parameter did the trick. Is there still a benefit to changing the caseRec declaration as you suggested? – smohyee Nov 21 '15 at 7:52
  • 1
    not required b'coze you are using in constructor caseRec = (Case)stdController.getRecord(); – Ratan Paul Nov 21 '15 at 7:55
1

When we use immediate="True" Salesforce bypass the view state and never bind the visualforce page variable with controller properties which cause this issue.

By default immediate attribtute is false which does the variable binding automatically.

Immedidate="True" is generally required when we bypass the page validation and we want to submit data using actionfunction or action support.

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.