3

I have Manage Package, where is some logic which sends Emails with EmailFileAttachment ( using Visualforce page rendered as PDF ) to Customers.
I want to add possibility to users to use theirs Custom Visualforce Page when sending emails.

I added Custom Setting for it,
Where a user can enter the Visualforce Page Name and if this Name is set I use it.

-------Issue-------
When I set there a Visualforce Page which is not from my Managed Package I get the error :

An internal server error has occurred\nError ID: 1687133430-16791 (-553401195)

This error throws on next line :

Blob receiptAttBody = myVisualforcePageRef.getContentAsPDF();

The main logic looks like that :

PageReference myVisualforcePageRef = new PageReference('/apex/'+ myVisualforcePageName); // this line works
myVisualforcePageRef.getParameters().put('id',recordId); // I tried to remove this line but it did not help
Blob receiptAttBody = myVisualforcePageRef.getContentAsPDF(); // error caused by this line

When I set there a Visualforce Page from the managed package it works ok.

However when I set there any page outside the package it throws the error!

I tried to add the simplest page without any logic and it still does not work.

Maybe there is any limitation on using Visualforce Pages ( which is outside of Managed Package ) inside in managed package code ?

I saw this question Accessing a external VF page from managed package , However it did not help me.

Any help will be appreciated
Thank you

6
  • 1
    What I can say is that we do almost exactly the same as you here in our managed package and it works fine as long as the external VF page is renderAs="pdf", uses the appropriate standardController (for us our custom object) and has the required extensions with both showHeader and sidebar set false. I don't know if all these properties have to be set for the page, but these certainly work (even when the controller extensions are only public rather than global I believe).
    – Phil W
    Aug 11, 2022 at 14:57
  • @PhilW thank for your answer, I will try it Aug 11, 2022 at 15:14
  • 1
    If memory serves me correctly, you have to prefix your unmanaged page reference with the default namespace don't you? e.g. PageReference myVisualforcePageRef = new PageReference('/apex/c__'+ myVisualforcePageName);. Could that be the issue? Aug 11, 2022 at 18:53
  • 1
    Adding to @PhilHawthorn's comment. The c__ prefix for a Visualforce page in the subscriber org is covered in salesforce.stackexchange.com/a/25864/102 Aug 11, 2022 at 19:27
  • 3
    @PhilHawthorn Thank you so much, I added the prefix and it did resolve the issue. Aug 12, 2022 at 8:20

1 Answer 1

3

Rendering an unmanaged page from within a Managed Package requires that you specify the default namespace within the page url. I would imagine that this is because by default, the platform is trying to resolve the page from the managed namespace.

So:

PageReference myVisualforcePageRef = new PageReference('/apex/' + myVisualforcePageName);

Becomes:

PageReference myVisualforcePageRef = new PageReference('/apex/c__' + myVisualforcePageName);

3
  • 1
    Thank you so much, it did resolve the issue. Aug 12, 2022 at 8:36
  • 1
    Very interesting. I wonder why we didn't need to use "c__" (I haven't looked at this in a very long time, so perhaps something changed or I simply missed that this was done). Anyway, great to know!
    – Phil W
    Aug 12, 2022 at 10:14
  • 1
    Maybe different circumstances then @PhilW because we had to do this way back in a package I built in 2012 for emailing invoices Aug 12, 2022 at 11:23

You must log in to answer this question.

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