+ Start a Discussion

How to pass parameters to Visualforce page from Apex method?

I'm trying to pass parameters from the Apex method to the VF page where I have a VF component. So that I can get the VF component content into the 'pageAsString' variable in the apex method and display that value in an LWC component.

I'm getting the VF component content into the 'pageAsString' variable but the dynamic values are not getting populated.

I tried manually passing record Ids to the VF component inside the VF page and it is working.

I'm unable to pass record Ids dynamically from the Apex method to the VF page.

Below is the code I have tried so far. (There is a lot of code but I just cut down to what is required to understand the question)

If you are wondering why I'm using these many components: I'm trying to preview a visualforce email template body in an LWC component. The visualforce template has a VF component.

//Apex Class
public class SampleController {
    public String aId { get;set; }
    public String bId { get;set; }
    public static void getACDetails(Id accId, Id ConId) {
        PageReference pageReference = new PageReference('/apex/TestVFPage');
        pageReference.getParameters().put('aId', accId);
        pageReference.getParameters().put('bId', ConId);
        String pageAsString = pageReference.getContent().toString();
//VF Page
<apex:page controller="SampleController" showHeader="false" sidebar="false" docType="html-5.0"> <!-- VF Component --> 
<c:ABCTemplateComponent accountId="{!aId}" contactId="{!bId}"/> 

//VF Component
<apex:component controller="ComponentController" access="global"> 
<apex:attribute name="accountId" assignTo="{!aRecordId}" type="Id" description="some description" /> 
<apex:attribute name="contactId" assignTo="{!cRecordId}" type="Id" description="some description"/> 

Could anyone help me understand how can I pass values from the Apex method to the VF page?
Suraj Tripathi 47Suraj Tripathi 47
Please refer to this link -

I hope you find the above solution helpful. If it does, please mark it as the Best Answer to help others too.

Thanks and Regards,
Suraj Tripathi