function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dbelwooddbelwood 

Calling PageReference.getContent().toString() not seeing recently inserted data

Hi All,

Quick question, though I think I know the answer.  I've got a quite complex apex method running as a result of being called from a VF command button.  During this method I insert and update several records.  At the end I am trying to generate html and plain text representations of VF pages to persist to an SObject that can then be picked up by a custom C# app and posted via SMTP.  This seems relatively straightforward, but I'm guessing that PageReference.getContent() runs in a separate execution context, so won't have access to data inserted in another context.

Question 1, if I call PageReference.getContent() from within an Apex method, does it run in a separate context and thus not have access to the DML changes in the current context?

Question 2, if 1 is true, is there any way to accomplish this?  I've tried persisting SObject-based parameters, but this is just the same thing as above, really.

Hope this makes sense and appreciate any help anyone can offer.

Best regards,
Dan
Ron HessRon Hess
Question 1, if I call PageReference.getContent() from within an Apex method, does it run in a separate context and thus not have access to the DML changes in the current context?

The DML changes commit at the end of the request (your action method) , since you are not yet at the end i believe the call to getContent() will not see the uncommitted DML. 


Question 2, if 1 is true, is there any way to accomplish this?  I've tried persisting SObject-based parameters, but this is just the same thing as above, really.

I think you can use @future to perform this.  Your action method would do the DML, then call an @future method and return.
The @future method would run and see the changed data when calling getContent().
dbelwooddbelwood
Ron,

Thanks for the quick reply.  I've previously made a separate @future method to do just such a thing, but received the highly cryptic error - "Inline content was null" error in the output Visualforce and switched back.  Have you seen this error before, if I could track down what this refers to, I'd have a better chance of successfully using the @future strategy.

Many thanks,
Dan
Ron HessRon Hess
the @future method may need to setParameter() to configure things for the page you are loading.

i have not seen that error.
dbelwooddbelwood
Hi Ron,

Thanks for the help, I'm beginning to think that one can't produce VF content as a string from a @future method.  I tried a very simple test:
Code:
global class Test {
 @future
 global static void testVFToString() {
  insert new Email_Record__c(To__c = 'dan@dfs-solutions.co.uk', From__c = 'test', Cc__c = '', Bcc__c = '', Subject__c = 'testVFToString', Plain_Text__c = Page.TestPattern.getContent().toString(), HTML_Text__c = Page.TestPattern.getContent().toString());
 }
}


Code:
<apex:page >
Hello
</apex:page>

 
Calling;

Code:
Test.testVFToString();

results in;
<h3><em>Exception: null in inline content TestPattern</em></h3>

Perplexing...

Dan

 

dchasmandchasman
Using PageReference.getContent() is not currently supported from @future methods - should be blocked and give you a good error message but this one slipped through - sorry about that.

I am thinking about a solution that will work for you now.


Message Edited by dchasman on 01-08-2009 02:59 PM
Scott.MScott.M
I am also attempting to do something like this, if anyone can come up with a solution that would be great! :)
tom cusack.ax852tom cusack.ax852

Its not possible to to use these methods from a @Future Method
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_future.htm

or the following.

• Triggers
• Scheduled Apex
• Batch jobs
• Test methods

• Apex email services

cristinacristina
Has someone found a solution to send Html content on triggers or future methods?