1

Im working on a visualforce page for the contact standard object.

  1. First I created an custom button and and directed it to a visualforce page
  2. I set up the page to display a pop-up once a user enters an email
  3. The pop-up displays all records related to that email

Instead of showing all records, I only want to show the most recently modified record.

Page

<apex:page standardController="Contact" extensions="TestPopup" showHeader="false" sidebar="false">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="show popup" action="{!showPopup}" rerender="popup" status="status"/>
                <apex:outputPanel id="popup">
                <apex:outputPanel id="popInnerOutputPnl" styleClass="customPopup" layout="block" rendered="{!displayPopUp}">
                    <apex:commandButton value="X" title="Close the popup" action="{!closePopup}" styleClass="closeButton" rerender="popup">
                    </apex:commandButton>
                    <apex:pageblockSection>                         

                        <apex:pageblockSectionItem >
                        <apex:outputLabel value="Email" for="address"></apex:outputLabel>
                                <apex:inputText value="{!searchstring}" label="Input"/>   
                        </apex:pageblockSectionItem>

                    </apex:pageblockSection>

                    <apex:commandButton value="Ok" action="{!redirectPopup}" styleClass="closeButton" rerender="popup">
                    <apex:commandButton value="Search" action="{!Search}" reRender="popup"/>
                    </apex:commandButton>
                </apex:outputPanel>

            <apex:pageblockTable var="con" value="{!contact}">
                <apex:column value="{!con.FirstName}"/>
                <apex:column value="{!con.LastName}"/>
                <apex:column value="{!con.Email}"/>
                <apex:column value="{!con.Account.Name}"/>
            </apex:pageblockTable>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>

    <style type="text/css">
        .customPopup {
            background-color: white;
            border-style: solid;
            border-width: 2px;
            left: 20%;
            padding: 10px;
            position: absolute;
            z-index: 9999;
            /* These are the 3 css properties you will need to tweak so the pop 
                                    up displays in the center of the screen. First set the width. Then set 
                                    margin-left to negative half of what the width is. You can also add 
                                    the height property for a fixed size pop up.*/
            width: 500px;
            top: 20%;
        }

        .disabledTextBox {
            background-color: white;
            border: 1px solid;
            color: black;
            cursor: default;
            width: 90px;
            display: table;
            padding: 2px 1px;
            text-align:right;
        }   

        .closeButton {
            float: right;
        }
    </style>

</apex:page>

Class:

public with sharing class TestPopup {

    public Boolean displayPopup {get;set;}
    public list<contact> contact {get;set;}
    public string searchstring {get;set;}  
    //  public string Email { get; set; }

    public TestPopup(ApexPages.StandardController controller) {}

    public TestPopup () 
    {
        // contact  = new list<Contact>();
    }

    public void showPopup()
    {
        displayPopup = true;
    }

    public void closePopup() 
    {
        displayPopup = false;
    }

    public PageReference redirectPopup()
    {

        displayPopup = false;

        //Please uncomment below 3 statements and replace YourObjectId
        // PageReference p=new Pagereference('/'+YourObjectId);
        // p.setRedirect(true);

        return null;

    }

    public PageReference Search()
    {
        displayPopup = false;

        //searched = true;

        string searchquery='select firstName,LastName,id,Account.Name,email from Contact where email like \'%'+searchstring+'%\' Limit 20';  
        contact= Database.query(searchquery);  

        system.debug('record is---->' +contact);

        return null;
    }
}

1 Answer 1

1

You can do this inside the WHERE clause of your soql query.

WHERE Email LIKE \'%'+searchstring+'%\' ORDER BY LastModifiedDate DESC LIMIT 1

Set the limit to one to return just the top record, and then set the ORDER BY clause to sort the records by the LastModifiedDate. You'll need to specify that the order is DESC for descending order, to get the highest date at the top of the returned list, and the oldest date at the bottom.


Additionally, if you are new to salesforce development, I would recommend both the Apex trailhead and the Visualforce trailhead. They're an excellent way to get accustomed to the platform.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .