0

I have a lightning action associated to an object that is being initialized twice, meaning it runs twice the code in the apex controller. What could be the source of the problem?

This is my code:

<aura:component controller="Controller_InsertAccount" implements ="force:lightningQuickAction,force:hasRecordId,flexipage:availableForRecordHome">
    <aura:attribute name="account" type="Object"/>
    <aura:attribute name="simpleAccount" type="Object"/>
    <aura:attribute name="accountError" type="String"/>
    <force:recordData aura:id="accountRecordLoader"
        recordId="{!v.recordId}"
        fields="Result__c"
        targetRecord="{!v.account}"
        targetFields="{!v.simpleAccount}"
        targetError="{!v.accountError}"
        recordUpdated="{!c.handleRecordUpdated}"
    />

and the controller:

handleRecordUpdated : function(component, event, helper) {

         var action = component.get("c.analyzeRules");
         //set the parameters
         action.setParams({
            accid : component.get("v.recordId")
         })
         action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
                console.log("Success");  
                component.set("v.type", "success" );
                component.set("v.message", "Completed Successfully!" );
                $A.get('e.force:refreshView').fire();
            }
            else if (state === "ERROR") {
                component.set("v.type", "error" );
                var errors = response.getError();
                    if (errors) {
                      if (errors[0] && errors[0].message) {
                         component.set("v.message",errors[0].message );//Fetching Custom Message.
                         }
                      }
             }
             else {
                  component.set("v.message", 'Request Failed!' );
             }
        });
        //Send action off to be executed
        $A.enqueueAction(action);


    }
  • How are you confirming that it runs twice? Can you post the debug logs and the apex controller that you are referring to? – Jayant Das Oct 12 '18 at 14:26
  • I have a log object that the apex controller is writting in and everytime I press the action it writes the log object twice. – I. Simoes Oct 12 '18 at 14:28
  • If I ran the code in debug console without using the action it only writes once. – I. Simoes Oct 12 '18 at 14:30
4

handleRecordUpdated can fire twice, once when a record is loaded, and again when the record is updated. You'll want to check the event's "changeType" parameter to see if this is an initial record load, a change/update, or a removal/deletion. You can read more in the Loading a Record documentation. Most likely, you're only interested in either the "LOADED" or "CHANGED" type.

handleRecordUpdated : function(component, event, helper) {
    if(event.getParam("changeType") === "LOADED") {
        ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.