+ Start a Discussion

Trigger to compare life of the contract for productos

Hello all!

I have a question, i have a field on my opp products for the "Life of the contract" call (Vigencia) it's a number field, what i need is to put that value on opp field called Vigencia_MAX, but i have two conditions for that:
1) Put the Vigencia on Opp from the product with the highest price or if
2) the price are the same, put the Highest Vigencia on pp.

So for that i have this

trigger VigenciaOpp on OpportunityLineItem (after insert, after update, after delete) {

list<id> oppids=new list<id>();
list<opportunity> oppnames=new list<opportunity>();
for(OpportunityLineItem c:trigger.new){
List<opportunity> opps=new List<opportunity>([select id, (select id,Vigencia_N_mero__c,TotalPrice from OpportunityLineItems Where Califica_a_Comision__c=True Order by Vigencia_N_mero__c DESC ,Vigencia_N_mero__c DESC Limit 1 ) from opportunity where Opportunity.Id in:oppids]);
Decimal myDecimal = 0.00;
Integer n=myDecimal.intValue();
for(opportunity o:opps){
for(OpportunityLineItem o1:o.OpportunityLineItems){
Decimal myDecimal2 =o1.Vigencia_N_mero__c;
update oppnames;

It work, but no in the way that I need, i'll really appreciate your help!
Murali MattaMurali Matta
Hi Ecorona,

Need more information.
1. What is pp?

Hello Murali! Sorry pp is Opp (Opportunity)