我正在寻找从特定视图中选择一小组随机联系人。如果他们的广告系列状态设置为已发送,则该记录位于该视图中。我已经创建了一个我觉得可以工作的顶级类,但是我正在尝试使这个类成为一个控制器并构建一个可视化强制页面。我对salesforce和apex很了解。做了一些研究后,我想我将不得不使用getter和setter来调用这些信息。我的顶级课程是
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');
}
}
}
非常感谢
答案 0 :(得分:0)
请尝试以下代码:
Visualforce页面:
<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');
}
}
首次加载页面时加载数据,然后单击按钮获取下一个随机联系人。
希望它对你有所帮助。