I'm hoping someone can help me with an issue I have regarding an Apex Trigger that creates a child record when a field of its corresponding master record is set to a particular value. The master object is Opportunity
and the child record is a custom object called Ad_Ops_Task__c
. My problem is that the following code creates duplicates of the child record when the Opportunity
field is set to the value specified in my code below:
trigger CreateAdOpsTask on Opportunity (after update)
{
List <Ad_Ops_Task__c> Ad_Ops_Tasks = new List <Ad_Ops_Task__c> ();
for(Opportunity a: Trigger.new)
{
if(a.StageName=='Ad6 - IO Received')
{
Ad_Ops_Task__c AOSTask = new Ad_Ops_Task__c ();
AOSTask.Opportunity__c = a.Id;
AOSTask.Name = 'AdOps Task - ' + a.Name + ' - ' + a.Media_Plan_Id__c;
Ad_Ops_Tasks.add(AOSTask);
}
}
insert Ad_Ops_Tasks;
}
Ad_Ops_Task__c
record for each opportunity in that stage. Do you have a workflow rule on this object that updates fields on the parent opportunity record? Or are you saying that there should only ever be one child record for the opportunity in this stage?