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
TheresaAndersonTheresaAnderson 

Trigger is producing 2 records

The following trigger is creating 2 record to the custom object.  The first record represents the before results and the second record is the new information.

 

trigger CreateAlertonOpptyNextSteps on Opportunity (before update, before insert) {
  List<Opportunity> uOppty = new List<Opportunity>();
  List<Opportunity_Next_Steps_History__c> ltask = new List<Opportunity_Next_Steps_History__c>();
  for(Opportunity a: Trigger.new){
    if(a.SYS_ONS_Created__c = True && (a.X1st_Next_Step__c != '' && a.X2nd_Next_Step__c != '' && a.X3rd_Next_Step__c != '')
       && (a.X1st_Next_Step__c != Trigger.oldMap.get(a.Id).X1st_Next_Step__c || 
           a.X2nd_Next_Step__c != Trigger.oldMap.get(a.Id).X2nd_Next_Step__c ||
           a.X3rd_Next_Step__c != Trigger.oldMap.get(a.Id).X3rd_Next_Step__c)){
     Opportunity_Next_Steps_History__c t = new Opportunity_Next_Steps_History__c();
     t.Opportunity_ID__c=a.Id;
...

    t.SYS_ONS_Created_History__c = True;
     ltask.add(t);     
     Insert ltask;
    }
    a.SYS_ONS_Created__c = False;
    update uoppty;
   }}

 

What am I missing?

Best Answer chosen by Admin (Salesforce Developers) 
TheresaAndersonTheresaAnderson

I have solved the problem...the problem was directly related to the when I was updating my system checkbox on the opportunity. 

All Answers

cirrocirro

The trigger is probably firing twice, when you call 

 

update uoppty;

 

You might want a boolean to track if the trigger has already run, as described here:

 

http://developer.force.com/cookbook/recipe/controlling-recursive-triggers

souvik9086souvik9086

First of all you are inserting Opportunity_Next_Steps_History__c within for loop. Insert outside for loop.

And Secondly you are using before trigger. So why are you using update statement? It will automatically update by assigning that. Try the below code

 

trigger CreateAlertonOpptyNextSteps on Opportunity (before update, before insert) {
  List<Opportunity> uOppty = new List<Opportunity>();
  List<Opportunity_Next_Steps_History__c> ltask = new List<Opportunity_Next_Steps_History__c>();
  for(Opportunity a: Trigger.new){
    if(a.SYS_ONS_Created__c = True && (a.X1st_Next_Step__c != '' && a.X2nd_Next_Step__c != '' && a.X3rd_Next_Step__c != '')
       && (a.X1st_Next_Step__c != Trigger.oldMap.get(a.Id).X1st_Next_Step__c || 
           a.X2nd_Next_Step__c != Trigger.oldMap.get(a.Id).X2nd_Next_Step__c ||
           a.X3rd_Next_Step__c != Trigger.oldMap.get(a.Id).X3rd_Next_Step__c)){
     Opportunity_Next_Steps_History__c t = new Opportunity_Next_Steps_History__c();
     t.Opportunity_ID__c=a.Id;
...

    t.SYS_ONS_Created_History__c = True;
     ltask.add(t); 
    }
    a.SYS_ONS_Created__c = False;
   }

  if(ltask.size()>0){

insert ltask;

}

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

TheresaAndersonTheresaAnderson

I have tried both recommendations and while the trigger is compiling, the result remains at 2 records. 

 

 

souvik9086souvik9086

Two records of the custom object are creating under same opportunity?

 

Do you want to fire the trigger for both event(insert/update)? Please check whether for one of the event, is it creating 2 records at a time?

 

Thanks

TheresaAndersonTheresaAnderson

I removed the 'before insert' and the problem remains.  Thank you.  I want to capture the information as the opportunity is created and when updated for a small list of fields.

TheresaAndersonTheresaAnderson

Yes, the same opportunity is causing the event to create 2 records in the custom object.  I am seeing a record before the changes and a record after the changes. 

TheresaAndersonTheresaAnderson

I have solved the problem...the problem was directly related to the when I was updating my system checkbox on the opportunity. 

This was selected as the best answer