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
charleskocharlesko 

Quick question about before/after update trigger

This is my first time writing a trigger with 'before update'. It simply doesn't get triggered. The test method returns success, but in real world, it's like the trigger isn't there at all.

 

This contact trigger is supposed to change a value of a custom field in case whenever it is updated and has EmailBouncedDate not null. The purpose of this trigger is to alert the case viewer that this contact's email address is invalid. The code I wrote is below.

 

I tried slipping in some other random things outside of the if statement just to see if the trigger runs at all, and it doesn't seem like it is.

 

Isn't a trigger supposed to run everytime any value in the sObject is changed?

 

 

trigger BouncedEmailHandler on Contact (before update) {

for (Contact updatedContact : trigger.new) {

if(updatedContact.EmailBouncedDate != null){
for (Case c : [SELECT Id, Need_Attention__c, Status, Invalid_Contact_Email__c FROM Case WHERE Case.contactId =: updatedContact.Id]) {

System.debug('Trigger executed');
//c.Status = 'Invalid Contact Email';
c.Need_Attention__c = true;
c.Invalid_Contact_Email__c = true; update c;

}
}

//test code - let's see if this is triggered at all
Contact[] cc = [SELECT Id, Phone FROM Contact WHERE Contact.Id =: updatedContact.Id LIMIT 1];
cc[0].phone = '111111111'; update cc;


}

}

 

 

 

Message Edited by charlesko on 11-04-2009 10:34 AM
Best Answer chosen by Admin (Salesforce Developers) 
aalbertaalbert

Yes, a trigger does execute when the object changed (in this case, a Contact is updated).

I recommend adding more System.debug messages in the trigger (output the EmailBounceDate, for example). 

My guess is the trigger is executing, but the logic and the data aren't aligned to execute as you expect. 

 

 

All Answers

aalbertaalbert

Yes, a trigger does execute when the object changed (in this case, a Contact is updated).

I recommend adding more System.debug messages in the trigger (output the EmailBounceDate, for example). 

My guess is the trigger is executing, but the logic and the data aren't aligned to execute as you expect. 

 

 

This was selected as the best answer
charleskocharlesko

I doubt the logic is wrong because the test method returns a success. If you look at my code at the end, as a test, it also just changes the value of the phone number field. Even that's not working, so I'm assuming that the trigger itself is not running at all.

 

Here's what I did to test. 

 

I just went to a contact that I know has a bouncedemaildate not null, and editted a random field, then hit save. If the trigger ran, it should also change the phone number field bypassing the if statement. However there is no change to the contact and no change to the case that is connected to this contact... :(

aalbertaalbert

Try putting in a System.debug statement before the for loop even starts. Something like System.debug('trigger executing...') just to prove its running. Then I recommend adding System.debug statements throughout to see how the logic is being executed and evaluated.

 

 

charleskocharlesko

Thanks a lot for replying very quickly :)

 

I put system.debug() all over, and they are executing fine when I run the test method. Is there a way to see output of system.debug() even when I'm using it for real?

aalbertaalbert

Yeah, either open up the System Log (link in the upper right hand corner of the browser when logged into salesforce.com application) or enable Debug Logs (Setup | Administration Setup | Monitoring | Debug Logs).

 

 

charleskocharlesko

Thanks. There is no APEX script running when I change any value in contact.

 

You know what? I think I know what the problem is! Maybe it's because the field I'm editing is actually a column from Account sObject because I just figured out all the contacts are connected to repective person accounts.

 

I'll try modifying my trigger and let you know if it is solved.

 

Again, thanks a lot for help!

charleskocharlesko
Thanks. The trigger runs fine after I modified it and make it a person account trigger.