You need to sign in to do that
Don't have an account?

Trigger to prevent duplicate records and it should also work for Bulkification
Hi All,
Urgent suggestion needed!!
I am new to Salesforce and have a requirement to prevent user to create the duplicate record and it should work for bulkyfication as well.
Please find the screenshot below of the Ticket Object detail page. The requirement is that there should be validation if user try to create duplicate records for same(Customer, username,email-id) and within the period of Start date & End Date.
You can create the duplicate records if the start date & End date is before or after the existing record.
-->In order to explain this better, I want to apply a condition that user cannot create duplicate Ticket if the Start date or End date lying the in the existing record.
for ex- if u have this entry in your DB
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/20/2018 3/25/2018
Now you tried to Enter below. It Should Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/21/2018 3/25/2018 Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/23/2018 3/28/2018 Fail
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/26/2018 3/27/2018 Pass
->NYC Concert John Smith John@gmail.com John@gmail.com 25 3/18/2018 3/19/2018 Pass
Key Values to check duplicacy is Username, Customer, Email Id, StartDate, End Date
It should support bulkyfication.
create a trigger on Ticket object and use the below code. use can modify the code to reflect your column names and include username in the query. you can expand this to handle more logic if required.
trigger CheckDuplicateTicket on Ticket__c (Before Insert) {
for (Ticket__c NewTicket : Trigger.New) {
List <Ticket__c> DupTickets = [Select customer_name__c from Ticket__c where customer_name__c = :NewTicket.customer_name__c
and email_address__c = :NewTicket.email_address__c
and ( ( start_date__c <= :NewTicket.start_date__c and end_date__c >= :NewTicket.start_date__c )
)
];
if (DupTickets.size() > 0 )
{
/* NewTicket.Description__c = 'Duplicate'; */
NewTicket.addError('Duplicate Ticket Found');
}
}
}
By Falcon100 (Appreciate your feedback)