I am new to apex and salesforce, and I have a trouble.

I have a component that I should use in the VF email template to show some text (parsed JSON data from google API).

Here is the temptate's code

<messaging:emailTemplate subject="Your Search Results." recipientType="Contact" relatedToType="SearchRequest__c" >
    <messaging:plainTextEmailBody >
        <c:SearchResult userID="{!recipient.Id}"/>

So, i am getting the contact's Id and send it to the component:

<apex:component controller="SearchResultComponentController" access="global" > 
    <apex:attribute name="userID" type="String" description="the contact ID" assignTo="{!contactID}"/>
    <script type="text/javascript">  
       sforce.connection.sessionId = "{!$Api.Session_ID}";
             function(result, event){
    <apex:repeat var="sr" value="{!SearchResults}" Id="Form">
    <apex:outputText > </apex:outputText> 
    <apex:repeat var="item" value="{!sr.items}">     
        <apex:outputText value="{!item.title}">{!item.title}</apex:outputText>

And after that i am using this ID inside the controller (sorry for some junk):

global with sharing class SearchResultComponentController {

        public static  String contactID {get; set;}
        public  static JSON2Apex[] SearchResults {get; set;}
        public  static String[] stringList {get; set;}

        global static void Implementation(){
        SearchResults = new JSON2Apex[]{};
        System.debug('///// contactId /// ' + contactID);
        //SearchResults = new  JSON2Apex();
        Map<Id, SearchRequest__c> reqMap = new Map<Id, SearchRequest__c>([select Id, Name from SearchRequest__c where Contact__c = :contactID]);
        Responce__c[] respList = [select Id, Name, ResponceData__c, JSONString__c from Responce__c  where SearchRequest__c IN :reqMap.KeySet()];
        //System.debug('/////  ' + respList);
        for(Responce__c res:respList){
            JSON2Apex resItem = new JSON2Apex(System.JSON.createParser(res.JSONString__c));
            System.debug('/////  ' + resItem);
            resItem = JSON2Apex.parse(res.JSONString__c);
            for(JSON2Apex.items item:resItem.items){
                system.debug( '//// items size  ' + resItem.items.size());
                system.debug( '//// kind ' + item.kind);
                system.debug( '//// search ' + item.title);

            //system.debug( '//// ' + resItem.items);

    public SearchResultComponentController(){

Suchwise I want the parsed data from controller been shown in the component onload the page(I am using a simple page with just a component for debug all this). But systm debug says than ID = null inside the remote method. I dont understand why.

PS This structure works perfectly with onclick action using action function, but I need to load the data right after the page opens, without any additional user actions.


Assuming it is this line:

System.debug('///// contactId /// ' + contactID);

where you detect the null ID, then that is "by design".

Using an action function, the view state carried in the page is used to repopulate the controller's field values such as contactID. But that is quite a lot of overhead if it is not really required, so the @RemoteAction option is also provided. In that case only the parameters that you pass to the method will have values.

If you want to continue to use the @RemoteAction, pass the contactID as a parameter to it:

SearchResultComponentController.Implementation('{!userID}', function...

global static void Implementation(Id contactId) ...
| improve this answer | |
  • AH! Yes, this is it! :) Thanks a lot! This is the way to pass the parameter to controller "onload", I can just assign this param to global varible if needed. But now I have "attempt to de-reference a null object". Think i can handle it myself though. – Igor H May 12 '14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.