1

I created a custom object budget and create a lookup relationship with opportunity. So one budget lookup field on opportunity and update this field according to close date via trigger. If close date within month of Jan so budget field update as “Jan”. So all Jan close date opportunity come under Jan budget.

My requirement is when any opportunity stage change as close won so opportunities amount should be some and populate on custom field “won amount “ on budget object. Same with open opportunities all open opportunities amount should show on “pipeline amount” custom field.
I think trigger would be good approach but I don’t know how to query. Could you experts help me?

enter image description here

trigger updateField on Opportunity (after update) {

Map<Id,Decimal>  budgetIDTotalMap = new MAp<Id,Decimal>();  
for (Opportunity opp : Trigger.new)
{
      if(opp.stageName == 'ClosedWon'){
        Decimal t = 0;
        if(budgetIDTotalMap.containsKey(opp.budget__c) && budgetIDTotalMap.get(opp.budget__c) != null)
               t = budgetIDTotalMap.containsKey(opp.budget__c);
            budgetIDTotalMap.put(opp.budget__c,t+opp.Amount);
       }
}
List<Budget__c> blist = [select id,Won_Amount__c from budget__c where id in :budgetIDTotalMap.keySet()];
for(Budget__c c: blist)
{
if(budgetIDTotalMap.containskey(c.id))
{
  c.Won_Amount__c += budgetIDTotalMap.get(c.id);
}
}

 if(blist.size()>0)
 update blist;
}
  • I presume you can't use the stand roll up summary field due it not be – Girbot Jan 28 '16 at 7:15
  • Being a master detail relationship. There are several roll up summary helper classes available that you could leverage for example andyinthecloud.com/2013/07/07/… (sorry for typos, on mobile) – Girbot Jan 28 '16 at 7:17
  • Yes if possible because I am not able to think the logic. how can I count Amount as per stage? – Russel baker 1 Jan 28 '16 at 7:23
  • I am getting error Compile Error: Illegal assignment from Boolean to Decimal at line 9 column 20 t = budgetIDTotalMap.containsKey(opp.budget__c); @Ratan – Russel baker 1 Jan 28 '16 at 8:25
  • I think error is with decimal. I tried with t.intValue(); but no luck. – Russel baker 1 Jan 28 '16 at 9:15
1

So you can get all the budget Id and based on that get all the opportunity where stageName = ClosedWon. Use aggregate query to get the some of Amount..

trigger updateField on Opportunity (after update) {

    Map<Id,Decimal>  budgetIDTotalMap = new MAp<Id,Decimal>();  
    set<id> setbudgetId = new set<Id>();

    for (Opportunity opp : Trigger.new)
    {
        if(opp.stageName == 'ClosedWon' && opp.budget__c != null){
            setbudgetId.add(opp.budget__c);
        }
    }

    for(AggregateResult objArg : [SELECT budget__c, SUM(Amount) total FROM Opportunity WHERE budget__c IN: setbudgetId AND stageName = 'ClosedWon' GROUP BY budget__c])
    {
        budgetIDTotalMap.put((Id)objArg.get('budget__c'), (Decimal)objArg.get('total'));
    }
    List<Budget__c> blist = [select id,Won_Amount__c from budget__c where id in :budgetIDTotalMap.keySet()];
    for(Budget__c c: blist)
    {
        if(budgetIDTotalMap.containskey(c.id))
        {
            c.Won_Amount__c = budgetIDTotalMap.get(c.id);
        }
    }
    update blist;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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