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
Sunil_sfcd803Sunil_sfcd803 

Can any one help me in writing test class for my below trigger.

Hi All,

Please help me to write test calss for below trigger.Also it will be helpful if you add basic information as comments for understanding.

trigger UpdateTicketDetails on Ticket_Details__c (before insert,before update) {

    if(Trigger.isInsert){
    
        Id ticketRecordType = Schema.SObjectType.Ticket_Details__c.getRecordTypeInfosByName().get('Service').getRecordTypeId();
      //list<Ticket_Details__c> ticketList = new list<Ticket_Details__c> ();
        for(Ticket_Details__c ticket:Trigger.new){        
            if(ticket.recordTypeId==ticketRecordType){            
                ticket.Incident__c=False;
                ticket.Ticket_Description__c='Ticket is Service Request';
            }else{            
             ticket.Incident__c=True;
             ticket.Ticket_Description__c='Ticket is Incident';    
            }            
            //ticketList.add(ticket);
        }
       // insert ticketList;
    }
    
    if(Trigger.isUpdate){      
       for(Ticket_Details__c ticket:Trigger.new){
            String oldStatus= Trigger.oldMap.get(ticket.Id).Ticket_status__c;
            
          If(ticket.Ticket_status__c!=oldStatus){
              system.debug('Status has been changed');
              ticket.Ticket_Summary__c='Status changed from ' + oldStatus +' to '+ ticket.Ticket_status__c+'.';
          }        
       }       
    }
}
Best Answer chosen by Sunil_sfcd803
Rahul.MishraRahul.Mishra
Hi Sunil,

Below is the sample test class, I could not give you the exact one since I am not aware about mandatory fields and field api name in your org on given object, you just update my test class  and use that.
 
@isTest
private class CLASS_NAME {
    
	@isTest(SeeAllData=true)
	private static testMethod void testTrigger() {
      Id ticketRecordTypeService = Schema.SObjectType.Ticket_Details__c.getRecordTypeInfosByName().get('Service').getRecordTypeId();
	  Id ticketRecordTypeSales = Schema.SObjectType.Ticket_Details__c.getRecordTypeInfosByName().get('Sales').getRecordTypeId();
	  Ticket_Details__c  td1 = new Ticket_Details__c(Name = 'td1', RecordType__c = ticketRecordTypeService);
	  insert td1;
	  
	  
	  Ticket_Details__c  td2 = new Ticket_Details__c(Name = 'td2', RecordType__c = ticketRecordTypeSales, Ticket_status__c = 'some valid value');
	  insert td2;
	  
	  
	  td2.Ticket_status__c = 'Another Valid Value';
	  update td2;
	}
	

}
Update the field API name, picklist field value and record type name to query.

Mark solved if it does help you.


 

All Answers

Rahul.MishraRahul.Mishra
Hi Sunil,

Below is the sample test class, I could not give you the exact one since I am not aware about mandatory fields and field api name in your org on given object, you just update my test class  and use that.
 
@isTest
private class CLASS_NAME {
    
	@isTest(SeeAllData=true)
	private static testMethod void testTrigger() {
      Id ticketRecordTypeService = Schema.SObjectType.Ticket_Details__c.getRecordTypeInfosByName().get('Service').getRecordTypeId();
	  Id ticketRecordTypeSales = Schema.SObjectType.Ticket_Details__c.getRecordTypeInfosByName().get('Sales').getRecordTypeId();
	  Ticket_Details__c  td1 = new Ticket_Details__c(Name = 'td1', RecordType__c = ticketRecordTypeService);
	  insert td1;
	  
	  
	  Ticket_Details__c  td2 = new Ticket_Details__c(Name = 'td2', RecordType__c = ticketRecordTypeSales, Ticket_status__c = 'some valid value');
	  insert td2;
	  
	  
	  td2.Ticket_status__c = 'Another Valid Value';
	  update td2;
	}
	

}
Update the field API name, picklist field value and record type name to query.

Mark solved if it does help you.


 
This was selected as the best answer
Sunil_sfcd803Sunil_sfcd803
Hi Rahul,

Thanks for your help.With few modifications it was successfull.