2

After I created a case and choose lookup field value(AccountId), I wanna get the value in my controller, but the field always return null. VF page

<apex:page standardController="Case" tabStyle="Case" extensions="NewCase">

<apex:form >
    <apex:actionFunction name="getAcctid" action="{! getAcctid}" />
</apex:form>

<script>
    function changeVal(){
        //getAcctid();
        document.getElementById('{! $Component.myForm.myBlock.myBlockSection.test}').value = '{! casex.accountid}';
        document.getElementById('{! $Component.myForm.myBlock.myBlockSection.test2}').value = '{! acctId}';
    }
</script> 

<apex:sectionHeader title="New Case" subtitle="Case Edit"/>
<apex:form styleClass="slds" id="myForm">
    <apex:pagemessages />
    <apex:pageBlock title="Case Edit" id="myBlock">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{! Save}"/>
            <apex:commandButton value="Save & Close" action="{! SaveAndClose}"/>
            <apex:commandButton value="Save & New" action="{! SaveAndNew}"/>
            <apex:commandButton value="Cancel" reRender="a,test" onclick="changeVal()" action="{! getAcctid}"/>
        </apex:pageBlockButtons>

        <apex:pageblockSection title="Case Information" collapsible="false" columns="2">
            <apex:outputField value="{! owner.Name}" />
            <apex:inputfield value="{! casex.status}"/>  
            <apex:inputField id="Contact" value="{! casex.contactid}"/>  
            <apex:inputField value="{! casex.Priority}"/>
            <apex:inputField id="Account" value="{! casex.Accountid}" onchange="changeVal()" />
            <apex:inputField value="{! casex.origin}"/>
            <apex:inputField value="{! casex.Type}" /><p/>
            <apex:inputField value="{! casex.reason}"/>
        </apex:pageblockSection>
        <apex:pageblockSection title="Additional Information" collapsible="false" columns="2">
            <apex:inputField value="{! casex.Product__c}"/>
            <apex:inputField value="{! casex.EngineeringReqNumber__c}"/>
            <apex:inputField value="{! casex.PotentialLiability__c}"/>
            <apex:inputField value="{! casex.SLAViolation__c}"/>
        </apex:pageblockSection>
        <apex:pageblockSection title="Description Information" collapsible="false" columns="1">
            <apex:inputField id="mySub" value="{! casex.Subject}" style="width: 58%"/>
            <apex:inputField value="{! casex.Description}" style="width: 60%"/>
        </apex:pageblockSection>
        <apex:pageblockSection title="Optional" collapsible="false" id="myBlockSection">
            <apex:panelgroup >
                <apex:inputcheckbox value="{! isprivate}" selected="true" label="hello"/>
                <apex:outputtext value="test"/>
            </apex:panelgroup>

            <apex:inputText id="test" />
            <apex:inputText id="test2" value="{! acctId}"/>
            <apex:outputText value="{! casex.accountid}" id="a"/>
        </apex:pageblockSection>
    </apex:pageBlock>
</apex:form> 

public class NewCase {

public Case casex{get;set;}
public Integer flag{get;set;}
public String username{get;set;}
public User owner{get;set;}
public Id acctId{get;set;}

private ApexPages.StandardController controller;

public NewCase(ApexPages.StandardController controller){

    this.controller = controller;

    flag = 1;
    Account acct = null;
    casex = new Case();

    casex.put('subject','test');

    acctId = ApexPages.currentPage().getParameters().get('def_account_id');
    if(acctId == null)
            flag = 2;
    else{
        casex.put('accountid', (Object)acctId);
    }

    username = UserInfo.getName();    
}

public PageReference Save(){
    insert casex;
    return null;
}

public PageReference SaveAndClose(){
    insert casex;
    return controller.cancel();
}

public PageReference SaveAndNew(){
    insert casex;
    casex = new Case();
    casex.put('accountid',(Object)acctId);

    return null;
}

public PageReference Close(){
    return controller.cancel();
}

public PageReference getAcctid(){
    system.debug(casex.accountid);
    system.debug(casex.Subject);
    return null;
    } 
}

Result: DEBUG|null

4
  • 1
    I believe it will be easier to debug if you can some some relevant code. Is this a VF page?
    – manjit5190
    Commented Nov 3, 2016 at 8:43
  • Please share the code
    – Samir
    Commented Nov 3, 2016 at 8:44
  • @manjit_singh hi, dude, I have pasted my test code.
    – C.William
    Commented Nov 3, 2016 at 9:02
  • @Samir ok, I have pasted my test code
    – C.William
    Commented Nov 3, 2016 at 9:03

1 Answer 1

1

There are some required field in your code thats why value does not pass from page to controller. You need to fill that or can use actionregion to pass the value.

Something like

<apex:actionRegion> 
     <apex:inputField value="{!casex.Accountid}">              
          <apex:actionSupport event="onchange" action="{!getAcctid}" rerender="additionalinfo"/>              
      </apex:inputField>
</apex:actionRegion>    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .