Thursday, May 30, 2019

Delete a record using LDS (force:recordData)

Here we taking a simple example to see how to use force:recordData to delete the record.

Put the below component in account record detail page.

deletecomp.cmp

<aura:component implements="force:hasRecordId,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome" access="global" >
    
    <aura:attribute name="recordError" type="String"/>
    <force:recordData aura:id="deleteAccount"
                      recordId="{!v.recordId}"
                      targetError="{!v.recordError}"
                      fields="Id"
                      />
    
   
  
    <!-- Contact form -->
    <lightning:card iconName="action:new_contact" title="Delete Account">
        <div class="slds-p-horizontal--small">
            <lightning:button label="Delete Account" variant="brand" onclick="{!c.deleteAccount}"/>
        </div>
    </lightning:card>
    <!-- Display errors if exists -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        
            {!v.recordError}
        
    </aura:if>

</aura:component>

deletecompcontroller.js


({
   deleteAccount: function(component, event, helper) {
       
         component.find("deleteAccount").deleteRecord($A.getCallback(function(saveResult) {
            
           // Handling state of response(SUCCESS,INCOMPLETE,ERROR)
           
             if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                alert("Account deleted successfully.");
            }
            else if (saveResult.state === "INCOMPLETE" ) {
                alert("Error in deleting record");
            }
            else if (saveResult.state === "ERROR") {
               alert("Problem deleting record, error:" + 
                           JSON.stringify(saveResult.error));
            }
            else
            {
                 alert('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
            
         }));
            
            
            
        
    }

})


Delete a record using LDS (force:recordData)

No comments:

Post a Comment