function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sumant HegdeSumant Hegde 

How to read current account id/account name in a custom controller

Hi,

I am new to VisualForce and Apex.

I have the following code in my VF page.

<apex:page standardController="Account" extensions="AccountContactExtend">
    <apex:form>
        <apex:pageBlock>
            <apex:inputField value="{!Account.Name}" label="Account Name"/>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!newPage}" value="New Contact"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

How can I get the value of "inputField" i.e. Account.Name, in my custom apex class "AccountContactExtend", so that I can use that value for SOQL query.

Thanks in advance.
 
Raj VakatiRaj Vakati
Use this code
 
<apex:page standardController="Account" extensions="AccountContactExtend">
    <apex:form>
        <apex:pageBlock>
            <apex:inputText value="{!accname}" label="Account Name"/>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!newPage}" value="New Contact"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class AccountContactExtend {
    public String accname{get;set;}
    public AccountContactExtend(ApexPages.StandardController con){
        
    }
    public PageReference newPage(){
        String accId=[Select Id , Name from Account where Name=:accname Limit 1].Id ;
        COntact con = new Contact();
        con.LastName='Test';
        insert con ;
        return new PageReference('/'+con.Id);
    }
}