Thursday, June 20, 2013

Salesforce - Insert data into Custom object using Apex

To insert data to Custom object using Apex just understand the below code. This is the basic for all other complex apex programming. Here in the Save() function the data you have enter in the text box will be saved. After inserting the data i am refreshing the page to clear the values in the text boxes. You han find the code in that function.
Apex Design Code:
 <apex:page controller="AddUserController">  
   <apex:form >  
     <apex:sectionHeader subtitle="All Books" title="Books" />  
     <apex:pageBlock >  
       <apex:PageBlockButtons >  
         <apex:commandButton action="{!Save}" value="Save"/>  
       </apex:PageBlockButtons>  
       <apex:pageBlockSection >  
         <apex:InputField value="{!users.Name}"/><br/>  
         <apex:InputField value="{!users.Age}" /><br/>  
         <apex:InputField value="{!users.Class}" /><br/>  
       </apex:pageBlockSection>  
   </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Apex Controller Code:
public class AddUserController  
 {  
   public User__c users;  
   public AddUserController()  
   {  
     users=new User__c();  
   }  
   public User__c getUsers()  
   {  
     return users;  
   }  
   public PageReference Save()  
   {  
     try  
     {  
       insert(users);  
       PageReference pagr= Page.UsersPage;  
       pagr.setRedirect(true);  
       return pagr;  
     }  
     catch(System.DMLException e)  
     {  
       return null;  
     }  
   }  
 }  

No comments:

Post a Comment