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
cathy369cathy369 

Test trigger that uses custom object with one record to get order number

I've got a very simple trigger that retrieves a unique order number from a custom object.  The reason for the trigger is the Order Number is used by 2 different objecsts.  Below is the trigger and associated class... As you can see, the object only has one record named "ORDER", which is how I retrieve it.  It works fine in test, but I'm not sure how to write my test class.  If I create a record in the object, then it will have 2 'ORDER' records.
I'm kind of at a loss.
Any help would be greatly appreciated...Thanks much.
trigger getOrdNum on Opportunity (before insert) {

    for (Opportunity o : Trigger.new){
       o.Test_SF_Ord_Num__c = orderNumber.getOrderNumber();
    }
}

________________________________________
Class:

public class orderNumber {

   public static decimal getOrderNumber(){
  
      List<Order_Number__c> ListOrderNum = [SELECT Order_Num__c FROM Order_Number__c WHERE Name = 'ORDER'];
   
      if( ListorderNum.size() > 0 )
      {
        ListorderNum[0].Order_Num__c += 1;
        update ListOrderNum[0];
        return ListOrderNum[0].Order_Num__c;
      }  
      return 0;
   }
}
Jasper WallJasper Wall
Hi Cathy,
The records created in testmethod are stored in testdata not in your organization.
testmethods do not have access to your organization data unless you specify @isTest(SeeAllData=true) in your test class,
try the below code.
 
@isTest
public class getOrdNumTest{
    public static testmethod void test1(){
        Opportunity opp1=new Opportunity();
        opp1.Name='ORDER';
        insert opp1;
    }
}

Mark it as the best answer if it helps,

Thanks,
Balayesu