1

This is a simplified example of the problem I'm having, using minimum data.

I've created 2 objects, branch and leaf. Each leaf has a lookup to branch. I put a button on the leaf list to create a new leaf using a custom VF page.

The custom VF page should display the new description every time the lookup field is changed, instead the description it started with continues to be displayed.

I've tried several variations with and without actionregion and outputpanel and using actionfunction instead of actionsupport and other variations. But it's always the same; the function doesn't get the value entered into the screen.

Can someone please explain what I would need to do to make this work.

Controller code:

public with sharing class LeafTest {

    public class LeafException extends Exception {}


    @TestVisible private ApexPages.StandardController theController;
    public String fromPage;
    public Branch__c theBranch { get; set; }
    public Leaf__c theLeaf { get; set; }

    public LeafTest(ApexPages.StandardController stdController) {

        theController = stdController;
        fromPage = ApexPages.currentPage().getParameters().get('leafid');
        theLeaf = new Leaf__c();
        theBranch = [SELECT Id, Name, Description__c
                     FROM Branch__c
                     WHERE Name = 'A'];
        theLeaf.Branch__c = theBranch.Id;
    }
    public PageReference changeBranch() {
        theBranch = [SELECT Id, Name, Description__c
                     FROM Branch__c
                     WHERE Id = :theLeaf.Branch__c];
        return null;
    }
    public PageReference save() {
        try
        {
            insert theLeaf;
        } 
        catch(Exception e) 
        {
            ApexPages.addMessages(e);
            return null;
        }
        PageReference pageRef = new PageReference('/00Q?fcf=' + fromPage);
        return(pageRef);
    }
    public PageReference cancel() 
    {
        PageReference pageRef = new PageReference('/00Q?fcf=' + fromPage);
        return(pageRef);
    }
}

Page code:

<apex:page showHeader="true" sidebar="true" standardController="Leaf__c"
           extensions="LeafTest">
    <apex:form id="TheForm">
        <apex:pageBlock mode="edit" id="leadinfo" >
            <apex:pageBlockSection columns="1" title="Leaf Information">
                <apex:inputField value="{!theLeaf.Name}" required="true" />
            </apex:pageBlockSection>
            <apex:actionRegion >
                <apex:pageblockSection id="BranchSection" columns="1"
                                       title="Branch Information">
                    <apex:inputField value="{!theLeaf.Branch__c}"
                                     required="true">
                        <apex:actionSupport event="onchange" 
                                            action="{!changeBranch}"
                                            immediate="true"
                                            reRender="BranchSection"
                                            status="branchChangeStatus" />
                    </apex:inputField>
                    <apex:outputField value="{!theBranch.Description__c}" />
                    <apex:actionstatus id="branchChangeStatus"
                                       startText="Retrieving branch info..."
                                       stopText="" />
                </apex:pageblockSection>
            </apex:actionRegion>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Create" action="{!save}"/>
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:pagemessages ></apex:pagemessages>
    </apex:form>
</apex:page>
  • for starters remove the immediate=true, second what do you mean by "blows up" – Eric Aug 27 '15 at 21:26
  • It was having a runtime error on the select because :theLeaf.Branch__c was null. But I added this as the last line in the constructor: theLeaf.Branch__c = theBranch.Id; Now it just stays on the description for the branch with name 'A', instead of changing to another branch's description when the other branch is selected. – aging hippie Aug 27 '15 at 21:33
  • You are still seeing the old branch description because the leaf is still pointing to the old branch..the select query in the changeBranch method is pulling from database old branch details. When the user selects new branch you should take that branch Id and insert into db and then the query in changeBranch will give you the new branch description – javanoob Aug 27 '15 at 21:35
  • Actually taking the immediate off made the output field refresh correctly. I don't want to save the "leaf" at that point because in the real world problem (much larger and more complex) there are more input fields to be entered. I also didn't want to take immediate off because there are input fields in the actionregion that are required, and I don't want them validated while they're selecting the lookup - not sure how to get around that. – aging hippie Aug 27 '15 at 21:44
  • 2
    Trying to have your cake and eat it to will require you to make several cakes......Unfortunately, if you leave immediate = true, the values will not post. – Eric Aug 27 '15 at 22:21

Your Answer

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

Browse other questions tagged or ask your own question.