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
Akshay KopardeAkshay Koparde 

Trigger on Account to update Description field

I have  written a practice trigger Which populates the Description feild on Account object If the Industry = Banking. From the UI it works good. Also get 100 Test coverage. I feel the trigger is not firing from Test class because my System.debug are not logging the fields I need. I would be very thankful if anyone can point out my mistake. I would like to add my System.assertEquals are failing. Please help me learn


/*Trigger*/

trigger UpdateDescriptionOnAccount on Account (before insert) {
    list<Account> ActList=new list<Account>();
    for(Account a :Trigger.new)
    {
        if(a.Industry=='Banking' && a.Description == Null)
        {
            a.Description='This is a Banking Account';
            ActList.add(a);
        }
       
    }
    //insert ActList;

}

This is my test Class
/*Test Class*/

@isTest
public class TestUpdateDescriptionOnAccount {
    static testmethod void TestUpdate()
    {
       Account a = New Account(Name='Test Account 1',Industry='Banking');
       insert a;
      System.debug('Account Inserted '+a.Name + 'Description ='+a.Description +a.Industry);
       //system.assertEquals('This is a Banking Account',a.Description);
       
        
       Account b = New Account(Name='Test Account 2',Industry='Agriculture');
       insert b;
       //System.assertEquals(Null, b.Description);
        
       list<Account>testAccount = new List<Account>();
        for(integer i=0;i<200;i++)
        {
            Account c = new Account(Name='Account '+i, Industry='Banking');
            testAccount.add(c);
        }
        test.startTest();
        	insert testAccount;
        test.stopTest();
      for(Account d : TestAccount)
       {
            //system.assertEquals('This is a Banking Account',d.Description);
           System.debug('Account Inserted '+d.Name + 'Description ='+d.Description);
       }
        
    }

}


 
Best Answer chosen by Akshay Koparde
Tanner RussellTanner Russell
1. You are not re querying the test account after applying the update so you are asserting on old results
after test.StopTest(); 
testAccount = [select id, name, description from account];
this will get you the new results after the update

All Answers

Tanner RussellTanner Russell
1. You are not re querying the test account after applying the update so you are asserting on old results
after test.StopTest(); 
testAccount = [select id, name, description from account];
this will get you the new results after the update
This was selected as the best answer
Akshay KopardeAkshay Koparde
Hi Tanner, Thank you for your quick reply. I really appreciate this. Your suggestion helped me to understand what I was doing wrong. I could solve Bulk operation in Test class. But still my single account insert fails. I notice my trigger wont fire for a single Account record insert from test class and it successfully fires during inserting record from UI. Please help.
Tanner RussellTanner Russell
What is the error message you get when you run the test?
Akshay KopardeAkshay Koparde
Hi Tanner Russel, When I try to " insert a " and try to assert I get assertion failed @ line number 10. If i comment line 10 and execute there is no such error. Then I used system.debug to see if the Trigger fired on insert a and updated Description field. But i noticed the description field was not updated after "insert a".
Tanner RussellTanner Russell
ohh yeah just add Account acc = [Select id, name, description from account where name = 'Test Account 1' limit 1];
System.AssertEquals('This is a Banking Account',acc.Description);
Tanner RussellTanner Russell
than after inserting b 
acc = [Select id, name, description from account where name = 'Test Account 2' limit 1];
System.AssertEquals(null,acc.Description);
Akshay KopardeAkshay Koparde
Hi Tanner Russel, Thanks your suggestion solved my problem. If you dont mind can you please explain why we need to query the inserted account. Why cant the field be populated by trigger? Thanks again.
Tanner RussellTanner Russell
Thats great!  The reason why we are querying on the inserted account is beacuse when account a is created it has no description and that is how it is stored in memory so a will stay like this until you change it. When you insert a the description gets added to the record but not the variable a, so all we have to do is find the record and set a or a new variable to it again so that it is the same as the record. 
Akshay KopardeAkshay Koparde
Hi Tanner Russel, Very thanks for your answers. Now I understand the importance of querying. I really appreciate your help. Cheers!