+ Start a Discussion
Steven CambersSteven Cambers 

Creating a test class for a trigger & change set - Help needed

I've made a trigger to automatically create a custom object "Qualification" record attached to the Opp. I recently update this to select the relevant Qualification Record Type based on Opp Record Type.

I'm trying to push the trigger to our production environment but i haven't ever created test classes before, i'm in the process of trying to learn but this has become a rush job to push the change set live now. Anyone able to help me out? Trigger code below:

trigger CreateQualification on Opportunity (after insert, before update) {

    List<Qualification__c> qualtoinsert = new List<Qualification__c>();

    for (Opportunity opp : Trigger.new) {

        // Create qualification record only when the Stage is Updated to "Attendance"
        if (opp.StageName == 'Attendance' && !opp.Qual_trigger__c ) {
            
            if (opp.RecordTypeId == '01220000000Mebg' || opp.RecordTypeId == '0120J000000B69P' || opp.RecordTypeId == '0122000000034yA'){
                
                Qualification__c qual = new Qualification__c();
                
                qual.RecordTypeId = '0127E00000070be';
                
                qual.Opportunity__c   = opp.id;           
                opp.Qual_Trigger__c = True;
                qualtoinsert.add(qual); // For Bulk processing of the Records.
                
            } //End if
            
            else {
                
                Qualification__c qual = new Qualification__c();
                
                qual.RecordTypeId = '0127E00000070bj';
                
                qual.Opportunity__c   = opp.id;           
                opp.Qual_Trigger__c = True;
                qualtoinsert.add(qual); // For Bulk processing of the Records.
                
            } //End else          
        } //End if
    } // End of For

    // Inserting the New Qualification Record.
    if ( !qualtoinsert.isEmpty()) {
        insert qualtoinsert;
    }
}

Many Thanks,
Steve.
Dinesh MultaniDinesh Multani
here is test class for your code
 
@isTest(SeeAllData=false)
    private class TestClass 
    {
         static testMethod void promoteCustomer_Addresses()
        {
            Account testAcct = new Account (Name = 'My Test Account');
            insert testAcct;
            
            // Creates first opportunity
            Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today()
                            
                            );
            
            insert oppt;

  }   
}




 
AmitdAmitd
Hi Steven,

Please try follwoing code

@isTest
    private class oppTest
    {
         static testMethod void create_Qualification()
        {
            Account acc= new Account (Name = 'Test Account');
            insert acc;
            //replace DeveloperNameOfRecordType with actual record type developername on opportunity as per record type Id used in your code
           Id RecordTypeIdContact = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('DeveloperNameOfRecordType').getRecordTypeId();
            Opportunity opp = new Opportunity(Name ='New mAWS Deal',
                            AccountID = acc.ID,
                            StageName = 'Attendance',Qual_trigger__c=false,
                            Amount = 1454,
                            RecrodTypeId=RecordTypeIdContact ;
                            );
            
            insert opp;

  }   
}