3

I have a method which I am calling imperatively in LWC but it is annotated with Cacheable = true at server end. How can I refresh the result of that method ? I know with @wire adapter we can refresh method using refreshApex(), but what if we want to refresh a method which is called imperatively but is annotated with cacheable = true?

Any suggestions?

3
  • Why are you calling imperatively? Any specific reason?
    – javanoob
    Commented Mar 1, 2020 at 16:16
  • 1
    Because, on the page load I don't need any info from that method. On the click of button,I want to query some info that is why I am calling it imperatively.
    – sfdc07
    Commented Mar 1, 2020 at 17:31
  • If you use window.location.href = ''; in your js method that calls apex imperatively, does that work? This works for me. (a bit hacky as the best practice route is wire method with import refresh) Commented Aug 3, 2022 at 2:30

3 Answers 3

4

According to the documentation it isn't possible to clear the cache for an imperative invocation (search for "Refresh the Cache for a Wired Property"):

You can't refresh data that was fetched by calling an Apex method imperatively.

As such the only way to resolve this is to either add a variable parameter (this is wasteful on the client side as many cache entries will be created for no good rason), remove the cacheable marker or to switch to using a wire. To be honest, the main purpose for using a wire (apart from having to write slightly less client-side code) is to benefit from caching, so I'd suggest removing the cacheable. If you are wanting to keep it because you also use the method with a wire, add a second method that can be used imperatively and have this call the first one (the aura enabled annotation is only applied when invoked by the client).

1

If is called imperatively then you must call it imperatively again. A workaround for the cacheable annotation to receive fresh data would be to pass an additional datetime parameter to the method (with now for example), therefore for each difference in parameters the method will be called again instead of returning a cached version (of course this leaves the concept of having a cacheable method pretty much useless)

0

When calling the apex method without wire service, the refreshApex() will not work for refreshing the cache.

And, as Phil W's reply, there is no built-in method to clear the cache in the case but the cache issue is still able to be solved.

What you need to do is just to remove the (Cacheable=true) annotation from the Apex method in the Apex class and you can process the apex method in the LWC without cache issue. This tip works perfectly for me and it will work for you.

In the doc, it states:

In the following scenarios, you must call an Apex method imperatively as opposed to using @wire.

  • To call a method that isn’t annotated with cacheable=true, which includes any method that inserts, updates, or deletes data.
  • To control when the invocation occurs.
  • To work with objects that aren’t supported by User Interface API, like Task and Event.
  • To call a method from an ES6 module that doesn’t extend LightningElement

You must log in to answer this question.

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