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
SFDC DummySFDC Dummy 

LastDate record Searching

HI

Hi...
I have created a page which will search record date wise...
like i have record

date                            value
10/9/2015--------------------a
11/9/2015--------------------aa
12/9/2015--------------------aaa
14/9/2015--------------------aaaa
15/9/2015--------------------aaaaa
20/9/2015--------------------aaaaaa

if i am searching record on 10/9/2015 the record will be display  'a'
if i am searching record on 11/9/2015 the record will be display  'aa'
if i am searching record on 13/9/2015 the record will be display  'aa'(12/9/2015 record)
if i am searching record on 14/9/2015 the record will be display  'aaaa'
if i am searching record on 20/9/2015 the record will be display  'aaaaaa'
if i am searching record on 19/9/2015 the record will be display  'aaaaa'(15/9/2015 record)

if there is record on particular date it will display if there is no record on that date it will display last date record

for example i have 10/1/2001 record if i am searching 10/1/2001 it will be displayed 10/1/2001.like this
User-added image

As per requirment there is no record in 11/1/ 2001 in m object if i am searching 11/1 2001 record it will be display last day record means 10/1/2001...but i am getting 
User-added image
public with sharing class dateInPBTbaleController {
    public BankBook_Entry__c account{get;set;}
    public List<BankBook_Entry__c> listAccount {get;set;}
   
    //public Date startDate1;
    //public Date endDat1;
    public Double totalRevenue {get;set;}
    public Double totalRevenue1 {get;set;}
    public Double totalRevenuee {get;set;}
    public Double totalRevenuee1 {get;set;}
    public Date dat {get;set;}
    public dateInPBTbaleController() {
        listAccount  = [SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                        FROM BankBook_Entry__c
                        order by Master_Code1__c ASC ];
    }

    public dateInPBTbaleController(ApexPages.StandardController controller){
    
        account = new BankBook_Entry__c();
        totalRevenue = 0;
        totalRevenue1 = 0;
        totalRevenuee = 0;
        totalRevenuee1 = 0;
    }
       
   public void displaingTable(){
        if(account.Date__c != null){
            listAccount  = [SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                                FROM BankBook_Entry__c
                                WHERE Date__c =: account.Date__c order by Master_Code1__c ASC ];
            
            if(listAccount.size() == 0){
                listAccount  = [SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                                FROM BankBook_Entry__c
                                WHERE Date__c <: account.Date__c order by Master_Code1__c ASC LIMIT 1 ];
            }
            
            if(account.Date__c == null){
                listAccount  = [SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c,LastModified__c
                                FROM BankBook_Entry__c order by Master_Code1__c DESC LIMIT 1];
                                 
            }
        }
        calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2();
     }
     public void calculateTotalRevenue() {
     for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
                }
        for(BankBook_Entry__c acct : listAccount) {
            if(acct.Debit__c!= null)
                totalRevenue+= acct.Debit__c;
                }
totalRevenuee1=totalRevenuee+totalRevenue;
              
       
        
    }
    
    
    
    public void calculateTotalRevenue2() {
        for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
            
              
        }
        
    }
    public void calculateTotalRevenue1() {
        for(BankBook_Entry__c acct1 : listAccount) {
            if(acct1.CB__c!= null)
                totalRevenue1+= acct1.CB__c;
               
        }
        
    }
}

----------------------------

