Apex 类控制器随机联系记录 [英] Apex Class Controller Random Contact Records

查看:27
本文介绍了Apex 类控制器随机联系记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从特定视图中选择一小组随机联系人.如果记录的活动状态设置为已发送,则记录在该视图中.我创建了一个我认为可以工作的顶点类,但是我正在尝试使这个类成为控制器并构建一个视觉力页面.我对 salesforce 和 apex 很了解.在做了一些研究之后,我想我将不得不使用 getter 和 setter 来调用信息.我的顶级课程是

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');
        }
    }
}

非常感谢

推荐答案

试试下面的代码:

Visualforce 页面:

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 类:

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.

希望对你有帮助.

这篇关于Apex 类控制器随机联系记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