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
jaredbakerjaredbaker 

Problems with Email Trigger-Error: Initial term of field expression must be a concrete SObject

I'm trying to write code to have the system send an email to a user when an administrator has added a comment to their activities.  I want the trigger to only fire when an update has been made to this field.  I am consistently getting the above error, but I assume there will be others when I resolve it.  Please help!

 

trigger adminCommentEmail on Task (after update) {
    List<Task> t = [SELECT Id, owner.Id, owner.Email, Subject,
                    ActivityDate, Administrative_Comments_Questions__c
                    FROM Task t];
    if(t.Administrative_Comments_Questions__c != PriorValue(t.Administrative_Comments_Questions__c))
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {t.subject};  //error here
        mail.setToAddresses(toAddresses);
        mail.setSubject('Administrator Comment on Your Activity');
        mail.setPlainTextBody('A comment has been added to your activity.  To view it, please click the following link: https://cs10.salesforce.com/'
                          + t.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Ran it through debugger, here is the completed code:

 

Trigger adminCommentEmail on Task (after update) {
  
Set<id> oID = new Set<id>();

for (Task t : trigger.new)
    oID.add(t.OwnerID);

map<id,User> oEmail = New Map<id,User>([Select Email From User Where ID in :oID]);
  
    for(Task oTask : trigger.new){

    if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {oEmail.get(oTask.OwnerID).Email};  //error here
        mail.setToAddresses(toAddresses);
        mail.setSubject('Administrator Comment on Your Activity');
        mail.setPlainTextBody('A comment has been added to your activity.  To view it, please click the following link: https://cs10.salesforce.com/'
                          + oTask.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    }
}

 This compiled for me, now lets see if it does what you need.

All Answers

Starz26Starz26

Since t is a list you need to refer to the specific index of t in order to access it (i.e. t[1] or t[100]). Also, since the records are already present in the trigger (unless you are getting other records) you do not need to requery.

 

The best way to do this though is to use for to loop through the list:

 

Trigger adminCommentEmail on Task (after update) { for(Task oTask : trigger.new){ if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {oTask.subject}; //error here mail.setToAddresses(toAddresses); mail.setSubject('Administrator Comment on Your Activity'); mail.setPlainTextBody('A comment has been added to your activity. To view it, please click the following link: https://cs10.salesforce.com/' + oTask.Id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } }

 

jaredbakerjaredbaker

You rock!  Looks like that's going to work...but I'm now having another issue.  I'm getting the following error when I try to save the record with the below code activated (very minor changes).  I can see the owner has an email address, so I don't know why it's coming up as a null value.  Ideas?

 

INVALID_EMAIL_ADDRESS, Invalid to address : null: []: Trigger.adminCommentEmail: line 13, column 9

 

Trigger adminCommentEmail on Task (after update) {
    
    for(Task oTask : trigger.new){

    if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {oTask.owner.email};  //error here
        mail.setToAddresses(toAddresses);
        mail.setSubject('Administrator Comment on Your Activity');
        mail.setPlainTextBody('A comment has been added to your activity.  To view it, please click the following link: https://cs10.salesforce.com/'
                          + oTask.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    }
}

Starz26Starz26

For that I believe you will have to do a query...

 

Try this:

 

Trigger adminCommentEmail on Task (after update) { Set<id> oID = new Set<id>; for (Task t : trigger.new) oID.add(t.Owner); map<id,Email> oEmail = [Select Email From User Where ID in :oID]; for(Task oTask : trigger.new){ if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {oEmail.get(oTask.Owner)}; //error here mail.setToAddresses(toAddresses); mail.setSubject('Administrator Comment on Your Activity'); mail.setPlainTextBody('A comment has been added to your activity. To view it, please click the following link: https://cs10.salesforce.com/' + oTask.Id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } }

 

jaredbakerjaredbaker

Complier error on line 3: Set<id> oID = new Set<id>;

Error: Compile Error: unexpected token: ';' at line 3 column 25

 

On a side note, you are officially my favorite!  Thank you so much for your help!!!

Starz26Starz26

opps, change to

 

Set<id> oID = new Set<id>();

jaredbakerjaredbaker

 map<id,Email> oEmail = [Select Email From User Where ID in :oID];

 

Error: Compile Error: Invalid type: Email at line 8 column 8

 

No worries.  Basic debugging; it just happens to be a little above my head at the moment.  I'll be pouring over this code to make sense of it once I've got it up and running!

Starz26Starz26

Ahh, try:

 

Trigger adminCommentEmail on Task (after update) { Set<id> oID = new Set<id>(); for (Task t : trigger.new) oID.add(t.Owner); map<id,String> oEmail = [Select Email From User Where ID in :oID]; for(Task oTask : trigger.new){ if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {oEmail.get(oTask.Owner)}; //error here mail.setToAddresses(toAddresses); mail.setSubject('Administrator Comment on Your Activity'); mail.setPlainTextBody('A comment has been added to your activity. To view it, please click the following link: https://cs10.salesforce.com/' + oTask.Id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } }

 OR

 

Trigger adminCommentEmail on Task (after update) { Set<id> oID = new Set<id>(); for (Task t : trigger.new) oID.add(t.Owner); map<id,User> oEmail = [Select Email From User Where ID in :oID]; for(Task oTask : trigger.new){ if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {oEmail.get(oTask.Owner).Email}; //error here mail.setToAddresses(toAddresses); mail.setSubject('Administrator Comment on Your Activity'); mail.setPlainTextBody('A comment has been added to your activity. To view it, please click the following link: https://cs10.salesforce.com/' + oTask.Id); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } } }

 

jaredbakerjaredbaker

Both are giving me the same error for this line: oID.add(t.Owner);

 

Error: Compile Error: Incompatible element type SOBJECT:Name for collection of Id at line 6 column 5

Starz26Starz26

I hate trying to do this from memory with out IDE, sorry about the issues...

 

Change Owner to OwnerID

Starz26Starz26

Ran it through debugger, here is the completed code:

 

Trigger adminCommentEmail on Task (after update) {
  
Set<id> oID = new Set<id>();

for (Task t : trigger.new)
    oID.add(t.OwnerID);

map<id,User> oEmail = New Map<id,User>([Select Email From User Where ID in :oID]);
  
    for(Task oTask : trigger.new){

    if(oTask.Administrative_Comments_Questions__c != trigger.oldMap.get(oTask.ID).Administrative_Comments_Questions__c)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {oEmail.get(oTask.OwnerID).Email};  //error here
        mail.setToAddresses(toAddresses);
        mail.setSubject('Administrator Comment on Your Activity');
        mail.setPlainTextBody('A comment has been added to your activity.  To view it, please click the following link: https://cs10.salesforce.com/'
                          + oTask.Id);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
    }
}

 This compiled for me, now lets see if it does what you need.

This was selected as the best answer
jaredbakerjaredbaker

You are amazing!!!  Thank you so much for all your help.  I really appreciate you taking time out of your day to help me find a solution.  I'll still need to tweak it to make it perfect, but now that I've got opperational code, I can start doing that!