+ Start a Discussion
Scott BradyScott Brady 

Need help with Apex Trigger Test Class

Hi Folks,

I require some assistance creating a test class for a trigger that I wrote. Any help would be greatly appreciated:
 
trigger SCHParseReceivedEmail on BMCServiceDesk__IncidentHistory__c (after insert){
    for (BMCServiceDesk__IncidentHistory__c action : trigger.new){    

            if(action.BMCServiceDesk__FKIncident__c != null){
                BMCServiceDesk__Incident__c ticket = [SELECT id, Customer_Reference_Number__c,BMCServiceDesk__Incident__c.Relationship_Name__c FROM BMCServiceDesk__Incident__c WHERE Id = :action.BMCServiceDesk__FKIncident__c];
                
                if(action.BMCServiceDesk__actionId__c == 'Email Received') {
                                    if(ticket.Relationship_Name__c == 'Random Customer Name') {
                        ticket.Customer_Reference_Number__c = action.BMCServiceDesk__description__c.substringBetween('##');
                            update Ticket;                
                }
           }    
    }

  }
}

Here are some required linkages between the objects being tested.

Incident History relies on an Incident to exist.  It receives emails and creates an incident history record.  I believe all I need to do in my test class is create an incident and an incident history record linked and verify that my trigger updated with the correct chracters located in between the ## delimiters in my substring.  I am having some issues getting a test class created and i think that I am probably overthinking things...

Here is what I have for my test class - currently getting an error Error: Compile Error: unexpected token: 'static' at line 16 column 5
 
@isTest(seeAllData=false)
Private class SCHParseReceivedEmailTest {

    public static List<Contact> contactsList;
    public static List<BMCServiceDesk__Incident__c> ticketsList;
    public static List<Account> accountsList;
    public static List<BMCServiceDesk__IncidentHistory__c> historyList;
    
    static void init(){
        contactsList = new List<Contact>();
        ticketsList = new List<BMCServiceDesk__Incident__c>();
        accountsList = new List<Account>();
        historyList = new List<BMCServiceDesk__IncidentHistory__c>();
        
    // Test Initiation
    static testMethod void testSCHParsingUpdater() {
    init();
    Test.startTest();
        
    // Create a test account, with test contact, as a requirement to create a new ticket (required fields)
    accountsList.add(new Account(
        Name = 'Random Customer Name',
        Type = 'Customer - Direct',
        Support_Level__c = 'Managed Services'
        ));
            Insert accountsList;
            
    contactsList.add(new Contact(
        LastName = 'SCH Testing',
        Phone = '5555555555',
        Email = 'test@testing.com',
        Account = [Select Id from Account where Name = 'Random Customer Name'].Id
        ));
            Insert contactsList;
    
    // Create a test ticket to be the updated record of the trigger
    ticketsList.add(new BMCServiceDesk__Incident__c(
        BMCServiceDesk__FKContact__c = [Select Id from Contact where LastName = 'SCH Testing'].Id,
        BMCServiceDesk__FKImpact__c = [Select Id from BMCServiceDesk__Impact__c where Name = '1-Enterprise-wide'].Id,   
        BMCServiceDesk__Urgency__c = [Select Id from BMCServiceDesk__Urgency__c where Name = '4 LOW'].Id,
        Customer_Reference_Number__c = 'SCH Testing',
        BMCServiceDesk__FKCategory__c = [Select Id from BMCServiceDesk__Category__c where Name = 'Request'].Id,
        BMCServiceDesk__shortDescription__c = 'Testing for SCH Parsing Updates'
        ));
            Insert ticketsList;
            
    // Create an Incident History record to be parsed by the ticket
    historyList.add(new BMCServiceDesk__IncidentHistory__c(
        BMCServiceDesk__FKIncident__c = [Select Id from BMCServiceDesk__Incident__c where Customer_Reference_Number__c = 'SCH testing'].Id,
        BMCServiceDesk__FKAction__c = [Select Id from BMCServiceDesk__Action__c where Name = 'Email Received'].Id,
        BMCServiceDesk__description__c = 'Email Received-SCH Internal Ticket Number##CHG0763071## Request Type##SCH Firewall Requests##',
        BMCServiceDesk__note__c = 'Email Received-SCH Internal Ticket Number##CHG0763071## Request Type##SCH Firewall Requests## (Ref:IN:01090698)'
        ));
            Insert historyList;
            }
            
    // Verify the trigger completed the customer reference field update
    ticketsList = [
        SELECT Id, Customer_reference_number__c
        FROM BMCServiceDesk__Incident__c
        WHERE Id = :ticketsList[0].Id
        ];
        
    // Verification
    System.asserEquals(ticketsList[0].Customer_reference_number__c, 'CHG0763071');
    
    Test.stopTest();
    }
}

 
Scott BradyScott Brady
updated test class (i had some typos)..

