+ Start a Discussion
DannyTKDannyTK 

Need assistance writing a test class

Hello Community, 

Have a simple trigger to update the owner of a custom object (Sales_Order__c) back to the createdby when status is updated to 'Sales Order Completion':


trigger SalesOrderUpdateOwner on Sales_Order__c (before Update)
{
for(Sales_Order__c sc : trigger.new)
{
Sales_Order__c oldrecord = trigger.oldMap.get(sc.id);
if(sc.Status__c == 'Sales Order Completion' )
  {     
   sc.OwnerId = sc.CreatedById ;
        }
}
}


I just cannot seem to be able to save a test class (i'm also not a developer, so that makes it more challenging to know what i'm doing).  Was trying to create a class where I Insert a new Sales_Order__c record, with the Status__c = 'Sales Order Submitted'.  Then I tried to add an update on the Status__c = 'Sales Order Completion' (which should trigger the code), but nothing takes (various error messages based on how i'm saving it).  Not sure if it's because the status is updated via Javascript button and i have to account for that somehow on the testclass (I get to the testclass where i can create the record, but cannot seem to be able to update the record).  Any assistance is appreciated...
Best Answer chosen by DannyTK
Vamsi KrishnaVamsi Krishna
Dan,

here is the trigger and test class... i removed the oldrecord line in the trigger as you are not using it..
also you might need to assign any required fields before inserting the sales order in the test class as i mentioned in the commented line

trigger SalesOrderUpdateOwner on Sales_Order__c (before Update){

for(Sales_Order__c sc : trigger.new){
  if(sc.Status__c == 'Sales Order Completion'){     
   sc.OwnerId = sc.CreatedById ;
  }
}

}
@isTest
private class TestSalesOrderUpdate {

    static testMethod void testTrigger() {
        Sales_Order__c so = new Sales_Order__c();
        //sc.Name = 'sales order'; // assign any other mandatory fields required to save Sales Order
        sc.Status__c = ''Sales Order Submitted';
        insert sc;

        sc.Status__c = 'Sales Order Completion';
        update sc;   

        system.assertEquals(sc.OwnerId, sc.createdById);

    }
}


All Answers

Vamsi KrishnaVamsi Krishna
Dan,

here is the trigger and test class... i removed the oldrecord line in the trigger as you are not using it..
also you might need to assign any required fields before inserting the sales order in the test class as i mentioned in the commented line

trigger SalesOrderUpdateOwner on Sales_Order__c (before Update){

for(Sales_Order__c sc : trigger.new){
  if(sc.Status__c == 'Sales Order Completion'){     
   sc.OwnerId = sc.CreatedById ;
  }
}

}
@isTest
private class TestSalesOrderUpdate {

    static testMethod void testTrigger() {
        Sales_Order__c so = new Sales_Order__c();
        //sc.Name = 'sales order'; // assign any other mandatory fields required to save Sales Order
        sc.Status__c = ''Sales Order Submitted';
        insert sc;

        sc.Status__c = 'Sales Order Completion';
        update sc;   

        system.assertEquals(sc.OwnerId, sc.createdById);

    }
}


This was selected as the best answer
DannyTKDannyTK
Thanks Vamsi,

thanks for cleaning up my trigger as well, works perfectly. 

another thumbs up sir.
-d