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
sangam vvsangam vv 

Friends I am stuck in the Visualforce Page?

Scenario:
I have account(parent)
 and contact and address(both are child)
i want to create account,contact,addres in one visual force page and i want to save respected data in respected objects?
How to do pleaser help me.
Dhruv Gour 14Dhruv Gour 14
HI ,

Call a controller method "savedata " from visualforce page using <apex :action> Try controller just like =

public address add{get;set;}
public contact con{get;set;}
public account acc{get;set;}

public pagereference saveData(){
acc.address=add.id;
acc.contact=con.id;
List<sobject> sobjlist = new list<sobject>();
sobjlist.add(add);
sobjlist.add(con);
sobjlist.add(acc);

insert sobjlist ;

return acc.id ;
}

PS: There may be some syntax error .

Please ask me if any other concern


Thanks
Dhruv
SFDC GuestSFDC Guest
Hi Sangam W,

Please use the below code for your requirement.
Here Address__c is API Name for Address object, Account__c is lookup field in Address object.
public with sharing class AccountContactAddressCtr
  {
    public Account acc {get; set;}
    public Contact con {get; set;}
    public Address__c address {get; set;}
    Public AccountContactAddressCtr(ApexPages.StandardController stdCtr)
    {
        acc = (Account)stdCtr.getRecord();
        con = new Contact();
        address = new Address__c();
    }
    public PageReference customSave()
    {
        try
        {
                insert acc;
                con.AccountId = acc.Id;
                upsert con;
                address.Account__c = acc.Id;
                upsert address;
                PageReference p = new PageReference('/'+acc.Id);
                p.setRedirect(true);
                return p;
        }
        catch(Exception e)
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Details could not be saved'));
            return null;
        }
    }
    
  }
  
<apex:page StandardController="Account" extensions="AccountContactAddressCtr">
    <apex:form>
    <apex:sectionHeader title="Account, Contact, and Address" subtitle="page"/>
    <apex:pageBlock>
        <apex:pageBlockSection  title="Account Information">
            <apex:inputField value="{!acc.Name}"/>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection title="Contact Information">
            <apex:inputField value="{!con.LastName}"/>
        </apex:pageBlockSection>
        
        <apex:pageBlockSection title="Address Information">
            <apex:inputField value="{!address.Name}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Please mark it as best answer if it resolved your problem.

Thank You,
Sohel Mohd