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
LuvaraLuvara 

Trigger is creating duplicate records

I'm trying to create a case related to the account of an opportunity that reaches a certain stage, but is not related to the current opportunity.

 

The trigger works, but it is created two cases instead of one. Any help would be appreciated.. my trigger and class is below.

 

Trigger:

trigger CreateCaseXML on Opportunity (after update) { List<Case> cases = new List<Case>(); for (Opportunity newOpp: Trigger.new) { if (newOpp.StageName == 'Booked' && newOpp.XML_SDK__c >1 && newOpp.CloseDate==System.today()) {cases.add(new Case(RecordTypeId = '0120000000095EH', AccountId = newOpp.AccountId, Priority = 'P4 - Low', OwnerId = '00500000006pXqU', Type = 'XML Gateway', Subject = 'XML Gateway Provision Request', Description = 'This was automatically created after the opportunity was booked on'+ ' ' + System.today())); } } insert cases; }

 

Test class:

public class CreateCaseXML { static testMethod void CreateCaseXML(){ //New account to test Account acct = new Account(name='test account'); insert acct; //New Opportunity to test Opportunity o = new Opportunity( accountid=acct.id,type='addon',Name='addon Test', StageName='Booked', XML_SDK__c=1500.00, CloseDate=System.today()); //New Case to test Case c = new Case( RecordTypeId = '0120000000095EH', AccountId = o.AccountId, OwnerId = '00500000006pXqU', Priority = 'P4 - Low', Type = 'XML Gateway', Subject = 'XML Gateway Provision Request', Description = 'This was automatically created after the opportunity was booked on'+ ' ' + System.today()); //Now insert case Test.startTest(); insert c; Test.stopTest(); } }

 

Thanks, Chris

 

 

JimRaeJimRae

It looks like your trigger is on Opportunity, but in your testmethod you don't insert the opportunity you are creating, but do insert a case, that is one issue.

You should insert the opportunity, which fires the trigger to create the case, then test to see if the case for the account that opportunity is related to was created.

LuvaraLuvara

I'm not quite sure how this one got marked as solved.. I fixed the test class, but I don't see anything in the results about it creating two cases.

 

 

public class CreateCaseXML { static testMethod void CreateCaseXML(){ //New account to test Account acct = new Account(name='test account'); insert acct; //New Opportunity to test Opportunity o = new Opportunity( accountid=acct.id,type='addon',Name='addon Test', StageName='Booked', X1St_Yr_ACV_Subscript__c=0.00, XML_SDK__c=1500.00, CloseDate=System.today()); //New Case to test Case c = new Case( AccountId = acct.id, RecordTypeId = '0120000000095EH', OwnerId = '00500000006pXqU', Priority = 'P4 - Low', Type = 'XML Gateway', Subject = 'XML Gateway Provision Request', Description = 'This was automatically created after the opportunity was booked on'+ ' ' + System.today()); //Now insert case Test.startTest(); insert o; Test.stopTest(); } }

 

It's creating one case when I create the opportunity with the criteria that should fire the trigger, and it's creating two cases if I'm updating an existing opportunity.

 

 

Chris

 

 

 

 

JimRaeJimRae
Could you post the system log generated when your run your testmethod?
LuvaraLuvara
 *** Beginning Test 1: CreateCaseXML.static testMethod void CreateCaseXML()

20090220220608.369:Class.CreateCaseXML.CreateCaseXML: line 8, column 9: Insert: SOBJECT:Account

*** Beginning Workflow Evaluation
User: Chris Luvara
Start Time: 20090220220609.017
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Account: test account 001S0000004TuXQ]
Rule Name: Customer Type
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria: 
[Account : Account Record Type equals Customer Accounts] 
AND [Account : Type equals Prospect]
Value found: 012000000008Qvv
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090220220609.020
*** Ending Workflow Evaluation20090220220608.369:Class.CreateCaseXML.CreateCaseXML: line 8, column 9: DML Operation executed in 650 ms
20090220220608.369:Class.CreateCaseXML.CreateCaseXML: line 26, column 9: Insert: SOBJECT:Opportunity
20090220220608.369:Class.CreateCaseXML.CreateCaseXML: line 26, column 9: Changing testing limit context based on DML operation of size: 1

*** Beginning CreateCaseXML on Opportunity trigger event AfterUpdate for 006S0000002TebA

20090220220609.861:Trigger.CreateCaseXML: line 5, column 9: SelectLoop:LIST:SOBJECT:Opportunity
20090220220609.861:Trigger.CreateCaseXML: line 18, column 5: Insert: LIST:SOBJECT:Case

*** Beginning Case Validation Rule Evaluation for null
Start Time: 20090220220609.895
Rule Name: Priorirty
Error Condition Formula: ISPICKVAL(Priority, "Px - New/No Priority")
Value(s) Found: Priority=P4 - Low
Result: PASS - Continue
End Time: 20090220220609.895
*** Ending Case Validation Rule Evaluation for null

Spooling All Immediate Actions
*** Beginning Workflow Evaluation
User: Chris Luvara
Start Time: 20090220220610.414
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
[Case: 00126462 500S0000000h44b]
Rule Name: Case Created
Evaluating Workflow Entry Criteria: 
Rule entry order: 1
[Case : Case Number not equal to null] 
AND [Case : Case Origin equals Email] 
AND [Case : Web Email not equal to null] 
AND [Case : Contact Email equals null]
Value found: 00126462
Value found: Internal
Criteria evaluates to false
Rule entry order: 2
[Case : Case Number not equal to null] 
AND [Case : Case Origin equals Email] 
AND [Case : Contact Email not equal to null]
Value found: 00126462
Value found: Internal
Criteria evaluates to false
Spooling All Immediate Actions
Starting evaluation of rule type Workflow
[Case: 00126462 500S0000000h44b]
Rule Name: Case Status to Resolved
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria: 
[Case : Status equals Resolved] 
AND [Case : Notify equals true]
Value found: Open
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Status to Reopened
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria: 
[Case : Status equals Reopened] 
AND [Case : Notify equals true]
Value found: Open
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Priority = Null
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria: 
[Case : Priority equals null]
Value found: P4 - Low
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Status to Engineering
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria: 
[Case : Status equals Engineering] 
AND [Case : Notify equals true]
Value found: Open
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Closed - Ok to Bill
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria: 
[Case : Closed equals true] 
AND [Case : Bill on Close equals true]
Value found: 0
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Status to Support
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria: 
[Case : Status equals Support] 
AND [Case : Notify equals true]
Value found: Open
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Owner is Jill
Trigger type: ON_CREATE_ONLY
Evaluating Workflow Entry Criteria: 
[Case : Case Owner contains Jill Chamberlain] 
AND [Case : Case Origin contains Self Service]
Value found: 00500000006pXqU
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Update RFE Status Assigned PM
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria: 
[Case : Case Owner starts with Iris] OR
[Case : Case Owner starts with Brett]
Value found: 00500000006pXqU
Value found: 00500000006pXqU
Criteria evaluates to false
[Case: 00126462 500S0000000h44b]
Rule Name: Case Status Support & XML
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria: 
[Case : Status equals Support] 
AND [Case : Application Area equals XML Gateway] 
AND [Case : Case Owner does not contain Miller] 
AND [Case : Assigned Owner does not contain Miller]
Value found: Open
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090220220610.421
*** Ending Workflow Evaluation20090220220609.861:Trigger.CreateCaseXML: line 18, column 5: DML Operation executed in 568 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 100
Number of DML rows: 2 out of 500
Number of script statements: 6 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Testing resources:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of script statements: 16 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
Static variables and sizes:
CreateCaseXML:cases:306


*** Ending CreateCaseXML on Opportunity trigger event AfterUpdate for 006S0000002TebA


Starting evaluation of rule type Escalation
End Time: 20090220220610.453
*** Ending Workflow Evaluation
*** Beginning Workflow Evaluation
User: Chris Luvara
Start Time: 20090220220610.530
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Account: test account 001S0000004TuXQ]
Rule Name: Customer Type
Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE
Evaluating Workflow Entry Criteria: 
[Account : Account Record Type equals Customer Accounts] 
AND [Account : Type equals Prospect]
Value found: 012000000008Qvv
Criteria evaluates to false
Spooling All Immediate Actions
Ending All Rules Evaluation
Bulk Execute all Immediate Actions
Starting evaluation of rule type Escalation
End Time: 20090220220610.530
*** Ending Workflow Evaluation20090220220608.369:Class.CreateCaseXML.CreateCaseXML: line 26, column 9: DML Operation executed in 1509 ms
20090220220608.369:Class.CreateCaseXML: line 4, column 28: returning from end of method static testMethod void CreateCaseXML() in 2162 ms

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 500
Number of SOSL queries: 0 out of 20
Number of DML statements: 2 out of 100
Number of DML rows: 2 out of 500
Number of script statements: 6 out of 200000
Maximum heap size: 0 out of 500000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 10
Number of System.runAs() invocations: 0 out of 20

Testing resources:

Resource usage for namespace: (default)
Number of SOQL queries: 1 out of 20
Number of query rows: 0 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of script statements: 17 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
*** Ending Test CreateCaseXML.static testMethod void CreateCaseXML()
JimRaeJimRae

Interesting.  Do you have any other triggers on the Opportunity?  One thing I noticed is that the trigger was an after update, but you don't update the opportunity in your testmethod, you only insert it.  Something else, either a trigger or a validation rule must be doing an update on the record.  Other than that, nothing obvious in the log, unfortunately.

My only other suggestion would be to put in some debug statements in your trigger so you can see where it is firing, and what the variables look like while is executing.

 

Here are some things you could look at:

 

 

trigger CreateCaseXML on Opportunity (after update) { system.debug('\n\nIN TRIGGER'); List<Case> cases = new List<Case>(); for (Opportunity newOpp: Trigger.new) { system.debug('\n\nIN TRIGGER LOOP: Opportunity='+newOpp); if (newOpp.StageName == 'Booked' && newOpp.XML_SDK__c >1 && newOpp.CloseDate==System.today()) {cases.add(new Case(RecordTypeId = '0120000000095EH', AccountId = newOpp.AccountId, Priority = 'P4 - Low', OwnerId = '00500000006pXqU', Type = 'XML Gateway', Subject = 'XML Gateway Provision Request', Description = 'This was automatically created after the opportunity was booked on'+ ' ' + System.today())); } } system.debug('\n\nCASES List is size '+cases.size()); insert cases; }

 

 

 

WhyserWhyser

Do you have any workflow rules that run a field update on the Opportunity object?

And your trigger shouldn't run on an after update trigger if you're doing an insert.

I didn't go through your log, but this is my guess.

 

This is the only way I can see this problem happening:

on insert:

- workflow rule runs, does a field update on opportunity

- after update trigger on opportunity runs, creating the case

 

on update

- after update trigger on the opportunity runs, creating the case

- the workflow rules field update are executed as a result of updating the opportunity

- the after update trigger fires again, due to the field update, creating another case.

 

EDIT: If this is your issue, I have dealt with it in this thread: http://community.salesforce.com/sforce/board/message?board.id=apex&thread.id=9639

Message Edited by Whyser on 02-23-2009 12:29 PM
MaxaMaxa

hi,

is there any way i can tweakt he code to createnew opportunity when existing opportunity been set to closed won?

i played around it but could not acompish it by myself