Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

I am trying to develop a visualforce custom component which takes an attribute from a visual force page. I need to access that attribute in the controller's Constructor so that I can bring some records from the database and I need to display those records in the Component. But the problem is that I am not getting Attribute value in Controller.

See the below code to understand the problem clearly.

Controller :

public with sharing class AdditionalQuestionController {

    public String CRFType {get;set;}

    public AdditionalQuestionController () {

        system.debug('CRFType : '+CRFType);

        List<AdditoinalQuestion__c> lstAddQues = [Select AddQues__c from AdditoinalQuestion__c wehre CRFType = :CRFType];

        system.debug('lstAddQue : '+lstAddQue);

    }

}

Component :

<apex:component controller="AdditionalQuestionController" allowDML="true">

    <apex:attribute name="CRFType" description="This is CRF Type."  type="String" required="true" assignTo="{!CRFType}" />

        <apex:repeat value="{!lstAddQue}" var="que">

            {!que}<br />

        </apex:repeat>

</apex:component>

VisualForce page :

 <apex:page >

    <c:AdditionalQuestionComponent CRFType="STE" />

</apex:page>

1 Answer

0 votes
by (32.1k points)

Here is an alternative solution for your controller which uses a "getter" method to populate your list:

public with sharing class AdditionalQuestionController {

    public String CRFType {set;}

    public AdditionalQuestionController () {

        system.debug('CRFType : '+CRFType); // this will be null in the constructor

    }

    public List<AdditoinalQuestion__c> getLstAddQue() {

        system.debug('CRFType : '+CRFType); // this will now be set

        List<AdditoinalQuestion__c> lstAddQues = [Select AddQues__c from AdditoinalQuestion__c wehre CRFType = :CRFType];

        system.debug('lstAddQue : '+lstAddQue);

        return lstAddQue;

    }

}

Browse Categories

...