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
Ramesh VaratharajRamesh Varatharaj 

trigger for annual contract value

Hello experts, I need your help on a trigger. I have a requirement to calcualte the annual contract value for opportunities, this should be at both product level and at opportunity level. To be exact, i need to sum the first 12 months Revenue from productlineitemschedule at each productlineitem and then a total one at opportunity level. Could you please help with a sample code which could manage bulk record updates please. 
Best Answer chosen by Ramesh Varatharaj
Ramesh VaratharajRamesh Varatharaj
okay, finally solved the issue with ACV and went with flow as i do not have to worry about test class etc., thanks all

All Answers

George AdamsGeorge Adams
What have you done so far?
Ramesh VaratharajRamesh Varatharaj
Hi George, I am not good with coding. This is what i have now - but this is triggered at opportunityproductitem level and rolls up only at productlineitem level. I need assistance to trigger at opportunity level and update the value at opportunty and opportunitylineitem as well. Also i do not know how can i limite the records to first 12months.


trigger ACVvalues on Opportunitylineitem (before update) {

map<ID, double> mapamount = new map<ID, double>();
// ACV

for(AggregateResult ar : [Select Sum(Revenue) numRecs, opportunitylineitemid OppId From opportunitylineitemschedule 
Where opportunitylineitemid in :trigger.newmap.keyset() group by opportunitylineitemid ])

{
    mapamount.put((ID)ar.get('OppId'), (Double)ar.get('numRecs'));
}



for(opportunitylineitem opp : trigger.new)
{
  Opp.Acv__c = mapamount.get(opp.id);
  
    
}

}
Ramesh VaratharajRamesh Varatharaj
i managed to implement this through visual flow. Only thing which i need to validate is 12months value, currently i am filtering based on schedule date <= service date + ((365/12)*11). Not sure whether this will work for all conditions, validating them now. 
Ramesh VaratharajRamesh Varatharaj
okay, finally solved the issue with ACV and went with flow as i do not have to worry about test class etc., thanks all
This was selected as the best answer