function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anand JeevakanAnand Jeevakan 

Error deleting a record in Lightning

Hi,
Getting this error while trying to delete a record in lightning (action):

Uncaught Action failed: force:recordData$controller$deleteRecord [Must load record in force:recordData before delete.]
Callback failed: aura://ComponentController/ACTION$getComponent
Callback failed: aura://ComponentController/ACTION$getComponent

Component code
<aura:component implements="force:lightningQuickAction,force:hasRecordId">
	<aura:handler name="init" value="{!this}" action="{!c.myAction}" />
    <aura:handler name="render" value="{!this}" action="{!c.cancelContactMerge}"/>
         <aura:attribute name="recordId" type="Id" />
        <aura:attribute name="record" type="Search_Merge_Duplicate_Contact_Pair__c" />
        <aura:attribute name="simpleRecord" type="Search_Merge_Duplicate_Contact_Pair__c" />
    
        <force:recordData aura:id="recordHandler"
                  recordId="{!v.recordId}"
                  targetRecord="{!v.record}"
                  targetFields="{!v.simpleRecord}"
                  layoutType="FULL"
                  />
</aura:component>

JS Controller:
({
	myAction : function(component, event, helper) {
      
	},
    
    cancelContactMerge : function(component, event, helper) {
        var acctId = component.get("v.simpleRecord.Account__c");
        component.find("recordHandler").deleteRecord($A.getCallback(function(deleteResult) {
            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful 
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (deleteResult.state === "SUCCESS" || deleteResult.state === "DRAFT") {
                // record is deleted
                console.log("Record is deleted.");
                // Close the Quick Action
                $A.get("e.force:closeQuickAction").fire();
                // Navigate back to Account to Keep
                var navEvt = $A.get("e.force:navigateToSObject");
    					navEvt.setParams({
      					"recordId": acctId
   						 });
    			navEvt.fire();
                
            } else if (deleteResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (deleteResult.state === "ERROR") {
                console.log('Problem deleting record, error: ' + JSON.stringify(deleteResult.error));
            } else {
                console.log('Unknown problem, state: ' + deleteResult.state + ', error: ' + JSON.stringify(deleteResult.error));
            }
        }));
            

	}
 
    
})

Tried the code  here (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_delete_record.htm) - it worked but the requirement doesn't want a confirmation pop up.

Thank you.
 
Devi ChandrikaDevi Chandrika (Salesforce Developers) 

Hi Anand,
This error is because you are calling cancelContactMerge controller action to delete record in render handler.It is trying to delete record before the record is loaded.Try invoking this controller action after the record is loaded completely

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards

Anand JeevakanAnand Jeevakan
Hi Devi, Please help me know how to invoke a controller action with out onclick event. Regards Anand Jeeavakan
Deepali KulshresthaDeepali Kulshrestha
Hi Anand,

You can put your handler inside aura:if tag so that they would run after the record and its id are loaded.

Please see below:

<aura:if isTrue="{!v.record!=null}">
    <aura:handler name="init" value="{!this}" action="{!c.myAction}" />
        <aura:handler name="render" value="{!this}" action="{!c.cancelContactMerge}"/>
<aura:if/>

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Anand JeevakanAnand Jeevakan
Hi Deepali, Thank you. But I got an error: Failed to save CancelContactMerge.cmp: c:CancelContactMerge:17,72: Invalid attribute "name": Source I'm not able to find any references for conditional invocation of controller method. Please share the code if you have tried in earlier. Regards Anand Jeevakan
Anand JeevakanAnand Jeevakan
Hi,
If anyone is encountering the same issue, this is the fix:
Check for the standard duplicate rule in Account object.
I deactivated the standard duplicate rule for account and the error disappeared.
Thank you.