0

每个联系人的活动数量字段,无论类型或状态如何。完整详细信息:要求能够基于联系人或客户或机会创建报告,并查看与联系人、客户或机会相关联的活动总数的“摘要”字段。请注意,我们不想为每个活动查看一行,我们希望为每个联系人或 opp 或帐户查看一个带有活动摘要计数的行。注意:原始请求还包括对报告进行唯一活动计数的能力

4

1 回答 1

0

我没有测试过这段代码,但你可以为插入做这样的事情(你还需要涵盖更新和删除)。在此示例中,NumberOfActivites__c 是您在联系人对象上的自定义任务计数字段:

Map<Id,Integer> countMap = new Map<Id,Integer>();
List<Contact> contactList = new List<Contact>();

for (Task t : trigger.new){
    //get id's of all contacts affected by the batch
    Id w = t.whoId;
    if (w.getSObjectType().getDescribe().getName() == 'Contact'){
        //since there could be more than one task related to a contact 
        //in a batch, you would have to count them
        if (countMap.keyset().containts(w)){
            countMap.get(w) += 1;
        } else {
            countMap.put(w,1);
        }
    }
}

//get list of contacts to be updated
contactList = [Select Id, NumberOfActivities__c
               From Contact
               Where Id In :countMap.keyset()];

//modify contacts in list with new count
for (Contact c : contactList){
    c.NumberOfActivites__c = c.NumberOfActivites__c + countMap.get(c.Id));
}

//do the update
update contactList;
于 2016-03-22T03:42:06.417 回答