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
Bryan GordonBryan Gordon 

trigger sending email isn't showing trigger values

I have a trigger that fires off when a field called "FIMS_FundID__c" is updated.  code looks like this:


trigger Z_TDF_TaskOnUpdate on Task (after update) {
    if(trigger.isUpdate){
        for(Task t : Trigger.new){
            Task oldTask = Trigger.oldMap.get(t.Id);
            if ( t.FIMS_FundID__c != oldTask.FIMS_FundID__C)
            {
              String userEmail = 'bgordon@dallasfoundation.org';
              Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
              String[] tmp = new String[]{'bgordon@dallasfoundation.org'} ;
              mail.setToAddresses(tmp); 
              mail.setInReplyTo('noreply@dallasfoundation.org');
              mail.setSenderDisplayName('Salesforce CRM Notification');
              mail.setSubject('ID CHANGED for record ' + t.FIMS_ID__c ); 
              String body = 'The Fund ID changed on activity record # ' + t.FIMS_ID__c + ' from ' + oldTask.FIMS_ID__c + ' to ' + t.FIMS_ID__c + '.'; 
              mail.setPlainTextBody(body); 
              Messaging.sendEmail(new Messaging.SingleEMailMessage[]{mail});
            }
        }
    }    
}


When the field is updated, the email is sent, but the values are "null"

So the subject reads: ID CHANGED for record null
and the body of the email reads:  The Fund ID changed on activity record # null from null to null.

does anyone know why the null values appear?  If I use "t.Id" or oldTask.Id, they show up in the email correctly, but none of the other fields do.

 
Best Answer chosen by Bryan Gordon
pconpcon
It sounds as if your FIMS_ID_c field is null at the time of the trigger firing?  Is that being set by another trigger?  If so, this may be the reason.  There is no order of operation for the triggers (other than before / after), and this trigger may be firing afterwards.  Also I see that you are comparing FIMS_FundID__c but using FMIS_ID__c in your email body, is that intentional?

What is the field type of FMIS_ID__c? Is it a formula?

All Answers

pconpcon
It sounds as if your FIMS_ID_c field is null at the time of the trigger firing?  Is that being set by another trigger?  If so, this may be the reason.  There is no order of operation for the triggers (other than before / after), and this trigger may be firing afterwards.  Also I see that you are comparing FIMS_FundID__c but using FMIS_ID__c in your email body, is that intentional?

What is the field type of FMIS_ID__c? Is it a formula?
This was selected as the best answer
Bryan GordonBryan Gordon
the field is actually a drop-down list that has a default value (so I can't be null).   
Good catch on the fields in the email body being different.   

it turns out that we don't get this value until Salesforce syncs with out foundation mgmt software....which explains why it's null.   When the sync occured, the value populated.

I guess it helps to talk things out :D

Thanks
pconpcon
Glad you figured it out.  Just a note, just because a dropdown list has a default value, does not mean it cannot be null.  If the record is inserted via the dataloader or via apex, the field can be set to either blank or null and will be honored.

If you do not mind, please pick a "Best Answer" (even if it's your own respsonse) to remove this from the unsolved queue.