You need to sign in to do that
Don't have an account?
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 });
}
}
Ran it through debugger, here is the completed code:
This compiled for me, now lets see if it does what you need.
All Answers
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: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 });
}
}
}
For that I believe you will have to do a query...Try this: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!!!
opps, change to
Set<id> oID = new Set<id>();
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!
Ahh, try:ORBoth 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
I hate trying to do this from memory with out IDE, sorry about the issues...
Change Owner to OwnerID
Ran it through debugger, here is the completed code:
This compiled for me, now lets see if it does what you need.
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!