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
Blake TanonBlake Tanon 

Class: Set variables

Hello,

 

I'm a novice programmer at best - trying to teach myself.  I know how to set/declare variables in triggers, but I'm not sure how to go about it in a class.  I'm working on building VF emails and I would like the class to pull a field value from the record that is is generated from.

 

We have a object for Territories (Territory__c), we would like to send out activity reports to our group each night with all of the Tasks and Events completed for that territory.  The salesforce scheduled reports aren't scalable since we need one per territory and can only send one per hour.  I would like to pull the Territory Name field in as a veriable, then query tasks/events that match that - Activities have a field called Territory__c that will match the name.  Here is my class:

 

public class TerritoryActivity{

    private final List<Task> t;
    private final List<Event> e;


    
    public TerritoryActivity(){
        
        
        t = [SELECT id, Territory__c, Assigned_Name__c, Firm_Name__c, Contact_Name__c, State__c,Description, Subject, Outcome__c, Type
             FROM Task
             WHERE Date_Completed__c = Yesterday AND Outcome__c = 'Success' AND Territory__c = 'New England'];
             
        e = [SELECT id, Territory__c, Assigned_Name__c, Firm_Name__c, Contact_Name__c, State__c,Description, Subject, Outcome__c, Type
             FROM Event
             WHERE Date_Completed__c = Yesterday AND Outcome__c = 'Meeting Occurred' AND Territory__c = 'New England'];

    }     
    
    
    public List<Task> getCalls(){
        return t;
    }
    
    public List<Event> getMeetings(){
        return e;
    }        
}

 

crop1645crop1645

Blake:

 

You'll get QueryExceptions on your Select SOQL if the where clause is not satisfied

 

use this -- the caller will deal with empty lists naturally and not have to test for null

 

public class TerritoryActivity{

    private  List<Task> tList = new List<Task>();  // init to empty list
    private  List<Event> eList = new List<Event> ();


    
    public TerritoryActivity(String terr){
        
        for (Task t :
        [SELECT id, Territory__c, Assigned_Name__c, Firm_Name__c, Contact_Name__c, State__c,Description, Subject, Outcome__c, Type
             FROM Task
             WHERE Date_Completed__c = Yesterday AND Outcome__c = 'Success' AND Territory__c = :terr])
tlist.add(t); for (Event e: [SELECT id, Territory__c, Assigned_Name__c, Firm_Name__c, Contact_Name__c, State__c,Description, Subject, Outcome__c, Type FROM Event WHERE Date_Completed__c = Yesterday AND Outcome__c = 'Meeting Occurred' AND Territory__c = :terr)
eList.add(e); } public List<Task> getCalls(){ return tList; } public List<Event> getMeetings(){ return eList; } }
Blake TanonBlake Tanon

Hi Eric,

Thanks for your responce, I put this code inplace in the class, but in the VF template I'm getting an error: "Unknown constructor 'TerritoryActivity.TerritoryActivity()'."  Do I need to change part of the component since you added TerrtitoryActivity(String terr)?

 

<apex:component controller="TerritoryActivity" access="global">
<html>
        <body>

        
        
         <STYLE type="text/css">
               TH {font-size: 11px; font-face: arial;background: #CCCCCC; border-width: 1;  text-align: center } 
               TD  {font-size: 11px; font-face: verdana } 
               TABLE {border: solid #CCCCCC; border-width: 1}
               TR {border: solid #CCCCCC; border-width: 1}
         </STYLE>
                  <font face="arial" size="2">
                 Yesterday's Successful Calls<br/>
        <table border="0" >
                 
                 
                 
                 <tr > 
                     <th>Assigned</th><th>Firm</th><th>Rep</th><th>State</th><th>Type</th><th>Subject</th><th>Comments</th>
                 </tr>
    <apex:repeat var="te" value="{!Calls}" >
       <tr>
           
           <td align="Left">{!te.Assigned_name__c}</td>
           <td align="Left">{!te.Firm_Name__c}</td>
           <td align="Left">{!te.Contact_Name__c}</td>
           <td align="Left">{!te.State__c}</td>
           <td align="Left">{!te.Type}</td>
           <td align="Left">{!te.Subject}</td>
           <td align="Left">{!te.Description}</td>
       </tr>
    </apex:repeat>                 
       </table>
       </font>
       
       </body>
    </html>
</apex:component>

 

 

crop1645crop1645

Blake -- sorry - I didn't realize this was going to be referenced by a VF component

 

0 - Remove territory from the constructor

1 - I would move the select statements out of the constructor and have them invoked within the getCalls(), getMeetings() methods

2 - That said, I don't see how your component knows what is the specific territory to use for the query.  Is this user input? Normally, this would be done as a component parameter:

 

<apex:attribute name="territory" 	description="territory used to query for events and tasks" type="string" 
												assignTo="{!territory}" default=""/>

 

Read up on this in the VF doc on components using custom controllers. You'll need to create a getter/setter territory in your controller