Print this page

Trigger is not updating object

Knowledge Article Number 000187819
Description User is testing a before_insert_trigger in the force.com IDE. System.debug[] statements that appear inside the trigger indicate that object was modified by the trigger before Insert. However, System.debug[] statements that appear after and outside the trigger indicate that the object was not modified.

Here's the code:

Test Class Code 

@isTest (SeeAllData=true) 
private class testDebug{ 
static testMethod void testUpdateOwner(){ 

Bonds__c b = new Bonds__C(Name='Null BondId'); 
insert b; 
Trades__c t = new Trades__C(bond_id__c=10); 
System.debug('$$$$$$$Before Insert t.id'+t.id); 
System.debug('$$$$$$$Before Insert t.bond_id__c'+t.bond_id__c); 
System.debug('$$$$$$$Before Insert t.bond__c'+t.bond__c); 
insert t; 
System.debug('$$$$$$$After Insert t.id'+t.id); 
System.debug('$$$$$$$After Insert t.bond_id__c'+t.bond_id__c); 
System.debug('$$$$$$$After Insert t.bond__c'+t.bond__c); 



Trigger code 
trigger testDebugTrade on Trades__c (before insert, before update) { 

for(Trades__c t : Trigger.new){ 
Bonds__c b = [Select id from Bonds__c where bond_id__c=:t.bond_id__c]; 
t.bond__c = b.id; 
System.debug('$$$$$$In Trigger t.bond__c'+t.bond__c); 


Resolution Needs to run a query on the field that was modified in the trigger to see its updated value. 

updated Code: 
@isTest (SeeAllData=true) 
private class testDebug{ 
static testMethod void testUpdateOwner(){ 

Bonds__c b = new Bonds__C(Name='Null BondId'); 
insert b; 
Trades__c t = new Trades__C(bond_id__c=10); 
System.debug('$$$$$$$Before Insert t.id'+t.id); 
System.debug('$$$$$$$Before Insert t.bond_id__c'+t.bond_id__c); 
System.debug('$$$$$$$Before Insert t.bond__c'+t.bond__c); 

insert t; 
String test1 = String.valueOf(t.Id); 
Trades__c p = [Select id, bond__c from Trades__c where id=:test1]; 


System.debug('$$$$$$$After Insert t.id'+t.id); 
System.debug('$$$$$$$After Insert t.bond_id__c'+t.bond_id__c); 
System.debug('$$$$$$$After Insert t.bond__c'+t.bond__c); 




Trigger Code: trigger testDebugTrade on Trades__c (before insert, before update) { 

for(Trades__c t : Trigger.new){ 
Bonds__c b = [Select id from Bonds__c where bond_id__c=:t.bond_id__c]; 
t.bond__c = b.id; 
System.debug('$$$$$$In Trigger t.bond__c'+t.bond__c); 





promote demote