Apex Class Controller Random Contact Records

删除回忆录丶 提交于 2019-12-12 02:53:21

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/25303247/apex-class-controller-random-contact-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!