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
tgk1tgk1 

Deployment issue for Trigger & Test Class

Hi everyone- I need to deploy an apex trigger and it's corresponding test class into my production instance.  Here's the code for the trigger:

 

trigger USERBusinessUnit on Account (before insert, before update) {
     
       // create a set of all the unique ownerIds  
       Set<Id> ownerIds = new Set<Id>();  
       for (Account a : Trigger.new)  
           ownerIds.add(a.OwnerId);      
     
    // query for all the User records for the unique userIds in the records  
       // create a map for a lookup / hash table for the user info  
       Map<Id, User> owners = new Map<Id, User>([Select USER.Business_Unit__c from User Where Id in 
       :ownerIds]);     
     
       // iterate over the list of records being processed in the trigger and  
      // set the Owner's Business Unit before being inserted or updated 
      for (Account a : Trigger.new)  
           a.Business_Unit__c = owners.get(a.OwnerId).Business_Unit__c;   
    
  }

 

 

And here is the test class that compiles (and works):

@isTest

private class USERBusinessUnitTriggerTest {


   private static TestMethod void testUSERBusinessUnitMethod() {
   
   User u = [Select id from User where id =: Userinfo.getuserId()];
   u.Business_Unit__c = 'Test BU';
   update u;

   test.startTest();
   system.runAs(u) {
   Account a = new Account(Name = 'Test Account');
   //fill all mandatory field of account before insert if any
   
   insert a;
   system.assertEquals(u.Business_Unit__c , a.Business_Unit__c);
   }
   test.stopTest();
      }

   }

 

 

Both the user & account objects have the Business_Unit__c field  (picklist field with either a value of P or E ).  When i went to deploy the change set I got the following error:

 

API NameTypeLineColumnProblem

USERBusinessUnitTriggerTest.testUSERBusinessUnitMethod()Class184Failure Message: "System.AssertException: Assertion Failed: Expected: Test BU, Actual: null", Failure Stack Trace: "Class.USERBusinessUnitTriggerTest.testUSERBusinessUnitMethod: line 18, column 4 External entry point"



 

Can anybody please help?  Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

please change it to this

 

@isTest
private class USERBusinessUnitTriggerTest {


   private static TestMethod void testUSERBusinessUnitMethod() {
   
   User u = [Select id from User where id =: Userinfo.getuserId()];
   u.Business_Unit__c = 'Test BU';
   update u;

   test.startTest();
   system.runAs(u) {
   Account a = new Account(Name = 'Test Account');
   //fill all mandatory field of account before insert if any
   
   insert a;
   
   a = [Select Business_Unit__c from Account where id =: a.id];
   system.assertEquals(u.Business_Unit__c , a.Business_Unit__c);
   }
   test.stopTest();
   

   }
   }