10

Right now I have a "Refresh" button on my page that collects some data and sends it to this method:

public static Boolean cacheData(String index, MyObject data) {
    try {reportPartition.put(hash(index), data);}
    catch(Exception e) {
        System.debug('Problem adding data to ReportData Cache: ' + e.getMessage());
        return FALSE;
    }
    return TRUE;
}

The method is used elsewhere and works find but for some reason right now my debug is coming back with the following Exception message:

platform cache put operation is not allowed here

The documentation page seems to confirm this shouldn't be a problem so I'm trying to determine what other factors or flags should I be looking for that could throw this exception at that time? Is there something that happens if the pagereference redirects afterwards or the page rerenders somewhere that I should be aware of?

Exception Details:

It appears the exception being thrown is Cache.PlatformCacheInvalidOperationException which from the documentation simply says:

A cache put or remove call is made that is not allowed. For example, when calling put or remove inside a Visualforce constructor.

This is being launched tomorrow afternoon so truly appreciate an quick help I can get ;)

P.S. We had polling on as well but commented that out for now unsure if that was causing some context in which put was not allowed

UPDATE: PageReference Within a Component As suggested from comments below I've been attempting put() calls throughout my application just to verify they are allowed where I expect them and at which point the context changes and they are not. Here are my current findings:

  • Page - CommandButton/PageReference: Allowed all the way through process
  • Component - CommandButton/PageReference: Not Allowed in the PageReference method or anything afterwards

(note: this is a complex component with sub-components within it, but not sure how that affects having a "constructor" within the pagereference. The component controller also has 2 wrapper classes, not sure if that is related)

UPDATE: Gist summary of code As per request in comments here is a summary of my code. Please don't judge it too critically. Because I could not solve my problem described here I came up with a solution I fittingly coined as "FluxCapacitor". For the sake of solving the problem because it is also constructed in the widget I figured I'd include it in this gist.

The path of execution is dashboard.page (with Dashboard.cls as its controller), which then uses the widget.component (with Widget.cls as its controller), and lastly the metric.component (with MetricController.cls as its controller)

  • Are you sure you're not calling it from your constructor, even indirectly? The first document you link to has a put call inside a PageReference method. – Adrian Larson Mar 6 '16 at 15:03
  • 1
    I will continue to debug with that in mind as the error seems to insinuate that is the problem. But as off right now many of my builder classes don't even have constructors (pointless with static methods and DML limits). For example my main page contructor uses an action="{!init}" like the documentation suggest and I then tested a Platform Cache put() directly in that init just to make sure it would work. Making progress will update when I have more – Xtremefaith Mar 9 '16 at 19:43
  • 1
    It might be worth putting a gist on GitHub so the community can analyze in more depth. – Adrian Larson Mar 10 '16 at 20:07
5

As I attempted storing a custom record through another PageReference call within a component I got the following error: System.LimitException: DML currently not allowed

While searching for answer I came across this solution. I realized that the DML limitations are often the same for Platform Cache put() method and so I decided to try the same solution here and it worked!

In short you have to allowDML on your component:

<apex:component controller="MyComponentController" allowDML="true">

Now Cache.OrgPartition.put() is acceptable within the context of your component and its pagereference(s).

  • 1
    Makes a lot of sense. Can you update your question title to indicate that the problem is based on using a Visualforce Component so OP is more useful in the future? – Adrian Larson Mar 14 '16 at 22:45

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.