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
DesaiDesai 

How to pass Account Name from VF Form to Apex controller

Hi,

We are creating a cusotm VF form, which contains Account Name and Contact Name. On Click of Save button , Account , Contact  and opportunity should be created and Account and Contact  should be linked to opportunity.

Thanks.
GauravGargGauravGarg
Hi Pallavi,

VF page
<apex:page controller="Record_Builder_AC">
    <apex:form >
        <apex:pageMessages id="msg"/>
        <apex:inputText id="AccountName" label="Account:" value="{!AccountName}"/>
        <apex:inputText id="ContactFName" label="Contact First Name:" value="{!contactFName}" />
        <apex:inputText id="ContactLName" label="Contact Last Name:" value="{!contactLName}" />
        
        <apex:commandButton action="{!validate_and_create_records}" title="Submit" id="submitForm" value="Submit Form" />
    </apex:form>
</apex:page>

Controller:
public class Record_Builder_AC {
    public String AccountName{get;set;}
    public String contactFName{get;set;}
    public String contactLName{get;set;}
    
    public PageReference validate_and_create_records(){
        pagereference redirect;


        if(String.isBlank(AccountName))
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account Name'));
            return null;
        }
        else if(String.isBlank(contactFName))
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Contact First Name'));
            return null;
        }
        else if(String.isBlank(contactLName))
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Contact last Name'));
            return null;
        }
        else
        {
            try
            {
                Account acc = new Account(Name=AccountName);
                insert acc;
                
                Contact con = new Contact(FirstName = contactFName, LastName = contactLName, AccountId = acc.id);
                insert con;
                
                Opportunity opp = new Opportunity(CloseDate = Date.today().addDays(20), AccountId = acc.Id, StageName='Qualified', Name=acc.Name+'-'+Date.today());
                insert opp;
                
                redirect = new PageReference('/'+acc.Id);
                
            }catch(exception e)
            {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,e.getMessage()));
            return null;
            }
            
        }
        
        return redirect ;
    }
}

Thanks,
Gaurav
Skype: gaurav62990​
DesaiDesai
Hi Gaurav,

Thanks for the quick response.

Wanted to know if we can use <apex:inputField> component ?

Thanks
GauravGargGauravGarg
Hi Pallavi,

Apex:InputField can be used only if we declare Object first and then use it on VF page. 

Thanks,
Gaurav