You need to sign in to do that
Don't have an account?
Help: Trigger creates duplicates
I created a trigger to insert and a new Ticket (custom object) whenever a new Case has been created and updates a Ticket when a Case has been updated. The problem I am having is that my trigger is creating duplicates when a new Case is created. How can I prevent the duplicate record from being created?
I have searched high and low and have tried a couple of solutions including the cookbook recipe Preventing Duplicate Records from Saving but I have not been successful implementing the code in my current trigger.
Tickets has a Master Detail field Case__c to Cases.
My code is as follows:
trigger createTicket2 on Case (after insert, before update) { //create new case if it does not exist for(Case c : Trigger.new){ Tickets__c tik = new Tickets__c( case__c = c.id, subject__c = c.subject, origin__c = c.Origin, type__c = c.Type, Case_Record__c = c.Record_Type_Umbrella__c, new_problem__c = c.New_Problem__c, problem_area__c = c.problem_area__c, SAP_Return_Order_Number__c = c.SAP_Return_Order_Number__c); insert tik; List<Case> openCases = [SELECT c.subject, c.origin, c.problem_area__c, c.sap_return_order_number__c , c.LastModifiedDate, c.createddate FROM Case c WHERE (c.Type ='Out of Box Failure') FOR UPDATE]; if(trigger.isupdate) { for(Case c1 : openCases) { list<Tickets__c> t= [select id, subject__c from tickets__c where case__c=: c1.id ]; // if (c1.LastModifiedDate != null && c1.LastModifiedDate > c1.CreatedDate) { // c1.subject=t.subject__c; for (tickets__c t1: t ) { if(shouldIRun.canIRun()==false ) FutureGroup.FutureMethod(c1.id, t1.id); } } } } } }
Thanks,
I have modified your code as required. It should now work fine. Please do let me know if you still have issue:-
All Answers
The problem lies in the way you are inserting trigger record. You are inserting ticket no matter what kind of DML operation of case is. By going through your requirement, I assume you need to insert Ticket record only on insertion of Case record. Please put a check before creating Ticket record. Something like below:-
if(trigger.isInsert){
Tickets__c tik = new Tickets__c(
case__c = c.id,
subject__c = c.subject,
origin__c = c.Origin,
type__c = c.Type,
Case_Record__c = c.Record_Type_Umbrella__c,
new_problem__c = c.New_Problem__c,
problem_area__c = c.problem_area__c,
SAP_Return_Order_Number__c = c.SAP_Return_Order_Number__c);
insert tik;
}
if(trigger.isInsert){Tickets__c tik = new Tickets__c( case__c = c.id, subject__c = c.subject, origin__c = c.Origin, type__c = c.Type, Case_Record__c = c.Record_Type_Umbrella__c, new_problem__c = c.New_Problem__c, problem_area__c = c.problem_area__c, SAP_Return_Order_Number__c = c.SAP_Return_Order_Number__c); insert tik;}
Also, I can see few more problem with this trigger. Happy to discuss if you want. :)
The solution you provided didn't seem to work. I'm still getting duplicates whenever a record is inserted and updated.
Could you please share your updated code? Perhaps then I will be in position to help you.
Regards,
Digamber Prasad
I run the code before and after making modification both way. Before modification, trigger was creating duplicate errors, however, after putting check it is no more creating duplicate records. Just to be clear, I am assuming you are not creating any Ticket record in 'FutureGroup.FutureMethod(c1.id, t1.id)' method.
Also, this modification is not going to get rid off of older duplicate records. So, please create a new case record and do testing on this.
Your reply is highly appreciated!
Regards,
Digamber Prasad
Correct - FutureGroup.FutureMethod is not creating a new ticket.
I'm not worried about older duplicates since I'm testing in SB.
Can you please post the code that you got to work? My code won't save, I must have done something wrong with the check.
This is what I have with the check built in...
You have put the check in wrong place, because of which it is still creating duplicate records. Below is code I ran on my org by commening few lines:-
trigger createTicket2 on Case (after insert, before update) {
//create new case if it does not exist
for(Case c : Trigger.new){
if(trigger.isInsert){
Tickets__c tik = new Tickets__c(
case__c = c.id,
subject__c = c.subject //,
/*
origin__c = c.Origin,
type__c = c.Type,
Case_Record__c = c.Record_Type_Umbrella__c,
new_problem__c = c.New_Problem__c,
problem_area__c = c.problem_area__c,
SAP_Return_Order_Number__c = c.SAP_Return_Order_Number__c*/
);
insert tik;
}
List<Case> openCases = [SELECT c.subject, c.origin , c.LastModifiedDate, c.createddate FROM Case c
WHERE (c.Type ='Out of Box Failure')
FOR UPDATE];
if(trigger.isupdate)
{
for(Case c1 : openCases)
{
list<Tickets__c> t= [select id, subject__c from tickets__c where case__c=: c1.id ];
// if (c1.LastModifiedDate != null && c1.LastModifiedDate > c1.CreatedDate)
{
// c1.subject=t.subject__c;
for (tickets__c t1: t )
{ /*
if(shouldIRun.canIRun()==false )
FutureGroup.FutureMethod(c1.id, t1.id);
*/
}
}
}
}
}
}
I have modified your code as required. It should now work fine. Please do let me know if you still have issue:-
Digamber - thanks so much for your help!