Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
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+'.';

          }        

       }       

    }

}
2 answers
  1. Jun 27, 2018, 7:09 AM
    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.

     
0/9000