0

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!

  • 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 Aug 21 '17 at 0:56
3

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.

| improve this answer | |
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);
    }
})
| improve this answer | |
  • 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. – Prudhvi Konda Aug 21 '17 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 Aug 22 '17 at 2:51
0

I am facing same issue that $A.get("e.force:closeQuickAction").fire() not closing modal popup although i am using it in setcallback. Below is my code please let me know what i am doing wrong.

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

    var qid = component.get("v.recordId");
        var action = component.get("c.saveQuote");
        //var action2 = component.get("c.getServerSideMessage");
        //action.setParams({"quote": component.get("v.quote")});
        action.setParams({"QID": qid});

        action.setCallback(this, function(response) {
            var state = response.getState();
            if(component.isValid() && state == "SUCCESS"){
                var c = response.getReturnValue();
                component.set("v.quote", c);
                $A.get("e.force:closeQuickAction").fire();
                $A.get('e.force:refreshView').fire();
            }

            else {
                console.log('There was a problem : '+response.getError());
            }
        });
        $A.enqueueAction(action);


} 

})

| improve this answer | |
  • Hi Faiza, The code seems to look fine. Please make sure that it is not going into the else block. Try adding an alert or a toast event in the if block and check if that is displayed on the browser. Thanks! – Prudhvi Konda Sep 5 '17 at 19:02
  • Its not going in else event as parent window is getting refreshed $A.get('e.force:refreshView').fire(); with this command but popup is not getting closed. – Faiza Naz Sep 12 '17 at 10:16

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.