+ Start a Discussion
Pat McQueenPat McQueen 

Using a component w/ a custom controller in a VF page

Hello,

I am using a component in a VF page and trying to pass information to the custom controller of the component and not having any luck.  I think I am missing something basic ... But can't seem to figure it out.  There are three parts - The VF page, Component and Controller.  When I run the VF page the Apex Debug log confirms that the TargetUserID: is not passed to the custom controller.  I thought that the apex:attribute tag with the assign to would call the setter for Target User ID.  Any assistance would be appreciated.

The code does run.  It just evaluates the TargetUserID as null and returns all the "null" booth shifts - I would like the target users' shifts.

pat

VF Page Code:
<apex:page >
<h1>Start</h1>
<c:BoothShiftList TargetUserID="00580000001UipR" />
</apex:page>

Custom Component <BoothShiftList> Code:
<apex:component controller="ScheduleEmailController">
<apex:attribute name="TargetUserID" description="User id of the person getting this page" type="string" required="true" assignTo="{!TargetUserID}"/>
<apex:pageBlock title="My Dreamforce Shifts">
<apex:pageBlockTable id="shifttable" value="{!bs}" var="currentBooth">
<apex:column headerValue="Shift Time">
<a href="/{!currentBooth.Id}">{!currentBooth.Shift__r.name}</a>
</apex:column>
<apex:column headerValue="Booth Name">
<a href="/{!currentBooth.Booth__r.Id}">{!currentBooth.Booth__r.Booth__c}</a>
</apex:column>
<apex:column headerValue="Add to Calendar">
<a href="/{!currentBooth.Id}">{!currentBooth.Assigned__c} - {!currentBooth.Is_this_Staffed__c}</a>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:component>

 
Custom Controller Code:
Public class ScheduleEmailController {

    public string TargetUserID { get; set; }
    public Booth_Shift__c[] bs { get; set; }

    public ScheduleEmailController()
    {    
        bs = [Select assigned__r.Id, b.Shift__r.Date__c, b.Shift__r.name, b.Shift__r.Shift_Time__c, b.Shift__c, 
            b.Booth__r.Booth_Type__c, b.Booth__r.Booth__c, b.Booth__c, b.Is_this_Staffed__c from Booth_Shift__c b
            where assigned__r.Id = :TargetUserID order by b.Shift__r.start__c LIMIT 10];
            
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('TargetUserID: ' + TargetUserID);
    }
}

 

 

dchasmandchasman
The problem is that you have essentially said assignTo from the attribute back to itself - something that we do plan to catch in the VF compiler at some point. Switch from this:

Code:
<apex:attribute name="TargetUserID" description="User id of the person getting this page" type="string" required="true" assignTo="{!TargetUserID}"/>

to something like this and you should be good to go:

Code:
<apex:attribute name="TargetUserID" description="User id of the person getting this page" type="string" required="true" assignTo="{!targetUser}"/>

Public class ScheduleEmailController {
public String targetUser { get; set; }
public Contact[] bs { get; set; }

public ScheduleEmailController() {
bs = [Select id, name from Contact LIMIT 10];

System.debug('Current User: ' + UserInfo.getUserName());
System.debug('TargetUserID: ' + targetUser);
}
}





Message Edited by dchasman on 10-21-2008 08:03 AM
Pat McQueenPat McQueen

Thanks Doug!  I actually had two problems.  The second problem was a timing problem.  The solution was here: http://forums.sforce.com/sforce/board/message?board.id=Visualforce&message.id=5832

 Basically, the assignment to targetUser happens AFTER the controller is initialized. This was an easy fix in the controller - just use a get method instead of defining our booth shift array in the controller.  

 

Controller New Code:  Note that the Debug does not give me the right answer because it happens before init.
public class ScheduleEmailController {

    public ID targetUser { get; set; }
    public Booth_Shift__c[] bs { set; }
    
    public Booth_Shift__c[] getbs() {
     return [Select assigned__r.Id, b.Shift__r.Date__c, b.Shift__r.name, b.Shift__r.Shift_Time__c, b.Shift__c, 
            b.Booth__r.Booth_Type__c, b.Booth__r.Booth__c, b.Booth__c, b.Is_this_Staffed__c from Booth_Shift__c b
            where b.assigned__c = :targetUser order by b.Shift__r.start__c];
    }

    public ScheduleEmailController()
    {                
            System.debug('Current User: ' + UserInfo.getUserName());
            System.debug('TargetUser: ' + targetUser);
    }

}


 
Everything Works now!!