function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JoyDJoyD 

Pass value from Component to Controller

I want to pass the ID of the Contact from my VisualForce Email Template up to the Controller, via the Custom Component, so that the Controller can query Events for just this Contact.  However, I seem to be getting stuck at Component to Controller.

 

Per the VF Developer's Guide, under "Custom Component Controllers", step 3 says, "In the <apex:attribute> tag in your component definition, use the assignTo attribute to bind the attribute to the class variable you just defined."  However, I am finding this never sets the value in the Controller - just within the Component.  Is the Guide wrong or do I have an error?

 

Here is a snippet of the Component code:

 

<apex:component controller="EmailApptController" access="global">

    <apex:attribute name="ToID" type="ID" description="the account ID" assignTo="{!contactID}"/>
    <apex:attribute name="Something" type="String" description="something to assign" assignTo="{!something}"/>

 

 

And the relevant Controller:

 

public class EmailApptController {
	public List<Event> appts {get; set;}
	public String message {get; set;}
	public ID contactID {get; set;}
	public String something {get; set;}
	
	public EmailApptController() {
		datetime yaynow = datetime.now();
		appts = [SELECT WhoId, WhatId, OwnerId, Subject, StartDateTime, Result__c, Confirmation_Status__c, Location_Map_URL__c, Location__c, Location_Address__c, Location_City_State_Zip__c, Location_Directions__c, Location_Directions_2__c
					FROM Event
					WHERE WhoId = :contactID
					AND StartDateTime >= :yaynow
					AND Result__c != 'Cancelled'
					AND Result__c != 'Rescheduled'];
					
		if (appts.isEmpty()){
        	message = 'There are no pending appointments for this client: ' + contactID + ' and something is: '+ something;
        }
	}
	
}

 

 

contactID and 'something' always return null from the controller, but when I access them in the Component they are fine.

 

I have found this post in the wiki re Controller Component Communication, but it's way over my head and seems overkill if all I want to do is grab this one ID.  Any thoughts?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
JoyDJoyD

Finally figured it out.  Solution for anyone who searches this out later:

 

EmailApptController() was being called BEFORE the values for ContactID were set.  (see, 1st line of Component sets controller="EmailApptController" -> THEN lines 2 and 3 were called, to set the values)

 

I adjusted the code so that the EmailApptController() is called AFTER the ContactID was set (it's called explicitly from inside SetContactID, instead of being implicitly called from the first line of the Component).

 

Component:

 

<apex:component controller="EmailApptController" access="global">

    <apex:attribute name="SomethingToType" type="String" description="something to assign" assignTo="{!something}"/>
    <apex:attribute name="ToID" type="ID" description="the account ID" assignTo="{!contactID}" />

 and the Controller:

 

public class EmailApptController {
	public List<Event> appts {get; set;}
	public String message {get; set;}
	public String something {get; set;}
	
	public ID contactID;
	public ID getContactID(){ return contactID; }
	public void setContactID(ID s){
		contactID = s;
		EmailApptController();
	}
	
	public void EmailApptController() {
		datetime yaynow = datetime.now();
		appts = [SELECT WhoId, WhatId, OwnerId, Subject, StartDateTime, Result__c, Confirmation_Status__c, 
					Location_Map_URL__c, Location__c, Location_Address__c, Location_City_State_Zip__c, 
					Location_Directions__c, Location_Directions_2__c
					FROM Event
					WHERE WhoId = :contactID
					AND StartDateTime >= :yaynow
					AND Result__c != 'Cancelled'
					AND Result__c != 'Rescheduled'];
		System.debug('just ran query');
		if (appts.isEmpty()){
        	message = 'There are no pending appointments for this client: ';
        }
	}
	
}

 

 

Note: it seems to take the ID type just fine.  Did not have to change that to a String.

All Answers

DharmeshDharmesh

change the type from id to String.

The assingTo  cant take Id type value.

 

-Dharmesh(http://sfdc-development.blogspot.com/)

 

JoyDJoyD

My String 'something' (which I added to test that exact issue before I posted) doesn't work either - it does not pass through to the Controller at all...

JoyDJoyD

Finally figured it out.  Solution for anyone who searches this out later:

 

EmailApptController() was being called BEFORE the values for ContactID were set.  (see, 1st line of Component sets controller="EmailApptController" -> THEN lines 2 and 3 were called, to set the values)

 

I adjusted the code so that the EmailApptController() is called AFTER the ContactID was set (it's called explicitly from inside SetContactID, instead of being implicitly called from the first line of the Component).

 

Component:

 

<apex:component controller="EmailApptController" access="global">

    <apex:attribute name="SomethingToType" type="String" description="something to assign" assignTo="{!something}"/>
    <apex:attribute name="ToID" type="ID" description="the account ID" assignTo="{!contactID}" />

 and the Controller:

 

public class EmailApptController {
	public List<Event> appts {get; set;}
	public String message {get; set;}
	public String something {get; set;}
	
	public ID contactID;
	public ID getContactID(){ return contactID; }
	public void setContactID(ID s){
		contactID = s;
		EmailApptController();
	}
	
	public void EmailApptController() {
		datetime yaynow = datetime.now();
		appts = [SELECT WhoId, WhatId, OwnerId, Subject, StartDateTime, Result__c, Confirmation_Status__c, 
					Location_Map_URL__c, Location__c, Location_Address__c, Location_City_State_Zip__c, 
					Location_Directions__c, Location_Directions_2__c
					FROM Event
					WHERE WhoId = :contactID
					AND StartDateTime >= :yaynow
					AND Result__c != 'Cancelled'
					AND Result__c != 'Rescheduled'];
		System.debug('just ran query');
		if (appts.isEmpty()){
        	message = 'There are no pending appointments for this client: ';
        }
	}
	
}

 

 

Note: it seems to take the ID type just fine.  Did not have to change that to a String.

This was selected as the best answer
NikuNiku

I have same problem... Just solved with your solution Thanks.

rekha sharmarekha sharma

We can use assignto with  <apex: attribute to pass from component to controller instead.

potoshopotosho

Thanks a lot dude, that works just great!

juppyjuppy
The suggested solution didn't work for me, but I was able to use the visualforce page to pass parameters instead.

In the page controller:
system.currentPageReference().getParameters().put('aid', 'someID);

then in the component controller:
system.currentPageReference().getParameters().get('aid');