3

we're having an issue with getContent() in asynchronous context, and I just can't find any way to work around it... We're exposing invocable Apex in our managed package, and one of them allow our customers to generate pdf. The code is pretty straightforward (instantiate a PageReference with url pointing to a Visualforce page, call getContent() and store it in Documents) and it works great outside of the package (dev org, or even packaging org). The method doing this job is annotated @future.

But when we package it and call the invocable via Process Builder, we hit the following issue :

org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded.

Has anyone seen this before? Thanks for your help!

2 Answers 2

3

Here is the solution I found after lots of investigation. The issue (org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded) was only happening when I was trying to generate a pdf out of a Visualforce page NOT in our managed package. The way we were building the URL used to instantiate the PageReference was apparently wrong.

Before the fix :

String lv_url = '';                    
if (String.isBlank(lv_page.NamespacePrefix)) {
    // Visualforce page outside of our package
    lv_url += URL.getSalesforceBaseUrl().toExternalForm();
    lv_url = lv_url.replace('sofactoapp', 'c');
}
lv_url += '/apex/';

if (String.isNotBlank(lv_page.NamespacePrefix)) {
    // for our internal Visuaforce template only
    lv_url += lv_page.NamespacePrefix + '__';
} 
// adding the template DeveloperName
lv_url += lv_template;

PageReference lv_pdf  = new PageReference(lv_url);

And this was working great in synchronous mode.

After the fix :

String lv_url = '/apex/';           
if (String.isNotBlank(lv_page.NamespacePrefix)) {
    // Visualforce page within managed package
    lv_url += lv_page.NamespacePrefix + '__';
} else {
    // Custom Visualforce page
    lv_url += 'c__';
}

// adding the template DeveloperName
lv_url += lv_template;

PageReference lv_pdf  = new PageReference(lv_url);

Basically, building a relative url and appending c__ before the Visualforce page DeveloperName was the correct way to go. This works in both synchronous and asynchronous mode.

0

instead of using this '*******************.sandbox.my.salesforce.com/apex/vfpagename'

use the exact url that you see by previewing the vfpage which contains '*******************c.sandbox.vf.force.com/apex/vfpagename'

This has solved my issue both for synchronous and asynchronous.

You must log in to answer this question.

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