1

I'm new to Salesforce lightning. I'm trying to build a quick action that performs backend logic based on a condition else display a toast. I have created the component and associated it to an object based quick action. Now, after invoking the toast event, I'm trying to close the quickAction modal popup using $A.get("e.force:closeQuickAction").fire(); This doesn't seem to work.

So, I have removed the controller logic and just added that one line and it still doesn't work.

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

Requesting to guide me on this. Thanks!

1
  • Please be more elaborate on your doubt. doInit is something that runs on Load of a lightning component / action. Once you open a modal , you might need to close it using quick action.
    – AjaySFDC
    Commented Aug 21, 2017 at 0:56

3 Answers 3

5

The reason why it is working when invoked in callback code because there is modal listener ready for the event and then modal listener can close the model.

But if you write your logic on doInit ( I assume you are registering with init event) then there is no modal which is listening. So modal will not be able to close.

If I am not wrong here you want to run some apex logic and not show modal to user since you don't have anything to show to user.

I would suggest show the modal with some message saying that "YOUR XYZ OPERATION is in progress."

As the modal is completely loaded you can fire close event upon which modal will get closed.

Note: You can destroy the component in doInit but it will only destroy contents of modal not the component, so its better to go with the solution where you can let user know some action is going on.

Another Update (Date : Aug 2019) If you are required to close the modal in init without making any server call then you can simulate async call nature by

setTimeout(()=>{
           let quickActionClose = $A.get("e.force:closeQuickAction");
           quickActionClose.fire();
        },1000);

This will make sure that component is loaded and ready to listen for close event.

0
1

Here is an example that i did for closeQuickAction to fire.

You need to add the lightning component as a quick action first and then include the same action on the lightning page layout.

js controller:

({
    doInit : function(component, event, helper) {

        var action = component.get("c.getRecordUpdate");

        action.setCallback(this, function(response) {
            var output = response.getReturnValue();

            var state = response.getState();
            if(component.isValid() && state == "SUCCESS" && output.isSuccess){
                $A.get("e.force:closeQuickAction").fire();
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    "title": "Success!",
                    "message": "AWB Details updated to Case Successfully!"
                });
                toastEvent.fire();
                $A.get('e.force:refreshView').fire();
            } else if (state == "ERROR" || !output.isSuccess) {
                var errors = response.getError();
                if (errors[0] && errors[0].message) {
                    // Did not catch on the Server Side
                    component.set("v.message", errors[0].message);
                    component.set("v.messageError", true);
                } else if ( !output.isSuccess ) {
                    // Did catch on the Server Side
                    component.set("v.message", output.message);
                    component.set("v.messageError", true);
                }
            }

        });
        $A.enqueueAction(action);
    }
})
2
  • Thanks for the response. I have tried it in the callback too and it is working there, but trying to understand why it fails when invoked directly. Commented Aug 21, 2017 at 1:49
  • Why do you want to close the quick action if your are not invoking any button events after the page loads completely. If you want to still close the quickaction then you can use registerEvent and there is some attribute "afterLoaded" attribute associated with it. Not sure of the correct attribute name . please check once.
    – AjaySFDC
    Commented Aug 22, 2017 at 2:51
1

Use below in renderer file in aura omponent

afterRender  : function(cmp, helper) {
    var ret = this.superAfterRender ();
    // do custom rendering here
    $A.get("e.force:closeQuickAction").fire();
},
1
  • This is the correct solution, though it would be nice if there was an explanation to go with it. Adding the closeQuickAction event in the Renderer file ensures it runs as soon as the component completes rendering. The reason it doesn't work in the Controller is because the component has not completed rendering when it executes, causing it to thrown an error like error while creating component for lightning component quick action [action failed: one:dockingpanelmanager$controller$onnotify [cannot read properties of undefined (reading 'gid')]]
    – Bryan
    Commented Nov 26 at 16:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .