Search code examples
salesforceapexvisualforce

How to put value in Controller from another Class?


I would like to call PageReference from VF Page in another class (Class A) so that it can generate a PDF and set as attachment. The VF Page is getting the ID from its Controller. I want to put the ID from Class A to VF Page so that it will the one to be used rather than the controller.

VF Page Name: ContactDocument

public class DocuGenerate {
    public Contact ccc {get;set;} 

    public CaseClosureDocumentController(ApexPages.StandardController controller)
    {
        ccc = (Contact) controller.getRecord();
        ccc = [SELECT ID, NAME FROM CONTACT WHERE ID =: ccc.id];

        //GENERATE A PDF WITH THE ID RETRIEVED.
    }
}


public class SendEmail {
    public static void SendMessage() {
        List<Contact> con = [SELECT ID FROM CONTACT LIMIT 1];
        for(Contact c : con){
            Pagereference vfpage1 = Page.ContactDocument;
            //HOW WILL I PASS CON.ID TO VF PAGE SO THAT IT WILL BE THE ONE TO PROCESS, NOT THE ONE IN VFPAGE?
        }
    }
}

EXPECTED: GENERATE A PDF FILE WHEREIN THE INFO IS ABOUT CONTACT ID I HAVE IN ANOTHER CLASS, INSTEAD THE ONE BEING GENERATED IN VF PAGE.


Solution

  • From what I can make out you want to be sending the Id of the contact from the controller of the page your on now to the PDF Rendering page... You can do this through the page reference and URL Parameters. Adding a parameter to the page reference means that any controller for that page (standard or extension) can get this id and use the record. Here's how you add a parameter to a page reference...

    public class SendEmail {
       public static void SendMessage() {
         List<Contact> con = [SELECT ID FROM CONTACT LIMIT 1];
         for(Contact c : con){
           Pagereference vfpage1 = Page.ContactDocument;
           vfpage1.getParameters().put('id', c.id);
           return vfpage1; //You probably need to return the page reference 
                           //in order to redirect to it
         }
       }
    }