Search code examples
salesforceapex-codevisualforceapexforce.com

Apex Class Controller Random Contact Records


I am looking on selecting a small group of random contacts from a specific view. The records are in that view if their campaign status is set to sent. I have created an apex class that I think would work, however I am trying make this class a controller and build a visual force page. I am knew to salesforce and apex. After doing some research i think i will have to use getters and setters to call the information. My apex class is

public class MyController {
    Integer count = [SELECT COUNT() FROM Contact];
    Integer rand = Math.floor(Math.random() * count).intValue();
    Set<Id> contactIds = new Set<Id>();{
        for(CampaignMember cm : [Select Id, ContactId from CampaignMember where Status = 'To be Called' and Campaign.Name = '2014/15 Mascot Data']){
            contactIds.add(cm.ContactId);
            List<String> orderBys = new List<String>{'Email Asc','Email Desc','Lastname Asc','Firstname Desc','LastModifiedDate Desc','LastModifiedDate Asc','CreatedDate Asc','CreatedDate Desc','Id Asc','Id Desc'};
            String orderBy = orderBys.get(Math.mod(rand,orderBys.size()));
            List<Contact> contacts = (List<Contact>)Database.query('Select Name From Contact where ID in :contactIds Order By ' + orderBy + ' Limit 5 OFFSET :rand');
        }
    }
}

Many thanks


Solution

  • Try the following code:

    Visualforce Page:

    <apex:page controller="MyController">
       <apex:form>
          <apex:pageblock id="pb" title="Contact Information">
             <apex:commandButton value="Get Random Contacts" action="{!getContacts}" rerender="randomContacts"/> 
             <apex:outputPanel id="randomContacts">
              <apex:pageblock> 
                  <apex:PageBlockTable value="{!contacts}" var="item">
                      <apex:column headerValue="Contact Name" value="{!item.Name}"/>
                       --display columns you wish to show.
                  </apex:PageBlockTable>  
               </apex:pageblock> 
             </apex:outputPanel>
          </apex:pageBlock>
       </apex:form>
    </apex:page>      
    

    Apex Class:

    public class MyController 
    {
       public List<Contact> contacts{get;set;}
    
       public MyController()
       {
            getContacts();
       }
    
       public void getContacts()
       {
            Integer count = [SELECT COUNT() FROM Contact];
            Integer rand = Math.floor(Math.random() * count).intValue();
            Set<Id> contactIds = new Set<Id>();
            contacts = new List<Contact>();
    
            for(CampaignMember cm : [Select Id, ContactId from CampaignMember where Status = 'To be Called' and Campaign.Name = '2014/15 Mascot Data'])
            {
               contactIds.add(cm.ContactId);
            }
            List<String> orderBys = new List<String>{'Email Asc','Email Desc','Lastname Asc','Firstname Desc','LastModifiedDate Desc','LastModifiedDate Asc','CreatedDate Asc','CreatedDate Desc','Id Asc','Id Desc'};
    
            String orderBy = orderBys.get(Math.mod(rand,orderBys.size()));
    
            contacts = Database.query('Select Name From Contact where ID in :contactIds Order By ' + orderBy + ' Limit 100 OFFSET :rand');
        }
    }
    

    First time it load data when page loaded and click on the button to get next random contacts.

    Hope it helps you.