How can we Insert, Update and Delete a record in a Visualforce Page ?

We can do simple Insert, Update and Delete in a Visualforce Page with out using any APEX code. If you want extend the standard methods, you will need to write an extension to the Standard Controller or create a Custom Controller.

Simple INSERT, UPDATE, DELETE in Visualforce page:

We can insert, update and delete records by  using Salesforce standard controller methods like “Save” and “Delete”.

For example we want to create, update or delete an account record using standard methods using below code:

Visualforce Code:

<apex:page standardController="Account"> 
 <apex:form> 
 <apex:pageBlock> 
 <apex:pageBlocksection columns="1">
 <apex:inputField value="{!Account.Name}"/>
 <apex:inputField value="{!Account.BillingCity}"/>
 <apex:inputField value="{!Account.BillingState}"/>
 <apex:inputField value="{!Account.BillingCountry}"/>
 <apex:inputField value="{!Account.BillingPostalCode}"/>
 </apex:pageBlocksection>
 
 <apex:pageBlockButtons >
 <apex:commandButton action="{!save}" rendered="{!ISBLANK(Account.Id)}" value="Create"/>
 <apex:commandButton action="{!save}" rendered="{!NOT(ISBLANK(Account.Id))}" value="Update"/>
 <apex:commandButton action="{!delete}" rendered="{!NOT(ISBLANK(Account.Id))}" value="Delete"/>
 </apex:pageBlockButtons> 
 </apex:pageBlock> 
 </apex:form>
</apex:page>

We did not write any class and methods to perform DML operations on objects. Now let’s test the page using following URL:

Insert

https://instance_name.salesforce.com/apex/page_name

create_account

Update & Delete

https://instance_name.salesforce.com/apex/page_name?id=AccountId

update_account

Extending standard controller methods using extension class

Let’s now extend standard controller save() method by amending previous code:

<apex:page standardController="Account" extensions="AccountExtension">  
....
....
</apex:page>

Create the following exctension class and override standard ‘save’ method.

Apex Code:

public class Blog_AccountExtension {
	
    Account acc;
    
    public Blog_AccountExtension(Apexpages.StandardController ctrl){
        //get account record
        acc = (Account)ctrl.getRecord();
        
    }
    
    public PageReference Save(){
        
        //do your custom changes here and save
        acc.Name = 'TEST Account';
        
        //do insert new or update
        upsert acc; 
        
        return new PageReference('/'+acc.Id);
    }
}

 

Summary

Salesforce provides standard methods to perform DML operations on the sObjects. We can always override these standard methods by writings extension classes.

 
Author:

Ajomon Joseph AJOMON JOSEPH
Senior Salesforce Architect
In Connect me on LinkedIn
twitter Follow me on Twitter @apexcoder
blogger Subscribe to my blog https://apexcoder.com
email Email me ajomon@apexcoder.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s