1

I got null value for agreementId when I try to type it

Apex Class

public class selectedTasks {
    private final List<agreement__c> agreementTasks;
    public String agreementId {set;get;}

    public agreement__c selectedAgreement {set;get;}
    private String finalQuery = '';

    public selectedTasks() {
        try{
            agreementTasks = [SELECT name FROM agreement__c WHERE Id =:agreementId ];

        } catch(Exception e){
            errorMessage = 'Id : '+agreementId;
        }
    }
}

VF Component

<apex:component controller="selectedTasks" access="global">
    <apex:attribute assignTo="{!agreementId}" 
                    name="agrId" type="String" 
                    description="Agreement ID"
                    access="global"
                    required="true"/>
    {!errorMessage}
</apex:component>

VF Page

<apex:page >
  <c:Partner_Tasks_Component agrId="a0Z25000001cSXG" />
</apex:page>
4
  • How is "selectedTasks" being called? I think you've oversimplified your code.
    – sfdcfox
    Commented Feb 7, 2016 at 17:46
  • @sfdcfox How can I call class ?? I thought it get and set values after load the component inside VF page Commented Feb 7, 2016 at 17:51
  • Your code is being called; by the time the page loads, the agreementId should be populated. Since you're not calling your function, none of the other stuff is populated, though. Let me write up an answer for you and see if it helps.
    – sfdcfox
    Commented Feb 7, 2016 at 18:05
  • shouldn't selectedTasks() be getSelectedTasks() to be usable in VF component?
    – cropredy
    Commented Feb 8, 2016 at 0:37

2 Answers 2

2

I have faced similar issues earlier when using components. Seems, we are not able to use {get;set;} when working with components. Please try changing the below line from your code

public String agreementId {set;get;}

to

public String agreementId;

public void setAgreementId(String argId){
agreementId = argId;    
} 

public String getAgreementId(){
return agreementId; 
}

I have done similar changes in my controller and it has worked fine for me. Please see if this works for you too.

0
1

Since you're trying to load an agreement record, I would change your class as follows:

public class selectedTasks {
    // Must be public or it won't be useful outside the class
    public Agreement__c agreement { get; set; }
    // Remember the Id in case we need it later.
    Id agreementId;
    public void setAgreementId(Id agreementId) {
        if(this.agreementId == null) {
            this.agreementId = agreementId;
            try {
                agreement = [SELECT Name FROM Agreement__c WHERE Id = :agreementId];
            } catch(QueryException e) {
                // The agreement could not be queried if we got here
            }
        }
    }
}
3
  • I got the same error Commented Feb 7, 2016 at 18:23
  • What error are you getting, precisely?
    – sfdcfox
    Commented Feb 7, 2016 at 18:28
  • I type the agreementId in VF page and its value is null. I added get method with default value (valid record Id) and it worked. so the problem is, agreement Id didn't passed from component attribute to the controller. It may cause of attribute type for example? or access? I gave it global access and the variable in the controller is public.. Commented Feb 8, 2016 at 6:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .