We are building a lightning component to be used as a ‘New’ Action Override.
This question is NOT related to our inability to access the Parent Id of the new record. An example of that issue is “I click on a ‘New’ Opportunity on a related List on the Account Detail page, and there is NO official way of accessing the Account ID from within the Lightning component”. This deficiency has been discussed ad nauseum in other threads, and there is STILL no solution for it.
This question is NOT related to the fact that the behavior of the New override is inconsistent with the native ‘New’ functionality (where a modal dialog will popup OVER the existing page). This has also been discussed forever and we still cannot do this…..
This question IS about how to reliably execute initialization code for such a component.
We use the ‘init’ event handler right?
Has anyone ever done this?
Our observations so far suggest that the event WILL be fired when the component is actually created (just like the documentation says), but that the component sometimes just ‘hangs around’ after the user has completed the action or navigated away from the page or whatever, and that the existing instance of this component will be used for the subsequent invocation of the ‘New’ action.
We believe that the component still exists, as handlers for the ‘render’ event, or ‘locationChanged’ or anything else you can think of, fire when the ‘New’ action is invoked again (but the init event does NOT fire).
How do we reliably execute code ONE TIME at the start of the ‘New’ Action override then?
Use another event, like ‘render’ which appears to always fire? No way we are doing that. If our one time code needs to update an attribute on the component we end up in an infinite loop mess.
We somehow destroy the component ourselves (perhaps when we navigate away from the page by handling locationChange? No way we’re doing that either. Of course we’ve tried :) and it simply confuses everything. In the example given above, returning to the Account detail page and pressing ‘New’ does not create a new instance of the component, and so does not fire ‘init’ and we land on an empty page in outer space. So, that is just a big hack anyway…….
So, assuming we have created a new Lightning component to be used as an Opportunity ‘New’ override We can reliably recreate this issue by
- Navigate to an Account Detail page.
- Click on the ‘New’ button on the Opportunity related list.
- Observe that the ‘New’ override ‘init’ event was actually fired. Cool..
- Navigate to a different account.
- Again, click on the ‘New’ opportunity button. You will observe that the ‘init’ event did NOT fire. The ‘state’ of the component is how it was the last time it executed. :( No ‘init’ event was fired for us to do our thing….
Hmmmm?? Could anyone please confirm / deny … whatever that you have also perhaps experienced this behavior? Perhaps we are doing something wrong here? I’m not of course going to bore you with copy pasted code. It is pure vanilla 101 lightning component code, and I trust the explanation of what we’re doing is clear enough.
Thanks for any help.
e.force:closeQuickAction
event seemed to do the trick for me. – Antonio Manente Jun 1 '18 at 19:22