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
Jan Kopejtko 2Jan Kopejtko 2 

Simple trigger is not doing what I want it to be

trigger TestTrigger on TestObj2__c (after insert) {
    Set<Id> ids = new Set<Id>();
    
    for(TestObj2__c a: Trigger.New) {
        ids.add(a.Account__c);
    }
    Account[] acs = [select Id from Account where Id in :ids];
    for(Account b :acs) {
        b.TestField__c = 'BECOMING A PROGRAMMER';
    }

}

Expectation: After I create a record of object TestObj2__c, a field of it's master object Account will get populated with "BECOMING A PROGRAMMER".

It does not do that though. Even wrote a test method for this, says the same:
 
@isTest
public class TestTestTrigger {
    @isTest static void td(){
        Account a = new Account(Name = 'fun');
        insert a;
        TestObj2__c b = new TestObj2__c();
        b.Name = 'Fun';
        b.Account__c = a.Id;
        insert b;
        
        Account isPopulated = [select TestField__c from Account where id = :b.Account__c];
        System.assertEquals('BECOMING A PROGRAMMER', isPopulated.TestField__c);
    
    }

}
Error Message	System.AssertException: Assertion Failed: Expected: BECOMING A PROGRAMMER, Actual: test
Stack Trace	Class.TestTestTrigger.td: line 12, column 1


Can anyone tell me what's missing in the puzzle?
 
Best Answer chosen by Jan Kopejtko 2
SarvaniSarvani
Hi Jan,

Did you miss @ in line 1 Shouldnt it be @isTest. Please try with below code:

Also before you test your test class. Try testing with sample record and see if the account record is updating its TestField__c with correct value.

A best practice is to bulkify your code insetad of calling DML statements for each commit.
 
trigger TestTrigger on  TestObj2__c(after insert) { 
   Set<Id> ids = new Set<Id>(); 
    list<Account> Accupdatelist=new list<Account>();
   for(TestObj2__c a: Trigger.New) { 
      ids.add(a.Account__c); 
   } 
   Account[] acs = [select Id from Account where Id in :ids]; 
   for(Account b :acs) { 
      b.TestField__c = 'BECOMING A PROGRAMMER'; 
           Accupdatelist.add(b);
   } 

    if(Accupdatelist.size()>0){
        update Accupdatelist;
       // system.debug(Accupdatelist);
    }
   
}

Test Class:
 
@isTest
public class TestTestTrigger {
    @isTest static void td(){
        Account a = new Account(Name = 'fun');
        insert a;
        TestObj2__c b = new TestObj2__c();
                b.Name= 'fun';
                b.Account__c = a.Id;
        insert b;
        
        Account isPopulated = [select TestField__c from Account where id = :b.Account__c];
        System.assertEquals('BECOMING A PROGRAMMER', isPopulated.TestField__c);
    
    }

}

Hope this helps! Please mark as best answer if it does.

Thanks

All Answers

MPS DevMPS Dev
Your trigger is after insert, you'll need a DML statement to commit the updated record(s). 
trigger TestTrigger on TestObj2__c (after insert) { 
   Set<Id> ids = new Set<Id>(); 
   for(TestObj2__c a: Trigger.New) { 
      ids.add(a.Account__c); 
   } 
   Account[] acs = [select Id from Account where Id in :ids]; 
   for(Account b :acs) { 
      b.TestField__c = 'BECOMING A PROGRAMMER'; 
   } 
   update acs;
}
Jan Kopejtko 2Jan Kopejtko 2
Thanks for the reply. I changed the trigger as you said and I still get the error on this test method:
 
isTest
public class TestTestTrigger {
    @isTest static void td(){
        Account a = new Account(Name = 'fun');
        insert a;
        TestObj2__c b = new TestObj2__c();
        b.Name = 'Fun';
        b.Account__c = a.Id;
        insert b;
        
        Account isPopulated = [select TestField__c from Account where id = :b.Account__c];
        System.assertEquals('BECOMING A PROGRAMMER', isPopulated.TestField__c);
    
    }

}

Error Message	System.AssertException: Assertion Failed: Expected: BECOMING A PROGRAMMER, Actual: test
Stack Trace	Class.TestTestTrigger.td: line 12, column 1


 
SarvaniSarvani
Hi Jan,

Did you miss @ in line 1 Shouldnt it be @isTest. Please try with below code:

Also before you test your test class. Try testing with sample record and see if the account record is updating its TestField__c with correct value.

A best practice is to bulkify your code insetad of calling DML statements for each commit.
 
trigger TestTrigger on  TestObj2__c(after insert) { 
   Set<Id> ids = new Set<Id>(); 
    list<Account> Accupdatelist=new list<Account>();
   for(TestObj2__c a: Trigger.New) { 
      ids.add(a.Account__c); 
   } 
   Account[] acs = [select Id from Account where Id in :ids]; 
   for(Account b :acs) { 
      b.TestField__c = 'BECOMING A PROGRAMMER'; 
           Accupdatelist.add(b);
   } 

    if(Accupdatelist.size()>0){
        update Accupdatelist;
       // system.debug(Accupdatelist);
    }
   
}

Test Class:
 
@isTest
public class TestTestTrigger {
    @isTest static void td(){
        Account a = new Account(Name = 'fun');
        insert a;
        TestObj2__c b = new TestObj2__c();
                b.Name= 'fun';
                b.Account__c = a.Id;
        insert b;
        
        Account isPopulated = [select TestField__c from Account where id = :b.Account__c];
        System.assertEquals('BECOMING A PROGRAMMER', isPopulated.TestField__c);
    
    }

}

Hope this helps! Please mark as best answer if it does.

Thanks
This was selected as the best answer
myford benefitsmyford benefits
Thanks for the answer.But Still i am getting error.myfordbenefits (https://myfordbenefits.xyz/)
Jan Kopejtko 2Jan Kopejtko 2
Sarvani: Thank you. Please ignore the above user. It is a spam.

I still have to be missing something because changed the trigger the way you sent me.

I can not test on a sample record if it is working becuase I am on my developer account and it seems that the trigger code needs to reach 75% coverage before it starts working on it's own.

But I am still recieving this error mesage (I added @isTest on line 1):
 
Error Message	System.AssertException: Assertion Failed: Expected: BECOMING A PROGRAMMER, Actual: test
Stack Trace	Class.TestTestTrigger.td: line 12, column 1

 
Jan Kopejtko 2Jan Kopejtko 2
I finally found out what the problem was! I had another trigger on object Account that was before update and it seems that it always updated the field to value "test". Thanks alot everyone.