2

I have an account xyz and I need to calculate the sum of all related opporunities of this account and if the amount is less than 2000 rating should come '1' , if its less than 3000 rating should be '2' and less than 5000 rating should be '3'.. I am trying this way..

trigger OppoSum on Opportunity (after insert,after update) 
{
    Set<Id> accIdSet = new Set<Id>(); 
    List<opportunity> oppList = new List<opportunity>();
    for(opportunity o : trigger.new)
    {
        accIdSet.add(o.accountId);
    }

    map<Id,Account> mapAcc = new map<Id,Account>([Select id, name,
                                                         (Select id From Opportunities) 
                                                   From Account Where Id IN : accIdSet]);

    for(opportunity o : trigger.new)
    {
        Account acc = new Account();
        acc.Id = mapAcc.get(o.accountId).id;
        for(integer i =0;i<acc.opportunities.size();i++)
        {
            acc.Oppo_Amount__c = Oppo_Amount__c + o.amount;
        }
        if(acc.Oppo_Amount__c<2000)
        {
            acc.rating = '1';
        }
        else if(acc.Oppo_Amount__c<3000)
        {
            acc.rating = '2';
        }
        else(acc.Oppo_Amount__c<5000)
        {
            acc.rating = '3';
        }
    }
}
0

2 Answers 2

6

If your only concerned is the functionality you described above you don't need to rely on a trigger for doing so. As a best practice you should try solving your tasks with formulas, workflows, processes and only after that go for triggers as the latter ones are harder to maintain.

Having said this you can achieve all the functionality from your description using a rollup field on Account that would sum the amount of the opportunities, then you can create a text formula field which would display one of the 3 categories based on the rollup value.

I hope this makes sense.

2
  • yeah it makes sense but I need to write a trigger for that...
    – Anzar
    Apr 7, 2016 at 13:50
  • yeah.you can use the roolup field on account to count all the amount of related opportunity. Apr 7, 2016 at 14:30
0

Try use this code :

trigger sumOfOppInAccount on Opportunity (after insert,after update) {
    Decimal i ;
    List<Account> accList = new List<Account>();
    Set<Id> set1 = new Set<Id>();
    Map<Id,Decimal> map1 = new Map<Id,Decimal>();
    for(Opportunity opp:Trigger.new){
            if(set1.contains(opp.AccountId) && map1.get(opp.AccountId) !=null){
             i = i+opp.Amount;
             map1.put(opp.AccountId,i); 
            }
            else{
                 i = opp.Amount;
            set1.add(opp.AccountId);
            map1.put(opp.AccountId,i);
            }
    }   
    accList =[SELECT Oppo_Amount__c,Rating FROM Account WHERE Id IN :set1];
    for(Account ac:accList){
        ac.Oppo_Amount__c = map1.get(ac.Id);
        if(ac.Oppo_Amount__c <=2000){
            ac.Rating = '1';
        }
        else if(ac.Oppo_Amount__c <=3000 && ac.Oppo_Amount__c >2000){
            ac.Rating = '2';
        }
        else{
            ac.Rating = '3';
        }
    }
    update accList;
  }

enter image description here

Regards,

Ajay

1
  • its not working properly..
    – Anzar
    Apr 8, 2016 at 6:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.