How to create dynamic filter fields and values in Salesforce reports?

Sample Report:


Sample Code:

Visualforce page:

<apex:page controller="DynamicReportController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection columns="3">
                <apex:pageBlockSectionItem >Member Name</apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:selectList size="1" value="{!memberNameOption}">
                        <apex:selectOption itemLabel="Starts With" itemValue="sw"/>
                        <apex:selectOption itemLabel="Equals" itemValue="eq"/>
                        <apex:selectOption itemLabel="Not Equal To" itemValue="ne"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ><apex:inputText value="{!memberNameValue}"/></apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >Member Age</apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem >
                    <apex:selectList size="1" value="{!memberAgeOption}">                        
                        <apex:selectOption itemLabel="Equals" itemValue="eq"/>
                        <apex:selectOption itemLabel="Not Equal To" itemValue="ne"/>
                        <apex:selectOption itemLabel="Less Than" itemValue="lt"/>
                        <apex:selectOption itemLabel="Greater Than" itemValue="gt"/>
                        <apex:selectOption itemLabel="Less Than Or Equal To" itemValue="le"/>
                        <apex:selectOption itemLabel="Greater Than Or Equal To" itemValue="ge"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem ><apex:inputText value="{!memberAgeValue}" /></apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Find" action="{!showReport}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:

public class DynamicReportController {
    public String memberNameOption {get;set;}
    public String memberNameValue {get;set;}
    public String memberAgeOption {get;set;}
    public String memberAgeValue {get;set;}
    
    public pageReference showReport() {
        PageReference pg = new pageReference('/00OU00000028ed5?pc0=00NU0000004KWBj&pn0='+ memberNameOption + '&pv0=' + memberNameValue + '&pc1=00NU0000000aMeA&pn1=' + memberAgeOption + '&pv1=' + memberAgeValue);
        pg.setRedirect(true);
        return pg;
    }
}

here pc0 and pc1 are field ids. Check the below steps to get the field ids.


1. Select the object.

2. Select the field.

3. Get the id from the URL.


Output:



Cheers!!!

No comments:

Post a Comment