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
stefandlstefandl 

Trigger is duplicating record creation. Any idea why?

Hello, I've created a trigger in order to create a case when a new product is added to an opportunity, according some rules.

But now, for a product added case is created more then once.

 

This is the trigger:

 

 

trigger NewProductAdded on OpportunityLineItem (after insert) {

//Query for Case record types
     List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Case' and isActive=true];
     
//Create a map between the Record Type Name and Id for easy retrieval
     Map<String,String> CampaignsRecordTypes = new Map<String,String>{};
     for(RecordType rt: rtypes)
     CampaignsRecordTypes.put(rt.Name,rt.Id);
     
//Query for Users 

    List<User> users = [Select Name, Id From User];
    
//Create a map between the User Name and Id for easy retrieval

     Map<String,String> CaseUsers = new Map<String,String>{};
     for(User  usr: Users)
     CaseUsers.put(usr.Name,usr.Id);
     
for (OpportunityLineItem oli : Trigger.new) 
 { 
     if (oli.Implementation_DirectTrack__c == 'Yes'& (oli.Approval_status__c == 'Approved by AD'))
        
    { 
       Case c = new Case ( 
       Opportunity__c = oli.OpportunityId, 
       Status = 'New', 
       Origin = 'Web', 
       Type = 'Internal Case', 
       Reason = 'Add Campaign to R-ADserver', 
       Description = 'Hello, a new product has been added. Please add campaign to R-Adserver',
       R_Adserver_Status__c = '',
       RecordTypeId=CampaignsRecordTypes.get('Traffic'),
       Account_Manager__c = oli.Account_Manager__c,
       OwnerId = CaseUsers.get('Traffic List') 
       ); 
       insert c; 
    }   
     else if (oli.Implementation_DirectTrack__c == 'No' & (oli.Approval_status__c == 'Approved by AD') )
    { 
       Case c = new Case ( 
       Opportunity__c = oli.OpportunityId, 
       Status = 'New', 
       Origin = 'Web', 
       Type = 'Internal Case', 
       Reason = 'Add Campaign to Emediate', 
       Description = 'Hello, a new product has been added. Please add campaign to Emediate',
       RecordTypeId=CampaignsRecordTypes.get('Traffic'),
              Account_Manager__c = oli.Account_Manager__c,
       OwnerId = CaseUsers.get('Traffic List')  
       ); 
       insert c; 
    } 
    else { } 
 }         

}

 

 

Any idea about how to fix it?

 

Many thanks in advance,

 

Stefano Di Leone

 

 

Cory CowgillCory Cowgill

Have you reviewed the 'Triggers and Order of Execution' section of the developer guide? (http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm)

 

In brief, a few things can cause this:

 

1. Do you have any workflow updates? If you have workflow updates which are executing against these records, it will cause the before and after triggers to fire one more time (and only one more time). Review the Dev Guide link I provided to see how order of execution can fire a trigger twice.

 

2. Do you have recursive triggers firing? During the Case insert, is there a trigger which could cause an update to OpptyLineItem?

uptime_andrewuptime_andrew

Definitely sounds like something caused by the order of execution.