4

I have a simple vf page that just calls a controller's method on the action attribute

<apex:page standardController="C_Object__c" extensions="Ext1" action="{!method1}"> <apex:outputText value="{!C_Object__c.Name}" rendered="false"/> </apex:page>.

On the method I call another vf page that renders as PDF to get its content, create a new Chatter file (ContentVersion) and redirect the user to the file just created (all this works just fine).

The problem I'm having is referencing the first visual force page, when it's called from a Detail Page Button everything works fine (the file gets created and the user is redirected to it) but when I call this visual force page from a Publisher Action (Object specific action) the file doesn't get created so there is nowhere to redirect to.

I need the standardController object record Id and Name value in order for this to work, is this Id not pass to a publisher action? (this will explain everything) or am I missing something else?

Update

(RESUME) Extension code:

private final C_Object__c a;

public Ext1(ApexPages.StandardController stdController) {
    this.a = (C_Object__c)stdController.getRecord();
}

public PageReference generatePdf(){
    String objectLabel = Schema.SObjectType.C_Object__c.Label;
    String objectName = a.Name;
    Id objectId = a.Id;

    String title = objectName + ' ' + objectLabel;

    PageReference pdfPage = Page.vfp_PdfPage;
    pdfPage.getParameters().put('id',objectId);

    /* generate the pdf blob */
    Blob pdfBlob = pdfPage.getContent();

    List<ContentDocument> oldFiles = [SELECT Id FROM ContentDocument WHERE Title = :title limit 1];

    if (oldFiles.size() > 0) {
        /* Update File */
        delete oldFiles;

        ContentVersion file = new ContentVersion();

        file.Title = title;
        file.PathOnClient = objectId + '.pdf';
        file.VersionData = pdfBlob;
        file.Origin = 'H';

        insert file;

        return new PageReference('/' + file.Id);
    } else {
        /* Create File */
        ContentVersion file = new ContentVersion();

        file.Title = title;
        file.PathOnClient = objectId + '.pdf';
        file.VersionData = pdfBlob;
        file.Origin = 'H';

        insert file;

        return new PageReference('/' + file.Id);
    }
}
1

Jose,

It might be helpful for you to post relevant code from your Extension controller and final VF Page.

I have tried to replicate this scenario and am able to pass Ids along just fine from an Object-specific Publisher Action done using a Custom Visualforce Page that has an extension controller into a final destination page.

For my setup, I have an initial Visualforce Page that looks identical to yours, and this is what I am using for my Object-specific Publisher Action:

<apex:page standardController="Account" extensions="AccExt" action="{!method1}">
   Id: <apex:outputText value="{!Account.Id}"/><br/>
   Name: <apex:outputText value="{!Account.Name}"/><br/>   
</apex:page>

Where AccExt defines an action method that sends the user to a target VF page (representing in your case the VF page that generates a PDF):

public with sharing class AccExt {

    public Account a; 

    public AccExt(ApexPages.StandardController controller) {
        a = (Account) controller.getRecord();
    }

    public PageReference method1() {
        PageReference p = System.Page.AccPage2;
        p.getParameters().put('id',a.Id);
        return p;
    }

}

Where AccPage2 looks like this:

<apex:page standardController="Account" renderAs="pdf">

    ACCOUNT PAGE 2

   Id: <apex:outputText value="{!Account.Id}"/><br/>
   Name: <apex:outputText value="{!Account.Name}"/><br/>   

</apex:page>

And the output is, as expected, a PDF rendered in context of the original Account:

enter image description here

So my conclusion is that the standardController object record Id is getting passed along just fine from the initial object-specific Publisher Action into your extension controller. Possibly you just need to pass this Id along from your extension controller by adding parameters to a PageReference, as I am doing from my action method.

| improve this answer | |
  • Thanks for the response, I just updated the question with some relevant extension code. I'm now thinking that the problem is somehow related to ContentVersion/ContentDocument since yours work. – Jose Jul 23 '14 at 20:02
1

If you are using a global action on the C_Object__c the id will not be passed on to the visualforce page.

If you need this publisher action to be run from your C_Object__c detail page, I suggest to use an Object specific action instead of global action. With these type of actions the id is passed on to the visualforce standard controller.

Object specific actions will only be available for your C_Object__c.

I hope this can help you further.

| improve this answer | |
  • I'm using an Object specific action. – Jose Jul 16 '14 at 12:05

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.