+ Start a Discussion
chidambarakumari rchidambarakumari r 

Binding two standard object using VF page

Hi,

I'm trying to bind two standard objects using VF page but I'm getting an error "Variable AccountID not exist"


public class AccountController {
 public Contact newContact{get;set;}
 public Account newAccount{get;set;}

public AccountController (ApexPages.StandardController controller) {
  newContact = new Contact();
  newAccount = new Account();  
 
}

public pagereference dosave()
{
insert newAccount;
newcontact.Accountid=newAccount.id;
PageReference acc = new ApexPages.StandardController(newAccount).view();
        acc.setRedirect(true);
        return acc;
}
}

****************************

<apex:page standardController="contact" extensions="AccountController">
<apex:form>
<apex:pageBlock>
<apex:pageBlockSection>
<apex:inputField value="{!newContact.Lastname}"/>
<apex:inputField value="{!newAccount.Name}"/>
<apex:commandButton action="{!dosave}" value="Save"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>  
</apex:page>
Suraj TripathiSuraj Tripathi
Hi Chidambarakumari,
please try this piece of code. Hope it will help you.
Page :
<apex:page standardController="contact" extensions="AccountController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputField value="{!newContact.Lastname}"/>
                <apex:inputField value="{!newAccount.Name}"/>
                <apex:commandButton action="{!dosave}" value="Save"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>  
</apex:page>

Controller :
public class AccountController {
    public Contact newContact{get;set;}
    public Account newAccount{get;set;}
    
    public AccountController (ApexPages.StandardController controller) {
        newContact = new Contact();
        newAccount = new Account();     
    }
    
    public pagereference dosave()
    {
        insert newAccount;
        newcontact.Accountid=newAccount.id;
        insert newContact;
        PageReference acc = new ApexPages.StandardController(newAccount).view();
        acc.setRedirect(true);
        return acc;
    }
}

If this code helps you.Please mark it as best.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi, Hope it helps.
  • Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
chidambarakumari rchidambarakumari r
Hi Suraj Tripathi,

Still I'm getting the error Error: Compile Error: Variable does not exist: Accountid at line 14 column 12