0

I need to find a way where it displays just one value in the below email template

Apex Controller:

 public  class ContactController
 {
public id contactId {get;set;}  
public List<Lead> getleads2()
{
      public List<Lead> getleads()
{
     List<Lead> leadList =  new List<Lead>();

        leadList = [
            SELECT Loan_Number__c
                 ,Loan_Number__c
                 , Banker__c
                 , banker__r.Name
                 , App_Date__c
                 , QL_banker__r.email                 
              FROM Lead 
             WHERE Banker__c =:contactId Limit 10];

        return leadList ;

}
    List<Lead> leadList2 =  new List<Lead>();

        leadList2 = [SELECT banker__r.FirstName FROM Lead WHERE Banker__c =:contactId Limit 1];             
        return leadList2 ;
}     

}
Visualforce Componet:

<apex:component controller="ContactController" access="global">
<apex:attribute name="ContId" type="id" description="Id of the contact" 
 assignTo="{!contactId }"/>

  <html> <apex:repeat value="{!leads}" var="l">
        <H2>HELLO {!l.bank__r.Name}</H2>
        </apex:repeat>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  </html>
</apex:component> 

My Output in Visualforce Email Template is

HELLO Aaron Shook

HELLO Aaron Shook

HELLO Aaron Shook

I just need it to display once. I know i am running into this because I am using the List in class and repeat in VF, but I want to know if there is any other way to achieve this. Need the solution ASAP.

Thanks in advance.

Test Class:

@isTest()
 private class test_ContactController {

static testMethod void mainTest() {

Contact c = new contact();
c.firstname = 'test';
c.LastName = 'last';
c.email = 'test@gmail.com';
c.recordtypeid = '0120d0000001Gvo';
insert c;

Lead l = new Lead();
l.lastName = 'asd';    
l.banker__c  = c.id; 
l.recordtypeid = '0120d0000001GzR';      
insert l;

ContactController classObject = new ContactController();
List<Lead> leadlist = new  List<Lead>();
List<Lead> leadList3 = new List<Lead>(); 
classObject.contactId = c.Id;

leadlist=  classObject.getleads();    

ContactController.recordWrapper wrapper = new ContactController.recordWrapper(c.Id); //Just passed contact parameter in this contructor 
wrapper.ContactName = 'test'; 

System.assertNotEquals(0, leadlist.size());
//System.assertNotEquals(1, recordWrapper.size());    
  } 
  }      
2
  • My answer still works (I believe), although you may want to look into a wrapper class to display the data instead of just the Lead List. That way you can populate single properties from the list and display them on the page without performing the query multiple times... – Eric Dec 6 '17 at 1:43
  • Hey Eric NVM got the Test class to work: ContactController.recordWrapper wrapper = new ContactController.recordWrapper(); to ContactController.recordWrapper wrapper = new ContactController.recordWrapper(c.Id); – Lin Dec 6 '17 at 22:37
3

Ok, an example using a wrapper class

Apex Code

public  class ContactController
 {
     public recordWrapper recordWrapper{
        get{
            if(recordWrapper == null){
                recordWrapper = New recordWrapper(contactId); //Not sure where you are getting contactId from but you will need it here
            }
            return recordWrapper;
        }
        set;
     }


     public class recordWrapper{

        public String ContactName {get;set;}
        public Lead[] leadsList {get;set;}

        public recordWrapper(Id contactId){
            leadsList = [SELECT bank__r.FirstName FROM Lead WHERE 
                     Bank__c =:contactId Limit 10];
            if(!leadsList.isEmpty()) { //Ensure it is not empty
                ContactName = leadsList[0].bank__r.FirstName;             
            }

        }


     }

 }

Component

<apex:component controller="ContactController" access="global">
<apex:attribute name="ContId" type="id" description="Id of the contact" 
 assignTo="{!contactId }"/>

  <H2>HELLO {!recordWrapper.ContactName}</H2>
  <apex:repeat value="{!recordWrapper.leadsList}" var="l">
    // ...Other Stuff...     
  </apex:repeat>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
</apex:component> 

Original response below

Not sure why you are using a list but if thats what you have to do, simply add a property for the Name and use it in the VF page.

Note: There is no way for your page to show the name 3 times on its own, unless the component is placed into a repeat, given the code you provided as you have limited the query to return a single results via the Limit 1 clause - just pointing that out

Since we do not have the full picture of your use case and code the below can serve as an example to get you started. Although you may have a larger issue if the component is indeed inside a repeat as it will still display 3 times unless you modify the parent VF page/code to only show it once

public  class ContactController
 {
     public id contactId {get;set;}  
     public String ContactName { //Added property to store name
        Lead[] tmp = getLeads2();
        if(!tmp.isEmpty()) { //Ensure it is not empty
            return tmp[0].FirstName;             
        }
        return null;
     }
     public List<Lead> getleads2(){        
        List<Lead> leadList2 =  new List<Lead>();            
        leadList2 = [SELECT bank__r.FirstName FROM Lead WHERE 
                     Bank__c =:contactId Limit 1];
        return leadList2 ;
     }     
 }

Then just display it on your page

<apex:component controller="ContactController" access="global">
<apex:attribute name="ContId" type="id" description="Id of the contact" 
 assignTo="{!contactId }"/>

  <H2>HELLO {!ContactName}</H2>
  <apex:repeat value="{!leads2}" var="l">
    // ...Other Stuff...     
  </apex:repeat>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
  </html>
</apex:component> 

4
  • 1
    Eric - merge field should be {!leads2} per your code example (typo). Also - there's an idempotency issue with ContactName - if retrieved first by VF, it will be null – cropredy Dec 6 '17 at 1:32
  • @cropredy - I just copied and pasted that part from the OP question. Nice catch... – Eric Dec 6 '17 at 1:34
  • 1
    see my edit re: idempotency – cropredy Dec 6 '17 at 1:34
  • @cropredy - wow, I am off today. completely missed that. Now the solution is completely fubar'ed.....Me thinks the overall design needs to be reworked to make it "more correct". Idempotency has bit me more than once.... – Eric Dec 6 '17 at 1:38

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.