0

I had a javascript button that performed some validation and update via APEX in classic and when transitioning to Lightning I create a Lightning quick action (aura component) to replace it. A button opens the action on the Opportunity detail page in a Lightning console. The component opens fine but when trying to Confirm I got an error message "Uncaught Error in $A.getCallback() [Cannot read property 'setParams' of undefined].Callback failed:apex...".

Aura component:

<aura:component implements="flexipage:availableForAllPageTypes,force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" controller="ConvertAPEX">             

    <aura:attribute name="recordId" type="String" /> 
    <aura:attribute name="opportunity" type="Opportunity"  /> 

    <div class="slds-hide">
    <lightning:recordEditForm aura:id="editform"
                                  recordId="{!v.recordId}"
                                  objectApiName="Opportunity">        
           <lightning:outputField fieldName="Account_Status__c" aura:id="status"/> 
           <lightning:outputField fieldName="IsClosed_text__c" aura:id="closed"/>  
    </lightning:recordEditForm>    
    </div>

    <div class="slds-modal__header">             
       <h2 class="slds-text-heading_medium slds-hyphenate">Convert</h2>        
    </div>

    <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
         <p>You are about to convert this record.</p>
            <br/>
         <p>Are you sure you want to continue?</p>      
    </div>

    <div class="slds-modal__footer">
        <lightning:button class="slds-button_brand" onclick="{!c.convertRegistration}" label="Confirm" />
        <lightning:button class="slds-button_neutral" onclick="{!c.cancel}" label="Cancel"/>
    </div> 

Javascript Controller:

({
    convertRegistration : function(component) {        

        var opp = component.get("v.opportunity");     

        var action = component.get("c.ConvertRegistrationData");
        action.setParams({             
            OppId: component.get("v.recordId")
            });        

        action.setCallback(this, 
           function(response) {           

            var state = response.getState();  
            if (state === "SUCCESS"){  

            component.set("v.opportunity", response.getReturnValue());            

            var accStatus = component.find("status").get("v.value");
            var isClosed = component.find("closed").get("v.value");    

            if (isClosed === "Yes") {
                $A.get("e.force:closeQuickAction").fire();               
                var toastEvent = $A.get("e.force:showToast");

                toastEvent.setParams({
                    "title": "WARNING!",
                    "type": "warning",   
                    "message": "Can only convert for OPEN opportunities."
                });

                toastEvent.fire();      
                $A.get('e.force:refreshView').fire();

            }
            else if(accStatus !== "Unregistered" || accStatus !== "Registered") {
                $A.get("e.force:closeQuickAction").fire();

                toastEvent.setParams({
                    "title": "WARNING!",
                    "type": "warning",   
                    "message": "Can only convert for Unregistered, Registered accounts."
                });

                toastEvent.fire();      
                $A.get('e.force:refreshView').fire();

                } else {  
                    $A.get("e.force:closeQuickAction").fire();

                    toastEvent.setParams({
                       "title" : "Success",
                       "message" : "Merchant converted.",
                       "type" : "success"
                    });

                    toastEvent.fire(); 
                    $A.get('e.force:refreshView').fire();
                }
            }
            else if (response.state === "ERROR") {                 

                var errors = response.getError();                
                $A.get("e.force:closeQuickAction").fire();

                toastEvent.setParams({
                    "title": "Error!",
                    "type": "error",   
                    "message": errors[0].message
                });

                toastEvent.fire();      
                $A.get('e.force:refreshView').fire();
            }    

        });

        $A.enqueueAction(action);
    },   

    cancel : function(){
        $A.get("e.force:closeQuickAction").fire();
        $A.get('e.force:refreshView').fire();
    }    

})

APEX Controller:

global class ConvertAPEX {

   @AuraEnabled
   Webservice static Boolean ConvertRegistrationData(Id OppId)
    {
        boolean retVal = false;

        Savepoint sp = Database.setSavepoint();

        try {

            rego_Branch_Settings__c rbs = rego_Branch_Settings__c.getOrgDefaults();


            Opportunity opp = [select Id
                                        ,AccountId
                                        ,Name
                                        ,Rego__c
                                        ,IsClosed_text__c
                                        ,Account_Status__c 
                                        from Opportunity where Id=: OppId];

            Account acc = [select Id                
                                    ,AccountStatus__c
                                    from Account where Id =: opp.AccountId];

            Rego__c r = getRego(acc.Id);

            boolean registrationCreated = SetFacilitatorRegistration(acc, r, opp);

            r.Provisioning_Supporting_Docs_Required__c = '';                

                //a serie of if statements              

                    {
                        update acc;
                        update opp;
                        update r;

                        retVal = true;
                    }
                    else {
                        throw Exception('Product failed to add.');
                    }   
                }
                else
                {
                    throw Exception('No need to convert.');
                }
            }
        }
        catch (Exception ex)
        {
            Error.SendErrorEmail(ex, 'Unable to Convert Registration');
            retVal = false;
            Database.rollback( sp );
        }

        return retVal;
    }   

Thank you in advance for your help. Sylvie

0

I'm guessing a bit here, but...

in your javascript function convertRegistration you initialize the variable toastEvent only if isClosed equals 'Yes':

if (isClosed === "Yes") {
    $A.get("e.force:closeQuickAction").fire();               
    var toastEvent = $A.get("e.force:showToast");

otherwise, 'toastEvent' is neither declared nor initialized:

else if(accStatus !== "Unregistered" || accStatus !== "Registered") {
    $A.get("e.force:closeQuickAction").fire();

    toastEvent.setParams({

Here, toastEvent is just undefined, just as it is further down, as it has not been initialized.

Either declare and assign toastEvent before the if-clause, or do that within each of the decision blocks.

I assume, your current code would work, if IsClosed_text__c contains 'Yes'.

| improve this answer | |
  • Thank you @Florian Heer, it solved this problem. But now I have another one! When submitting I got the 2nd toast that fire even if accStatus = unregistered. Any Idea? Thanks – ultima67 Jun 2 at 4:23
  • 1
    Well, your logic is wonky if(accStatus !== "Unregistered" || accStatus !== "Registered"): if accStatus equals 'Unregistered', then it is not equal to 'Registered' => the second part of your if-clause becomes true, and due to the logical OR, the whole expression is true. In fact, the whole expression is always true, no matter what accStatus contains. – Florian Heer Jun 2 at 12:10

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.