0

Could somebody please help me with my Aura code? It works fine and does exactly what I need it to do but the only issue I'm having is, once the aura component loads, the lightning:spinner keeps randomly showing about 3 times and then finally goes away. Am I perhaps implementing the spinner in the wrong fashion? or is my recordUpdated() maybe not correctly implemented? Thanks for any help you can provide!

ErroHandler.cmp

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">

<aura:attribute name="record" type="Object"/>
<aura:attribute name="RecordSimp" type="Object"/>
<aura:attribute name="recordError" type="String"/>
<aura:attribute name="recordSuccess" type="String"/>
<aura:attribute name="showSpinner" type="boolean" default="false"/>

<aura:handler event="aura:waiting" action="{!c.handleShowSpinner}"/>
<aura:handler event="aura:doneWaiting" action="{!c.handleHideSpinner}"/>

<force:recordData aura:id="RDLoader"
                  fields="ErrorMessage__c,ErrorMessagePersistent__c"
                  recordId="{!v.recordId}"
                  targetRecord="{!v.record}"
                  targetFields ="{!v.RecordSimp}"
                  targetError="{!v.recordError}"
                  recordUpdated="{!c.recordUpdated}"
                  mode="EDIT"/>

<div class="Error Detail">
    <lightning:card iconName="utility:error" variant="error" title="Error!">
        <aura:if isTrue="{!v.showSpinner}">
            <lightning:spinner variant="brand" size ="medium"/>
        </aura:if>
        <div class="slds-p-around_medium">
            <p class="slds-text-heading--small">
                <lightning:formattedText value="{!v.RecordSimp.CaseErrorMessage__c}"/>
                <br></br><br></br>
                <lightning:formattedText value="{!v.RecordSimp.ErrorMessagePersistent__c}"/>
            </p>
            <br></br>
            <lightning:button aura:id="submitbuttonid" variant="brand" label="Clear Message? Click here" onclick="{!c.handleClick}" disabled = "false"/>
        </div>
    </lightning:card>
</div>

</aura:component>

ErrorHandlerController.js

({   
recordUpdated: function(component, event, helper){
    var eventParams = event.getParams();
    if(eventParams.changeType === "LOADED"){
        var record = component.get('v.RecordSimp');
        record.ErrorMessage__c = '';
        record.ErrorMessagePersistent__c = '0 errors';
    }
},

//event="aura:waiting"  
handleShowSpinner: function(component, event, helper) {
    component.set("v.showSpinner", true); 
},

//event="aura:doneWaiting"
handleHideSpinner : function(component,event,helper){
    component.set("v.showSpinner", false);
},

handleClick: function(component, event, helper)
{     
    let button = component.find('submitbuttonid');
    button.set('v.disabled',true);
    
    component.find("RDLoader").saveRecord($A.getCallback(function(saveResult){
        if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
                "title": "Success",
                "message": "Thank you!",
                "type": "success"
            });
        }else {
            var toastEvent = $A.get("e.force:showToast");
            toastEvent.setParams({
                "title": "Error",
                "message": "Please try again.",
                "type": "error"
            });
        }
        toastEvent.fire();
    }));
}

})

1
  • Please do not change or destroy the content of your questions such that the answers you've received no longer provide value. I've rolled this question back to the original form.
    – David Reed
    Commented Mar 29, 2022 at 20:24

1 Answer 1

1

Don't use aura:waiting and aura:doneWaiting. As the documentation says:

The aura:waiting event is deprecated. Execute logic after queueing an action instead of using this event. A component is waiting for a response for an action until the action’s callback is invoked. The aura:doneWaiting application event is fired for every server request, even for requests from other components in your app. Unless your component is running in complete isolation in a standalone app and not included in Lightning Experience or the Salesforce mobile app, the container app may trigger your event handler multiple times. This behavior makes it difficult to handle each event appropriately.

Show the spinner when the component loads, and then hide it when recordUpdated is fired.

6
  • thank you so much! got it working now :) no more random showing of the loading spinner :)
    – user113071
    Commented Mar 29, 2022 at 10:09
  • Is there a way to hide the lightning:button and only display it to a certain profile?
    – user113071
    Commented Mar 29, 2022 at 14:54
  • @Krpyto202 Yes, you'd just want to load the user's profile, then check if it is the right profile to display the button. That requires a bit more code than I put in a comment, however. See this answer, which describes how to do so for Role; you can change for this to work with Profile.
    – sfdcfox
    Commented Mar 29, 2022 at 15:24
  • I've updated the code above with what I have now, what I'm seeing is, when I define the role within the button, it shows regardless of whatever role I specify, even if it's fake etc. 'test101'. I've also created a apxc. class which is exactly like the one in your message. I am trying via Roles for now, and then I'll switch to profile once I get this working
    – user113071
    Commented Mar 29, 2022 at 16:28
  • @Krpyto202 expressions start with {! and end with }. An expression, if present in an attribute, must be the only thing in the attribute (so, no strings on the outside). Attribute names in an expression do not start with !; if you use that, it will perform the NOT operation (true=>false, false=>true). It should look like: {!v.rolename == 'System Administrator'? 'slds-button_brand slds-align_absolute-left slds-size_2-of-2':'slds-hide'}
    – sfdcfox
    Commented Mar 29, 2022 at 16:42

You must log in to answer this question.