Thursday, March 14, 2013

Inserting Account and Contact record from Visualforce Page


Visualforce Page:

<apex:page controller="insertreccls" >
  <apex:Form >
      <apex:Pageblock >
          <Apex:pageblockSection >
             <apex:inputText value="{!aname}" label="Account Name"/>
             <apex:inputText value="{!cname}" label="Contact Name"/>
          </Apex:pageblockSection>
          <apex:pageBlockButtons >
              <apex:commandButton value="Insert Account" action="{!insertrec}"/>
          </apex:pageBlockButtons>
      </apex:Pageblock>
  </apex:Form>
</apex:page>


Apex Class:

public with sharing class insertreccls {

    public PageReference insertrec() {
       
       System.debug('--------->'+aname);
       System.debug('--------->'+cname);
       
       Account obj = new Account();
       obj.name = aname;
       obj.type='Prospect';
       insert obj;
       
       Contact c = new Contact();
       c.lastname = cname;
       c.accountid = obj.id;
       insert c;
       
       System.debug('-------->'+obj.id);
       System.debug('-------->'+c.id);
       
       Pagereference ref = new Pagereference('/'+obj.id);
       return ref;
                                                     }
    public String cname { get; set; }
    public String aname { get; set; }
                                                             }

No comments:

Post a Comment