<apex:page docType="html-5.0" standardController="BankBook_Entry__c"  extensions="dateInPBTbaleController">


        <apex:define name="body">
            
            <div style="width:800px;margin: 0px auto 10px auto;border: 1px solid black;">
    <apex:form >
        <apex:pageBlock >
            
                    <b><apex:outputLabel value="Trial Balance Report On: "  style="font-weight: bold;color:red;"/></b>
                     <apex:inputField value="{!account.Date__c}" style="font-weight: bold;color:red;"/>
                     
                     
                   <!--- Trial Balance Report On: <apex:input type="date" value="{!dat}"/>-->
                 
                 <apex:commandButton value="Display" action="{!displaingTable}" reRender="tableId" style="font-weight: bold;color:red;"/><br/><br/><br/>
         
                 <apex:pageblockTable value="{!listAccount }" var="record" id="tableId" >
                    <apex:column value="{!record.Master_Code1__c}" headerValue="Master Code"/>
                    <apex:column value="{!record.Master_Name__c}" headerValue="Master Name"/>
                    <apex:column value="{!record.Debit__c}" headerValue="Debit(+)" style="color:green;" id="tableId">
                     <apex:facet name="footer" >
                    <apex:outputText value="Rs.{0, number, ###,###,###,##0.00}" style="font-weight: bold;color:green;font-size:12px;height=18px;" >
                        <apex:param value="{!totalRevenuee1}"  />
                    </apex:outputText>
                </apex:facet>
            </apex:column>
                    <apex:column value="{!record.CB__c}" headerValue="Credit(-)" style="color:red;">
                    <apex:facet name="footer" >
                    <apex:outputText value="Rs.{0, number, ###,###,###,##0.00}"  style="font-weight: bold;color:red;font-size:12px;height=18px;"  >
                        <apex:param value="{!totalRevenue1}"  />
                    </apex:outputText>
                </apex:facet>
            </apex:column>
             <td  style="background-color: #DAFFFF" ><b> <apex:outputText value="Current Total" /></b></td>
            <!--- <apex:column value="{!record.Bank_Closing_Balance__c}" headerValue="Bank Closing Balance" style="color:green;">
                    <apex:facet name="footer" >
                    <apex:outputText value="Rs.{0, number, ###,###,###,##0.00}"  style="font-weight: bold;color:green;font-size:12px;height=18px;"  >
                        <apex:param value="{!totalRevenuee}"  />
                    </apex:outputText>
                </apex:facet>
            </apex:column>-->
                    
                    
                    </apex:pageblockTable>
             
        </apex:pageBlock>
    </apex:form>
      </div>
        </apex:define>
</apex:page>

 
Best Answer chosen by SFDC Dummy
Abhishek BansalAbhishek Bansal
Hi,

Please find your required code below :
 
public with sharing class dateInPBTbaleControllernew1 {
    public BankBook_Entry__c account{get;set;}
    public List<BankBook_Entry__c> listAccount {get;set;}
    public Double totalRevenue {get;set;}
    public Double totalRevenue1 {get;set;}
    public Double totalRevenuee {get;set;}
    public Double totalRevenuee1 {get;set;}
   
    public Date dat {get;set;}
    
    public Map<Date,List<BankBook_Entry__c>> mapOfDateWithEntry;

    public dateInPBTbaleControllernew1(ApexPages.StandardController controller){
    
        account = new BankBook_Entry__c();
        totalRevenue = 0;
        totalRevenue1 = 0;
        totalRevenuee = 0;
        totalRevenuee1 = 0;
        
    }
     
   public void setValues(){
        listAccount  = new List<BankBook_Entry__c>([SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                        FROM BankBook_Entry__c
                        order by Master_Code1__c ASC ]);
        mapOfDateWithEntry = new Map<Date,List<BankBook_Entry__c>>();
        List<BankBook_Entry__c> listOfEntryData;
        for(BankBook_Entry__c entry : listAccount){
            if(mapOfDateWithEntry.containsKey(entry.Date__c)){
                mapOfDateWithEntry.get(entry.Date__c).add(entry);
            }
            else{
                listOfEntryData = new List<BankBook_Entry__c>();
                listOfEntryData.add(entry);
                mapOfDateWithEntry.put(entry.Date__c,listOfEntryData);
            }
        }
        calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2();
   }
   public void displaingTable(){
        setValues();
        if(account.Date__c != null){
            totalRevenue = 0;
            totalRevenue1 = 0;
            totalRevenuee = 0;
            totalRevenuee1 = 0;
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }
            else {
                Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                for(Date keyDate : allDateSet){
                    if(keydate < account.Date__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.Date__c < keyDate){
                        count ++ ;
                    }
                }
                Date requiredDate ;
                
                if(allDateSet.size() == count){
                    requiredDate = [Select Date__c from BankBook_Entry__c Order by Date__c Desc LIMIT 1][0].Date__c;
                }
                else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);
            }
        }
       calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2(); 
    
    }
     public void calculateTotalRevenue() {
     for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
                }
        for(BankBook_Entry__c acct : listAccount) {
            if(acct.Debit__c!= null)
                totalRevenue+= acct.Debit__c;
                
                }
totalRevenuee1=totalRevenuee+totalRevenue;
             
       
        
    }
    
    
    
    public void calculateTotalRevenue2() {
        for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
            
              
        }
        
    }
    public void calculateTotalRevenue1() {
        for(BankBook_Entry__c acct1 : listAccount) {
            if(acct1.CB__c!= null)
                totalRevenue1+= acct1.CB__c;
                 
               
        }
        
    }
}
Regards,
Abhishek
 

All Answers

