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
karol.freebergkarol.freeberg 

Apex trigger testing problem.

I have a very simple trigger to do an update to a field called CTS_Rep__c. It is on a custom object and the trigger updates the field from the account object. The code is below.
trigger TOR_Update_CTS_Rep on Trial_Order_Request__c (before update, before insert) {

    //This trigger updates the CTS_Rep__c field from the Account CTS_Rep__c Field.
    //The lookup to account is based on the TOR field called Consignee_Ship_To_Name__c.
    //Note: By using the "before" trigger to modify the Trigger.new objects, you don't need 
    // to explicitly perform an update DML statement. When the trigger ends, it will implicitly 
    // update the data as you have modified the values 

    For (Trial_Order_Request__c newTOR : Trigger.new)
    {
        Account acc = 
            [Select ID, CTS_Rep__c from Account Where ID =: newTOR.Consignee_Ship_To_Name__c Limit 1];
        newTOR.CTS_Rep__c = acc.CTS_Rep__c;
        System.debug('In Trigger new tor= ' + newTOR.CTS_Rep__c);              
    } //End for Loop
}  //End of trigger
The trigger works fine on inserts and updates but when I wrote my test and ran that I ran into some problems. I am not getting the CTS_Rep__c field filled in. The trigger does not seem to save the data. My test code is below.
@ISTest public class TOR_CTS_Rep_Test {

 testMethod private static void runtests(){
        //In order for this test to work the three users Finance, Test User, and SysAdmin
        //must be opened and saved in prod so agent will get those last modified!!!
        //Also, Finance user contact created must = true and marked as an inactive user. 
        //SysAdmin and Test User contact created field must = false and marked active.        
        
        //create users
        List<User> theusers = Create_Accts_Contacts_From_Users_Test.buildUsers();
        //Use user0 for the account owner
        User User0 = theusers[0];
     	//Use user2 for the CTS Rep
     	User User2 = theusers[2];
     
        //create Account
        Account testacct = NewObjFactory.buildTestAcct(0, 'ABC Company', User0.id);
     	testacct.CTS_Rep__c = user2.id;
     	System.debug('acct CTS Rep = ' + testacct.CTS_Rep__c);
        insert testacct;
     
  		String theTORID;
     Test.startTest();
        // Create the Trial Order Request to test the insert of a new TOR with CTS Rep
        Trial_Order_Request__c TOR = NewObjFactory.buildtestTOR(0, testacct.id);
     	insert TOR;
     	theTORID=TOR.id;
     Test.stopTest();
     
     System.debug('TOR CTS Rep = ' + TOR.CTS_Rep__c);
     	//check to see if cts rep was added
        Trial_Order_Request__c TOR1 = [Select id, name, CTS_Rep__c
                       from Trial_Order_Request__c where ID =: theTORID ];
     System.debug('TOR1                    = ' + TOR1.CTS_Rep__c);
     System.debug('TOR                     = ' + TOR.CTS_Rep__c);
     System.debug('TOR1 Name                    = ' + TOR1.Name);
     System.assertEquals(TOR1.CTS_Rep__c,TOR.CTS_Rep__c);
 } //End runtests
}  //End test
When I run debug does not show the CTS_Rep__c as having any data. Below is the debug log.
User-added image
As you can see by the debug log, the ID for the CTS_Rep__c  is null. It pulls the correct data in name field so I know on my select statement that I am getting the correct record but again the CTS_Rep__c is null. Can anyone explain to me what I am doing wrong and how to correct the problem. Thank you.
Best Answer chosen by karol.freeberg
sandeep@Salesforcesandeep@Salesforce
Hi Karol,

CTS_Rep__c is null for TOR not for TOR1 and similarly name is coming null because of TOR. It is happening because after inerting TOR you have queried on it and getting in form of TOR1 so you can find values in fields of TOR1 instance not in TOR. even TOR and TOR1 are same thing but in different -2 instance mode.

Thanks 
Sandeep Singhal
http://www.codespokes.com/

All Answers

karol.freebergkarol.freeberg
By the way, the test is failing because the system. assertEquals is not matching like it should.
sandeep@Salesforcesandeep@Salesforce
Hi Karol,

CTS_Rep__c is null for TOR not for TOR1 and similarly name is coming null because of TOR. It is happening because after inerting TOR you have queried on it and getting in form of TOR1 so you can find values in fields of TOR1 instance not in TOR. even TOR and TOR1 are same thing but in different -2 instance mode.

Thanks 
Sandeep Singhal
http://www.codespokes.com/
This was selected as the best answer