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?
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;
}