function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Siddharth LakhotiaSiddharth Lakhotia 

Custom (sum)Rollup on Opportunity based on Opportunity Products value

Hi,

I have a field on Opportunity called Estimated Value. Its a blank text field. A user can come and enter the value in it

On opportunity products, I have a field called List Value 

I need help in writting a trigger to calculate list value of all the line items and rollup the sum to the estimated value field, if the field is not already filled. 

Can someone help me in writting one?
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Siddharth,

Try this code:
Map<Id,Integer> oppLineMap=new Map<Id,Integer>();
for(OpportunityLineItem opp:newList)
{

if(oppLineMap.containsKey(opp.Opportunity__c))
{
Integer ListValue=oppLineMap.get(opp.Opportunity__c);

oppLineMap.put(opp.opportunity__c,ListValue+opp.ListVal);
}
else
{
oppLineMap.put(opp.opportunity__c,opp.ListVal);
}

}
List<Opportunity> oppList=[select id,EstimatedVal__c from Opportunity where id in: oppLineMap.keySet() and (EstimatedVal__c==null or EstimatedVal__c=='')];
for(opportunity opp:oppList)
{
if(oppLineMap.containsKey(opp.Opportunity__c))
{
opp.EstimatedVal__c=oppLineMap.get(opp.Opportunity__c);
}
}

update oppList;