{"id":6132,"date":"2021-03-24T02:41:04","date_gmt":"2021-03-24T02:41:04","guid":{"rendered":"https:\/\/www.greytrix.com\/blogs\/salesforce\/?p=6132"},"modified":"2021-03-24T02:41:07","modified_gmt":"2021-03-24T02:41:07","slug":"add-delete-row-dynamically-in-salesforce-lightning","status":"publish","type":"post","link":"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/24\/add-delete-row-dynamically-in-salesforce-lightning\/","title":{"rendered":"Add Delete Row Dynamically In Salesforce Lightning"},"content":{"rendered":"\n<p>In this blog, we will discuss how to Add Delete Row Dynamically In Salesforce Lightning Component. We shall take an example of adding account records in Salesforce using Add Delete Row functionality.<\/p>\n\n\n\n<p>Similarly, we have created 1 sample lightning component using which user can create multiple records as per requirement by Adding or Deleting new rows on UI. Also make \u2018Account Name\u2019 as mandatory field.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 1:<\/strong> <strong>Go to the Developer Console ->> File ->> New ->> Apex Class:<\/strong> <strong>AuraController.apxc<\/strong><ul><li>Create apex class i.e. <strong>AuraController<\/strong><\/li><li>Create AuraEnabled method i.e. <strong>saveAccountList <\/strong>which take single parameter to hold account list. Refer below code snippet.<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AuraController \r\n{   \r\n    @AuraEnabled\r\n    public static void saveAccountList(List&lt;Account> accList) \r\n\t{        \r\n        Insert accList;\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Step 2:<\/strong> <strong>Go to the Developer Console ->> File ->> New ->> Lightning Component: AuraComponent.cmp<\/strong><ul><li>Declare attribute of type account array to store multiple account and name it as \u2018accountList\u2019.<\/li><\/ul><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;aura:component controller=\"AuraController\" Implements=\"force:appHostable,flexipage:availableForRecordHome,force:hasRecordId\">\r\n    &lt;aura:attribute name=\"accountList\" type=\"Account&#91;]\"\/>\r\n    &lt;lightning:card>\r\n        &lt;div class=\"slds-m-around--xx-large\">\r\n            &lt;div class=\"slds-float_right slds-p-bottom_small\">\r\n                &lt;h1 class=\"slds-page-header__title\">Add Row \r\n                    &lt;lightning:buttonIcon iconName=\"utility:add\"  size=\"large\" variant=\"bare\" alternativeText=\"Add\" onclick=\"{!c.addRow}\"\/>\r\n                &lt;\/h1>\r\n            &lt;\/div>\r\n            &lt;div class=\"container-fluid\">        \r\n                &lt;table class=\"slds-table slds-table_bordered slds-table_cell-buffer\"> \r\n                    &lt;thead>\r\n                        &lt;tr class=\"slds-text-title_caps\">\r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\">Sr. No&lt;\/div>\r\n                            &lt;\/th>\r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\" title=\"Account Name\">Account Name&lt;\/div>\r\n                            &lt;\/th>\r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\" title=\"Phone\">Phone&lt;\/div>\r\n                            &lt;\/th>\r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\" title=\"Fax\">Fax&lt;\/div>\r\n                            &lt;\/th>  \r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\" title=\"Website\">Website&lt;\/div>\r\n                            &lt;\/th>  \r\n                            &lt;th scope=\"col\">\r\n                                &lt;div class=\"slds-truncate\" title=\"Action\">Action&lt;\/div>\r\n                            &lt;\/th>\r\n                        &lt;\/tr>\r\n                    &lt;\/thead>   \r\n                    &lt;tbody>      \r\n                        &lt;aura:iteration items=\"{!v.accountList}\" var=\"acc\" indexVar=\"index\">\r\n                            &lt;tr>\r\n                                &lt;td> \r\n                                    {!index + 1}\r\n                                &lt;\/td>\r\n                                &lt;td>\r\n                                    &lt;lightning:input name=\"accName\" type=\"text\" maxlength=\"50\" value=\"{!acc.Name}\" \/>\r\n                                &lt;\/td>\r\n                                &lt;td>\r\n                                    &lt;lightning:input name=\"accPhone\" type=\"phone\" maxlength=\"10\" value=\"{!acc.Phone}\" \/>\r\n                                &lt;\/td>\r\n                                &lt;td>\r\n                                    &lt;lightning:input name=\"accFax\" type=\"text\" value=\"{!acc.Fax}\" \/>\r\n                                &lt;\/td>\r\n                                &lt;td>\r\n                                    &lt;lightning:input name=\"accWebsite\" type=\"text\" value=\"{!acc.Website}\" \/>\r\n                                &lt;\/td>\r\n                                &lt;td>\r\n                                    &lt;a onclick=\"{!c.removeRecord}\" data-record=\"{!index}\">\r\n                                        &lt;lightning:icon iconName=\"utility:delete\" size=\"small\" alternativeText=\"Delete\"\/>\r\n                                        &lt;span class=\"slds-assistive-text\">Delete&lt;\/span>\r\n                                    &lt;\/a>\r\n                                &lt;\/td> \r\n                            &lt;\/tr>\r\n                        &lt;\/aura:iteration>\r\n                    &lt;\/tbody>\r\n                &lt;\/table>\r\n                &lt;div class=\"slds-align_absolute-center slds-p-top_small\">\r\n                    &lt;lightning:button variant=\"brand\" label=\"Submit\" title=\"Brand action\" onclick=\"{!c.saveAccounts}\" \/>\r\n                &lt;\/div>\r\n            &lt;\/div>\r\n        &lt;\/div>\r\n    &lt;\/lightning:card>\r\n&lt;\/aura:component><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Lightning JS Controller: AuraComponentController.js<\/em><\/strong><\/h4>\n\n\n\n<p><em>See code comment<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n    addRow: function(component, event, helper) {\r\n        \/\/get the account List from component  \r\n        var accountList = component.get(\"v.accountList\");\r\n        \/\/Add New Account Record\r\n        accountList.push({\r\n            'sobjectType': 'Account',\r\n            'Name': '',\r\n            'Phone': '',\r\n            'Fax': '',\r\n            'Website ': '', \r\n            \r\n        });\r\n        component.set(\"v.accountList\", accountList);\r\n    },\r\n    \r\n    removeRecord: function(component, event, helper) {\r\n        \/\/Get the account list\r\n        var accountList = component.get(\"v.accountList\");\r\n        \/\/Get the target object\r\n        var selectedItem = event.currentTarget;\r\n        \/\/Get the selected item index\r\n        var index = selectedItem.dataset.record;\r\n        \/\/Remove single record from account list\r\n        accountList.splice(index, 1);\r\n        \/\/Set modified account list\r\n        component.set(\"v.accountList\", accountList);\r\n    },\r\n    \t\r\n    saveAccounts: function(component, event, helper) {      \r\n        if (helper.validateAccountRecords(component, event)) {\r\n            \/\/Call Apex method and pass account list as a parameters\r\n            var action = component.get(\"c.saveAccountList\");\r\n            action.setParams({\r\n                \"accList\": component.get(\"v.accountList\")\r\n            });\r\n            action.setCallback(this, function(response) {\r\n                \/\/get response status \r\n                var state = response.getState();\r\n                if (state === \"SUCCESS\") {\r\n                    \/\/set empty account list\r\n                    component.set(\"v.accountList\", &#91;]);\r\n                    alert('Accounts saved successfully');\r\n                }\r\n            }); \r\n            $A.enqueueAction(action);\r\n        }\r\n    },\r\n})<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><em><strong>Lightning JS Helper:<\/strong> <strong>AuraComponentHelper.js<\/strong><\/em><\/h4>\n\n\n\n<p><em>See code comment<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>({\r\n    validateAccountRecords: function(component, event) {\r\n        \/\/Validate all account records\r\n        var isValid = true;\r\n        var accountList = component.get(\"v.accountList\");\r\n        for (var i = 0; i &lt; accountList.length; i++) {\r\n            if (accountList&#91;i].Name == '') {\r\n                isValid = false;\r\n                alert('Account Name cannot be blank on '+(i + 1)+' row number');\r\n            }\r\n        }\r\n        return isValid;\r\n    },\r\n})<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><em>Output:<\/em><\/strong><\/h4>\n\n\n\n<p><strong>Create Account Record:<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/03\/1-CreateAccountRecord.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/03\/1-CreateAccountRecord.png\" alt=\"Create Account Record\"><\/a><\/center>\n<font size=\"2\"><center><i>Create Account Record<\/i><\/center><\/font>\n\n\n\n<p><strong>Created Account Records:<\/strong><\/p>\n\n\n\n<center><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/03\/2-CreatedAccountRecords.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" class=\"size-full\" style=\"border: 1px solid #A9A9A9; padding: 2px; margin: 2px; align: center;\" src=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-content\/uploads\/2021\/03\/2-CreatedAccountRecords.png\" alt=\"Created Records in Account Object\"><\/a><\/center>\n<font size=\"2\"><center><i>Created Records in Account Object<\/i><\/center><\/font>\n\n\n\n<p>We hope you may find this Blog resourceful and helpful. If you still have concerns and need more help, please contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a><\/p>\n\n\n\n<p style=\"text-align: justify\"><b>About Us<\/b><\/br>\n<p><a href=\"https:\/\/www.greytrix.com\/\">Greytrix<\/a> \u2013 a globally recognized and one of the oldest Sage Development Partner and a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners and Sage PSG across the globe. We offer Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that require apex code incorporation into the Salesforce platform.<br><br> Greytrix has some unique solutions for Cloud CRM such as <a href=\"\">Salesforce Sage integration<\/a> for <a href=\"https:\/\/www.greytrix.com\/sage-x3-erp\/integration\/\">Sage X3<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-100-integration\/\">Sage 100<\/a> and <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/sage-300-integration\/\">Sage 300 (Sage Accpac)<\/a>. We also offer best-in-class Cloud CRM <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Salesforce customization and development services<\/a> along with services such as Salesforce <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/data-migration-support\/\">Data Migration<\/a>, <a href=\"https:\/\/www.greytrix.com\/salesforce-cloud-services\/crm-development\/\">Integrated App development<\/a>, Custom App development and Technical Support business partners and end users. Salesforce Cloud CRM integration offered by Greytrix works with Lightning web components and supports standard opportunity workflow. Greytrix GUMU&#x2122; integration for Sage ERP \u2013 Salesforce is a 5-star rated app listed on <a href=\"https:\/\/appexchange.salesforce.com\/appxListingDetail?listingId=a0N30000000psM5EAI\" target=\"_blank\" rel=\"noopener\">Salesforce AppExchange<\/a>.<br> The GUMU&#x2122; Cloud framework by Greytrix forms the backbone of cloud integrations that are managed in real-time for processing and execution of application programs at the click of a button.<br><br> For more information on our Salesforce products and services, contact us at <a href=\"mailto:salesforce@greytrix.com\">salesforce@greytrix.com<\/a>. We will be glad to assist you.<\/p>\n\n\n\n<p><strong>Related Posts:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/22\/salesforce-how-to-get-row-index-of-lightning-table-rows\/\" target=\"_blank\">Salesforce \u2013 How to get row index of lightning table rows<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2020\/12\/30\/how-to-activate-and-add-salesforce-lightning-component-on-community-experience\/\" target=\"_blank\">How to Activate and Add Salesforce Lightning component on Community experience<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/20\/how-to-add-lightning-web-components-as-custom-tabs-in-salesforce\/\" target=\"_blank\">Adding Lightning Web Components as Custom Tabs in Salesforce<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/01\/20\/how-to-set-cookies-in-http-callout-apex-salesforce\/\" target=\"_blank\">How to set cookies in HTTP Callout (APEX Salesforce)<\/a><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we will discuss how to Add Delete Row Dynamically In Salesforce Lightning Component. We shall take an example of adding account records in Salesforce using Add Delete Row functionality. Similarly, we have created 1 sample lightning component using which user can create multiple records as per requirement by Adding or Deleting new\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.greytrix.com\/blogs\/salesforce\/2021\/03\/24\/add-delete-row-dynamically-in-salesforce-lightning\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[1119,1117,1118,99,206,1055,207,208,1116,367,651],"class_list":["post-6132","post","type-post","status-publish","format-standard","hentry","category-salesforce-srv","tag-add-delete-row-dynamically","tag-add-row-dynamically","tag-delete-row-dynamically","tag-dynamic-component","tag-lightning","tag-lightning-aura-component","tag-lightning-basics","tag-lightning-component","tag-lightning-dynamic-rows","tag-salesforce","tag-salesforce-lightning-component"],"_links":{"self":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/comments?post=6132"}],"version-history":[{"count":1,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6132\/revisions"}],"predecessor-version":[{"id":6135,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/posts\/6132\/revisions\/6135"}],"wp:attachment":[{"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/media?parent=6132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/categories?post=6132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.greytrix.com\/blogs\/salesforce\/wp-json\/wp\/v2\/tags?post=6132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}