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.
<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