+ Start a Discussion
Rohan ChadhaRohan Chadha 

Write test class for Trigger which check duplicate record

Hi All,

Urgent help needed.!!
Detail Page
I am new to salesforce  and  need some help in writing the test class for below Trigger. This trigger is validating the duplicate records.

Request you to please help me in writing the test class for this.

Below Customer and Username are lookup field. Here i would like to test for single and bilk records.

Request you to please provide me the test class.




 
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<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(tickt1.size()>0)   
for(Ticket__c a:Trigger.new)
        {
        for(Ticket__c tickt1 )
               {                
                 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.');
                }
        }
}

 
NagendraNagendra (Salesforce Developers) 
Hi Rohan,

Sorry for this issue you are encountering.

May I suggest you please find the sample code below and tweak it as per your requirement which might help. Hope this helps.

Thanks,
Nagendra
UjwalaUjwala
@istest
public class testTicket{
@istest static void testDuplicateTicket()
{
 create two ticket record with same values
 database.saveresult result=database.insert(ticket record);
System.assert(result.getErrors().size() > 0);
          System.assertEquals('You cannot create a duplicate Ticket.',
                            result.getErrors()[0].getMessage() );

}
}