You need to sign in to do that
Don't have an account?

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?
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.
Test Class:
Hope this helps! Please mark as best answer if it does.
Thanks
All Answers
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.
Test Class:
Hope this helps! Please mark as best answer if it does.
Thanks
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):