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
bstangerbstanger 

Problem assigning value to trigger.new[0].anyfield = somevalue

I'm new to SF & writing my first triggers/unit tests.  I'm using Eclipse & the syntax of my trigger is verified, but when I run the test class, I get

 

"System.Exception: Assertion Failed: Expected: 1234, Actual: null"

 

even though I specifically set it in the trigger (oli[0].Teacher_Var_Cost_Per_Hr__c = 1234;) 

 

Any help would be appreciated. 

 

trigger GetCurrentTeacherHrCosts on OpportunityLineItem (before insert, before update) {

 

OpportunityLineItem[] oli = Trigger.new;

 

oli[0].Teacher_Var_Cost_Per_Hr__c = 1234;

 

}

 

public class Test {

static testMethod void myTest(){

OpportunityLineItem oli = new OpportunityLineItem(OpportunityId='0068000000RJ4L8AAL', UnitPrice=9999.99, Quantity=99, PricebookEntryId='01u80000003LaQXAA0');

insert oli;

System.assertEquals(1234, oli.Teacher_Var_Cost_Per_Hr__c);

}}

Best Answer chosen by Admin (Salesforce Developers) 
David VPDavid VP

You're running the assert against the in-memory object. Triggers do their work deeper down at the database level.

 

After your insert of 'oli' and before your assertEquals, you need to query the value from the database and then verify if it got updated.

 

 

 

 

All Answers

David VPDavid VP

You're running the assert against the in-memory object. Triggers do their work deeper down at the database level.

 

After your insert of 'oli' and before your assertEquals, you need to query the value from the database and then verify if it got updated.

 

 

 

 

This was selected as the best answer
BritishBoyinDCBritishBoyinDC

So your test class would look more like this:

 

 

public class Test { static testMethod void myTest(){ OpportunityLineItem oli = new OpportunityLineItem(OpportunityId='0068000000RJ4L8AAL', UnitPrice=9999.99, Quantity=99, PricebookEntryId='01u80000003LaQXAA0'); insert oli; OpportunityLineItem testoli = [Select Id, Teacher_Var_Cost_Per_Hr__c from OpportunityLineItem where Id=:oli.Id limit 1]; System.assertEquals(1234, testoli.Teacher_Var_Cost_Per_Hr__c); }}

 

 

 

 

 

bstangerbstanger
Thanks for the feedback gentlemen.  I've got it going now based on your advice.