• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

Add and Delete Row Functionality by Using Visualforce Page in Salesforce

  • July 31, 2017
  • Merfantz Editor
  • Salesforce Developments Tutorial
  • 0

It is simple to add and remove rows in visualforce page. Try the below code and you will get some ideas,

Visualforce Page :

<apex:page StandardController=”Account” extensions=”MultiAdd” id=”thePage”>

<apex:form >

<apex:pageblock id=”pb” >

<apex:pageBlockButtons >

<apex:commandbutton value=”Save” action=”{!Save}”/>

<apex:commandbutton value=”Add” action=”{!Add}” rerender=”pb1″/>

</apex:pageBlockButtons>

<apex:pageblock id=”pb1″>

<apex:repeat value=”{!lstInner}” var=”e1″ id=”therepeat”>

<apex:panelGrid columns=”5″>

<apex:panelGrid headerClass=”Name”>

<apex:facet name=”header”>Del</apex:facet>

<apex:commandButton value=”X” action=”{!Del}” rerender=”pb1″>

<apex:param name=”rowToBeDeleted” value=”{!e1.recCount}” assignTo=”{!selectedRowIndex}”></apex:param>

</apex:commandButton>

</apex:panelGrid>

<apex:panelGrid title=”SPD” >

<apex:facet name=”header”>Country</apex:facet>

<apex:inputfield value=”{!e1.acct.ShippingCountry}”/>

</apex:panelGrid>

<apex:panelGrid >

<apex:facet name=”header”>IsActive</apex:facet>

<apex:inputfield value=”{!e1.acct.Active__c}”/>

</apex:panelGrid>

<apex:panelGrid >

<apex:facet name=”header”>Name</apex:facet>

<apex:inputfield value=”{!e1.acct.Name}”/>

</apex:panelGrid>

</apex:panelgrid>

</apex:repeat>

</apex:pageBlock>

</apex:pageblock>

</apex:form>

</apex:page>

 

Controller:

public class MultiAdd {

//will hold the account records to be saved

public List<Account>lstAcct = new List<Account>();

//list of the inner class

public List<innerClass> lstInner{ get;set; }

//will indicate the row to be deleted

public String selectedRowIndex{get;set;}

//no. of rows added/records in the inner class list

public Integer count = 1;    //{get;set;}

////save the records by adding the elements in the inner class list to lstAcct,return to the same page

public PageReference Save(){

PageReference pr = new PageReference(‘/apex/MultiAdd’);

for(Integer j = 0;j<lstInner.size();j++)    {

lstAcct.add(lstInner[j].acct);

}

insert lstAcct;

pr.setRedirect(True);

return pr;

}

//add one more row

public void Add(){

count = count+1;

addMore();

}

/*Begin addMore*/

public void addMore(){

//call to the iner class constructor

innerClass objInnerClass = new innerClass(count);

//add the record to the inner class list

lstInner.add(objInnerClass);

system.debug(‘lstInner—->’+lstInner);

}/* end addMore*/

/* begin delete */

public void Del(){

system.debug(‘selected row index—->’+selectedRowIndex);

lstInner.remove(Integer.valueOf(selectedRowIndex)-1);

count = count – 1;

}/*End del*/

/*Constructor*/

public MultiAdd(ApexPages.StandardController ctlr){

lstInner = new List<innerClass>();

addMore();

selectedRowIndex = ‘0’;

}/*End Constructor*/

/*Inner Class*/

public class innerClass{

/*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */

public String recCount{get;set;}

public Account acct{get;set;}

/*Inner Class Constructor*/

public innerClass(Integer intCount){

recCount = String.valueOf(intCount);

/*create a new account*/

acct = new Account();

}/*End Inner class Constructor*/

}/*End inner Class*/

}/*End Class*/

Result:-

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

————————————Hope this will help you!———————————–

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
Tags: Field Service ManagementFieldax
  • Previous How to Restrict Unwanted Scripts Created Many Leads in Salesforce
  • Next Xero Integration in Saleforce

Leave a Reply

You must be logged in to post a comment.

Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Mastering Attachment Compression for Salesforce Developers May 9, 2025
Salesforce API Integrations: Connect with Slack, Zoom, and Teams May 2, 2025
Guide to Streamlining Sales Success
Mastering Salesforce CPQ: A Comprehensive Guide to Streamlining Sales Success April 17, 2025

Copyright @2023 Merfantz Technologies, All rights reserved