i am now getting a weird error when i try to save it, however.

An internal server error has occurred
An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience. 

Thank you again for your patience and assistance. And thanks for using salesforce.com! 

Error ID: 1382155508-370134 (310774775)
 
@isTest(seeAllData=false)
Private class SCHParseReceivedEmailTest {

    public static List<Contact> contactsList;
    public static List<BMCServiceDesk__Incident__c> ticketsList;
    public static List<Account> accountsList;
    public static List<BMCServiceDesk__IncidentHistory__c> historyList;
    
    static void init() {
        contactsList = new List<Contact>();
        ticketsList = new List<BMCServiceDesk__Incident__c>();
        accountsList = new List<Account>();
        historyList = new List<BMCServiceDesk__IncidentHistory__c>();
        }
    // Test Initiation
    static  testMethod void testSCHParsingUpdater() {
    init();
    Test.startTest();
        
    // Create a test account, with test contact, as a requirement to create a new ticket (required fields)
    accountsList.add(new Account(
        Name = 'Stanford Childrens Health',
        Type = 'Customer - Direct',
        Support_Level__c = 'Managed Services'
        ));
            Insert accountsList;
            
    contactsList.add(new Contact(
        LastName = 'SCH Testing',
        Phone = '5555555555',
        Email = 'test@testing.com',
        Account = [Select Id from Account where Name = 'Stanford Childrens Health']
        ));
            Insert contactsList;
    
    // Create a test ticket to be the updated record of the trigger
    ticketsList.add(new BMCServiceDesk__Incident__c(
        BMCServiceDesk__FKContact__c = [Select Id from Contact where LastName = 'SCH Testing'].Id,
        BMCServiceDesk__FKImpact__c = [Select Id from BMCServiceDesk__Impact__c where Name = '1-Enterprise-wide'].Id,   
        BMCServiceDesk__FKUrgency__c = [Select Id from BMCServiceDesk__Urgency__c where Name = '4 LOW'].Id,
        Customer_Reference_Number__c = 'SCH Testing',
        BMCServiceDesk__FKCategory__c = [Select Id from BMCServiceDesk__Category__c where Name = 'Request'].Id,
        BMCServiceDesk__shortDescription__c = 'Testing for SCH Parsing Updates'
        ));
            Insert ticketsList;
            
    // Create an Incident History record to be parsed by the ticket
    historyList.add(new BMCServiceDesk__IncidentHistory__c(
        BMCServiceDesk__FKIncident__c = [Select Id from BMCServiceDesk__Incident__c where Customer_Reference_Number__c = 'SCH testing'].Id,
        BMCServiceDesk__FKAction__c = [Select Id from BMCServiceDesk__Action__c where Name = 'Email Received'].Id,
        BMCServiceDesk__description__c = 'Email Received-SCH Internal Ticket Number##CHG0763071## Request Type##SCH Firewall Requests##',
        BMCServiceDesk__note__c = 'Email Received-SCH Internal Ticket Number##CHG0763071## Request Type##SCH Firewall Requests## (Ref:IN:01090698)'
        ));
            Insert historyList;
            
    // Verify the trigger completed the customer reference field update
    ticketsList = [
        SELECT Id, Customer_reference_number__c
        FROM BMCServiceDesk__Incident__c
        WHERE Id = :ticketsList[0].Id
        ];
        
    // Verification
    System.assertEquals(ticketsList[0].Customer_reference_number__c, 'CHG0763071');
    
    Test.stopTest();
    }
}