获取帐户中的商机数量

时间:2016-08-24 04:42:14

标签: salesforce apex

如何计算与帐户相关的机会数量,

创建/删除商机时,帐户的总机会字段数应增加/减少。

如何解决它,pl帮我提供示例代码。

3 个答案:

答案 0 :(得分:1)

实际上,如果您需要计算与帐户相关的所有商机,则无需编写代码。在帐户上创建“汇总/汇总”字段类型。评估Opportunity对象,并运行“Count”操作。就是这样!

<强> UPD: 如果你需要用触发器来解决它,它将看起来像这样:

trigger CountOpportunitiesOnAccount on Opportunity (after insert, after delete){
    Set<Id> aId = new Set<Id>();
    if(Trigger.isInsert || Trigger.isDelete || Trigger.isUndelete){
        for(Opportunity opp : Trigger.New){
            aId.add(opp.AccountId);
        }
        updateAccounts(aId);
    }

    if(Trigger.isDelete){
        for(Opportunity opp : Trigger.old){
            aId.add(opp.AccountId);
        }
        updateAccounts(aId);
    }

    private void updateAccounts(Set<Id> accIds){
        List<Account> accs = [select id, OpportunitiesAmount from Account where Id in :accIds];
        List<Opportunity> opps = [select id from Opportunity where AccountId in :accIds];
        for(Account a : accs){
            a.OpportunitiesAmount = opps.size();
        }
        update accs;
    }
}

答案 1 :(得分:1)

所以,你走了。这是精确代码,它计算帐户的相关机会的确切数量,并在帐号的标准字段中填充它(您也可以在自定义字段上添加它)。

trigger TriggTask on Opportunity (after insert, after delete) 
{
List<id> TriggerList = new List<id>();
if(Trigger.isInsert)
{
    List<id> TriggerList = new List<id>();
    for(Opportunity Opp : Trigger.new)
    {
        TriggerList.add(Opp.AccountId);
    }
    List<Account> DML = New List<Account>();
    List<Account> Ls = [select id,(select id from Opportunities) from Account where id in:TriggerList];  

    for(Account AccNew : ls)
    {
        Integer Num = AccNew.Opportunities.size();
        AccNew.AccountNumber=String.valueOf(Num);
        DML.add(AccNew);
    }
    update ls;
}

if(Trigger.isDelete)
{
    for(Opportunity Opp : Trigger.old)
    {
        TriggerList.add(Opp.AccountId);
    }

    List<Account> DML = New List<Account>();
    List<Account> Ls = [select id,(select id from Opportunities) from Account where id in:TriggerList];
    for(Account act : Ls)
    {
        act.AccountNumber=String.valueOf(act.Opportunities.size());
        DML.add(act);
    }
    update Ls;
}  
}

答案 2 :(得分:0)

如果您必须编写触发器,您可能还会考虑重新设置机会。这就是许多开发人员首选Roll-Up Summary字段的原因。可能存在产生错误或意外结果的边缘情况。

trigger CountOpportunitiesOnAccount on Opportunity (after insert, after delete, after update){
Set<Id> aId = new Set<Id>();   
if(Trigger.isInsert || Trigger.isDelete || Trigger.isUndelete || Trigger.isUpdate){
    for(Opportunity opp : Trigger.New){
        if(Trigger.isUpdate){
            if(Trigger.newMap.get(opp.Id).AccountID != Trigger.oldMap.get(opp.Id).AccountID){
                aId.add(opp.AccountId);
            } 
        }
        else{
            aId.add(opp.AccountId);
        }
    }  
    if(aID != NULL && aID.size() > 0){
        updateAccounts(aId);
    }       
}
if(Trigger.isDelete){
    for(Opportunity opp : Trigger.old){
        aId.add(opp.AccountId);
    }
    updateAccounts(aId);
}   
private void updateAccounts(Set<Id> accIds){
    List<Account> accs = [select id, Number_Of_Opps__c, (select Id from Opportunities) from Account where Id in :accIds];
    List<Account> liAccToUpdate = new list<Account>();
    //Should be able to handle more than one opportunity/account in the trigger.
    for(Account a : accs){
        //Prevent recursive updating
        if(a.Number_Of_Opps__c != a.Opportunities.size()){
            a.Number_Of_Opps__c = a.Opportunities.size();
            liAccToUpdate.add(a);
        } 
    }
    if(liAccToUpdate != NULL && liAccToUpdate.size() > 0){
        update accs;
    }
}
}