suresh dupadasuresh dupada
Hi, as per your requirement, i am searching for a record 
1. based on date= 14-9-2015 (If records found then i need to display records)
2. in case 14-9-2015 records not found (Which records i need to display)
         Means, eaither records created on 13-9-2015 only or all records on  (13,12,11,10,9,........).
 
SFDC DummySFDC Dummy
Hi suresh records created on 13-9-2015 only. if there is no record on particular date it will be display only last day record
Navee RahulNavee Rahul

Hi SFDC Dummy,

use isEmpty rather than using NULL.

did you receiving the debug in 

if(listAccount.size() == 0){
}
in above line.

Thanks
D Naveen Rahul.
Abhishek BansalAbhishek Bansal
Hi,

Please find your required code below :
 
public with sharing class dateInPBTbaleControllernew1 {
    public BankBook_Entry__c account{get;set;}
    public List<BankBook_Entry__c> listAccount {get;set;}
    public Double totalRevenue {get;set;}
    public Double totalRevenue1 {get;set;}
    public Double totalRevenuee {get;set;}
    public Double totalRevenuee1 {get;set;}
   
    public Date dat {get;set;}
    
    public Map<Date,List<BankBook_Entry__c>> mapOfDateWithEntry;

    public dateInPBTbaleControllernew1(ApexPages.StandardController controller){
    
        account = new BankBook_Entry__c();
        totalRevenue = 0;
        totalRevenue1 = 0;
        totalRevenuee = 0;
        totalRevenuee1 = 0;
        
    }
     
   public void setValues(){
        listAccount  = new List<BankBook_Entry__c>([SELECT id, CB__c,Date__c,Debit__c,Master_Code1__c,Master_Name__c,Bank_Closing_Balance__c
                        FROM BankBook_Entry__c
                        order by Master_Code1__c ASC ]);
        mapOfDateWithEntry = new Map<Date,List<BankBook_Entry__c>>();
        List<BankBook_Entry__c> listOfEntryData;
        for(BankBook_Entry__c entry : listAccount){
            if(mapOfDateWithEntry.containsKey(entry.Date__c)){
                mapOfDateWithEntry.get(entry.Date__c).add(entry);
            }
            else{
                listOfEntryData = new List<BankBook_Entry__c>();
                listOfEntryData.add(entry);
                mapOfDateWithEntry.put(entry.Date__c,listOfEntryData);
            }
        }
        calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2();
   }
   public void displaingTable(){
        setValues();
        if(account.Date__c != null){
            totalRevenue = 0;
            totalRevenue1 = 0;
            totalRevenuee = 0;
            totalRevenuee1 = 0;
            Set<Date> allDateSet = mapOfDateWithEntry.keySet();
            if(allDateSet.contains(account.Date__c)){
                listAccount = mapOfDateWithEntry.get(account.Date__c);
            }
            else {
                Date compareDate ;
                Integer count = 0;
                for(Date firstKeydate : allDateSet){
                    compareDate = firstKeydate;
                    break;
                }
                for(Date keyDate : allDateSet){
                    if(keydate < account.Date__c && keydate > compareDate){
                        compareDate = keyDate;
                    }
                    if(account.Date__c < keyDate){
                        count ++ ;
                    }
                }
                Date requiredDate ;
                
                if(allDateSet.size() == count){
                    requiredDate = [Select Date__c from BankBook_Entry__c Order by Date__c Desc LIMIT 1][0].Date__c;
                }
                else {
                    requiredDate = compareDate;
                }
                listAccount = mapOfDateWithEntry.get(requiredDate);
            }
        }
       calculateTotalRevenue();
        calculateTotalRevenue1();
        calculateTotalRevenue2(); 
    
    }
     public void calculateTotalRevenue() {
     for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
                }
        for(BankBook_Entry__c acct : listAccount) {
            if(acct.Debit__c!= null)
                totalRevenue+= acct.Debit__c;
                
                }
totalRevenuee1=totalRevenuee+totalRevenue;
             
       
        
    }
    
    
    
    public void calculateTotalRevenue2() {
        for(BankBook_Entry__c acct2 : listAccount) {
            if(acct2.Bank_Closing_Balance__c!= null)
                totalRevenuee= acct2.Bank_Closing_Balance__c;
            
              
        }
        
    }
    public void calculateTotalRevenue1() {
        for(BankBook_Entry__c acct1 : listAccount) {
            if(acct1.CB__c!= null)
                totalRevenue1+= acct1.CB__c;
                 
               
        }
        
    }
}
Regards,
Abhishek
 
This was selected as the best answer