Search code examples
salesforceapexvisualforce

How to correctly pass the body of my apex class to the visualforce page


I have an apex class which has a getApexClassInfo() method:

public with sharing class ApexParser {
    
    @AuraEnabled(cacheable=true)
    public static ApexClass getApexClassInfo(String ApexClassName) {
        ApexClass ApexClassBody = [
            SELECT Name, Body
            FROM ApexClass         
            WHERE Name =: ApexClassName
        ];
        return ApexClassBody;
    }
}

The method returns the body of the class that was selected in the combobox

Tell me how to correctly pass the ApexClassBody value to the visualforce page

<apex:page controller="ApexParser" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
    <apex:pageBlock>
        <h1>Test to see if this will generate</h1>
            <apex:outputText value="{   ??????   }"/>
        <h2><p>END</p></h2>
    </apex:pageBlock>
</apex:page>

Solution

  • You should be able to do something like this. Basically, you can use a public getter to store your 'Body' in and then bind that to a variable in your Visualforce code. Be sure to call your getApexClassInfo() method somewhere too.

    public static String body { get; set; }
    
    @AuraEnabled(cacheable=true)
    public static ApexClass getApexClassInfo(String ApexClassName) {
        ApexClass ApexClassBody = [
            SELECT Name, Body
            FROM ApexClass         
            WHERE Name =: ApexClassName
        ];
        body = ApexClassBody.Body;
        return ApexClassBody;
    }
    
    
    <apex:page controller="ApexParser" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
        <apex:pageBlock>
            <h1>Test to see if this will generate</h1>
                <apex:outputText value="{!body}"/>
            <h2><p>END</p></h2>
        </apex:pageBlock>
    </apex:page>