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
Rohan ChadhaRohan Chadha 

Duplicate record prevention is throwing validation even on Editing the existing records

Hi All,

Urgent help Needed!!
I need your help since i am new to salesforce.

Actually the issue is that i have written one trigger to validate the duplicate record creation for a perticular object but it is throwing the validation even when you are trying to edit the existing record.

Kindly sugest me the solution or please guide me if it is a valid scenario.

The trigger which i have written is
trigger duplicat on Ticket__c(before insert,before update)
 {
    list<string> cust = new list<string>();
    list<string> usernme =new list<string>();
    list<string> email= new list<string>();

for(Ticket__c a:Trigger.new)
    {
    cust.add(a.Customer);
   usernme.add(a.username);
    email.add(a.email-id);

    }

List<Work_Order_Rate__c> WOR =  List<Ticket__c> tickt1 =[select User_Name__c,Customer__c,Start_Date__c, End_Date__c from Ticket__c where User_Name__c=:usernme and Customer__c=:a.cust];

if(WOR.size()>0)   
for(Ticket__c a:Trigger.new)
        {
        for(Ticket__c b:WOR )
               {                
                 if(b.Start_Date__c  tickt1.Start_Date__c || b.Start_Date__c <=tickt1 .End_Date__c ||
                b.End_Date__c >=tickt1 .Start_Date__c || b.End_Date__c <=tickt1 .End_Date__c )

                a.adderror('You cannot create a duplicate Ticket.');
                }
        }
}

Regards,
Rohan
Manohar kumarManohar kumar

Hi Rohan, 

If you don't want trigger to be run when editing existing records, you can simply remove "before update" from your trigger. 

Please let me know, if you can't for some other resons. 

Thanks,

Manohar

Rohan ChadhaRohan Chadha
Hi Manohar,

I have one question like if a user tried to edit the existing record and try to modified differnt entries which may create duplicate record then i believe  "Before update" is required in trigger.

Please let me know if there is any other way to restrict it.

Quick response is highly appreciated.


Regards,
Rohan
Manohar kumarManohar kumar

Hi Rohan, 
Can you pls paste your actual code. I will help you, also you are checking ticket object fields with work order rate ? You are specifing duplicate record if username, customer and email are same ?  pls let me know. 

Thnaks,
Manohar

mukesh guptamukesh gupta
Hi Rohan,

you can check duplicate record by email id :-

 Please use this code and let me know if you  have any issue.
 
trigger duplicat on Ticket__c(before insert,before update)
    List<string> emailList= new List<string>();
    List<Ticket__c> ticketList = new List<Ticket__c>();

  for(Ticket__c t:Trigger.new)
    {
   	emailList.add(t.email-id);
     }

  ticketList = [select User_Name__c,Customer__c,Start_Date__c, End_Date__c from Ticket__c where  email IN: emailList];

        for(Ticket__c tic : trigger.new){

            if(ticketList.size() > 0 ){
                tic.addError('Ticket already exists in your Organization with name '+tic.User_Name__c);
}

        }

}

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh