trigger OpportunityTrigger on Opportunity (before insert) {

     for(Opportunity thisOpportunity : Trigger.New) {

     thisOpportunity.New_Value__c ='Actual Trigger Value';


This is my test class

@isTest private class OpportunityTriggerHandler_Test {

    @isTest static void test_OpportunityTriggerHandler() {

        Contact ordwayContact =  New Contact();
        ordwayContact.LastName = 'abjsb';
        ordwayContact.MailingStreet = 'Indore';
        insert ordwayContact;
          Opportunity testOpportunity = new Opportunity(
            StageName = 'Prospecting',
            CloseDate = system.today(),
            BillingContact__c   = ordwayContact.Id,
            Name = 'Test Opportunity Triggers'
        insert testOpportunity;



I am not getting the thisOpportunity.New_Value__c in my test class. When I insert the record through UI I am avail to see this value but when I do this through test class not getting the value in the test class

1 Answer 1


You have to query back the opportunity in order to see the value of the field. Since the value you are showing on the debug shows the value contained in the variable testOpportunity and not the value of the opportunity stored in memory(which was modified by the trigger).

add a line after the update to update the value of the variable with the value stored in memory.

testOpportunity = [SELECT id , New_Value__c FROM Opportunity WHERE id =: testOpportunity.id];

And it should show the correct value.

Bonus1: Anyway I suggest you aswell to change your System.debug to a System.assert since now your test is not making any testing. By using assert you make sure that the test is accomplishing what you intent to.

Something like

System.assertEquals('Actual Trigger Value',testOpportunity.New_value__c);

Bonus 2:

Since hardcoding that string is not the most clean option i would suggest aswell to move that value to a constant on your trigger and either make it public or private with @testvisible. And then reference it on the assert.

  • Thanks for your help Feb 6, 2020 at 8:54
  • If it helped you to solve your problem, please mark the answer as accepted. If you still have any problems related to the question , add the details and I'll be happy to help. Feb 6, 2020 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.