You need to sign in to do that
Don't have an account?

Trigger creates two task per Product's Opportunity instead of one
Hi!
I've created a trigger to create a task everytime someone creates an Opportunity's product. But I have a problem, the trigger creates the task twice! I couldn't find yet the problem. Could someone tell what's wrong in the code?
Thanks! :smileyhappy:
trigger createTask2 on OpportunityLineItem (before insert, before update) {
List<Task> tasks = new List<Task>();
List<OpportunityLineItem> OppLIs = Trigger.new;
for (OpportunityLineItem OppLI : OppLIs) {
//Obtengo el Id del usuario que creo la oportunidad
Opportunity Opp = [ Select OwnerId from Opportunity where id=:OppLI.OpportunityId limit 1 ];
//User usuario = [ Select Name from ]
//Obtengo el día que se vence el producto
Date diaVenc = OppLI.Fecha_de_vencimiento__c;
Datetime dhReminder = datetime.newInstance(diaVenc.year(), diaVenc.month(),diaVenc.day());
dhReminder.addHours(10);
Task tsk = new Task(whatID = OppLI.OpportunityId,
Ownerid = Opp.OwnerId,
Subject = 'test',
ActivityDate = diaVenc,
IsReminderSet = true,
ReminderDateTime = dhReminder );
tasks.add(tsk);
}
insert tasks;
}
If I add two products to the Opportunity, the trigger would add two task, one for each product.
But the problem I was having ( because I've solved it) , was that the trigger added two task for one product.
I've solved when I took out before update. I don't understand it very well why It has been solved with that , but It did began to work fine now.