2

I've been dabbling around Lightning Components (Aura). I decided to use Aura so I can display may Lightning Web Component (LWC) in a pop-up window (or modal) through the click of a Custom Quick Action. However, I keep getting an error.

Basically, all I'm trying to accomplish with Aura is to:

  1. Fetch the Stage__c field by calling an Apex method.
  2. Check if that Stage__c's field value is equal or not equal to "Closed".
  3. Display a toast message and close the component if Stage__c == "Closed", in succession.

I've written a simple code to do this but I can't seem to move forward and call the Apex correctly as I'm always receiving this error:

enter image description here

So, I checked my Apex method to make sure it has @AuraEnabled annotation, but to no avail.

Here's what I have:

CMP

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" >
    <aura:attribute name="recordId" type="Id" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
    <c:editProduct recordId="{!v.recordId}" onclose="{!c.closeQA}"/>
</aura:component>

Controller JS

({ doInit : function(component, event, helper){
        helper.getOppty(component);
    }
})

Helper JS

({ 
getOppty : function(component, event, helper) {
    var action = component.get("c.getOpptyCurrency");
    action.setParams({
        opptyId: component.get("v.recordId")
    });
    action.setCallback(this, function(a){
        var state = a.getState();
        var returnVal = a.getReturnValue();
        console.log('Return Value :', returnVal);

        if(state == "SUCCESS"){
            for(i=0; i < returnVal.length; i++){
                var stage = returnVal[i];
                stage.Stage__c;
            }
        }else{
            console.log("Failed with state: " + state);
        }
    });
    $A.enqueueAction(action);

    console.log('Stage :', stage.Stage__c);
    if(stage.Stage__c === "Closed"){
        this.closeQA(component, event, helper);
        this.showError(component, event, helper);
    }
},

showError : function(component, event, helper) {
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            title : 'Error',
            message:'This is an error message',
            duration:' 5000',
            key: 'info_alt',
            type: 'error',
            mode: 'dismissible'
        });
        toastEvent.fire();
    },

closeQA : function(component, event, helper) {
        $A.get("e.force:closeQuickAction").fire();
    }
})

Apex

@AuraEnabled
public static Opportunity__c getOpptyCurrency(string opptyId){
        Opportunity__c oppty = [SELECT Id, Stage__c
                                FROM Opportunity__c
                                WHERE Id =: opptyId LIMIT 1];
        return oppty;
     }

Can anyone help point me in the right direction? What am I doing wrong exactly in this case

1
  • One side note on the apex method parameter, you should use the Id type rather than a String when you're working with a record id to easily prevent things such as SOQL injection. getOpptyCurrency(Id opptyId)
    – Mark Pond
    Jun 1 '20 at 15:42
3

You need to define the controller for the Aura Component.

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" controller="myApexClass">

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.