0

I am very new to Apex, and I'm trying to pick up as much as I can. I've worked off some code and written some of my own to eventually send out an email when an attachment is added to a case. I was able to do this without issue, but now I'm trying to have it automatically send the email to the owner of the case. Getting that email field is where I'm having my issue. I've looked through countless similar questions, but all the answers point to using a For loop, which I am, and I'm referencing the right type - I think.

Here is the code in question, thank you for any help. I'm hoping to learn from my mistakes, so an answer with an explanation of what I'm doing wrong would be much appreciated!

trigger Send_email on Attachment (after insert) {

//Creates a list to hold IDs 
List<Id> caseIds=new List<Id>();

//Grabs the id of the case the attachment is added to and puts it in aforementioned list 
for (Attachment att : trigger.new)
{
   caseIds.add(att.parentId);
}
Map<Id, Case> Ca=new Map<Id, Case>();

Ca.putAll([select id, Subject from Case where id IN :caseIds]);
//runs the trigger that grabs the ID 
Attachment attach = trigger.new[0];

list<string> OwnerList = New List<string>(); 
List<case> Case_Owner = new List<case>();
 for(id i: caseIds){
     Id CaseID = i.id;
     Case_Owner.add(CaseId);
 } 
 For(case c: Case_Owner){
     string Owner = c.OwnerId;
     OwnerList.add(Owner);
 }

//creates a mail variable to hold the information for an email 
Messaging.SingleEmailMessage mail = new    Messaging.SingleEmailMessage();

 String[] toAddresses = new String[] {OwnerList[0]};



mail.setToAddresses(toAddresses);

The error is "Initial term of expression must be a concrete SObject: Id"

The error is on line 20 - "ID CaseID = i.id;"

3

In your For loop you are looping over a list of IDs so each iteration of i is an id.

Simply change to:

Id CaseID = i;

Or to be succinct:

Case_Owner.add(i);

Similarly for the next for loop:

OwnerList.add(c.ownerId);

Or to completely make it simple:

Map<ID,case Case_Owner> caseMap = new Map<ID,Case>([select id,ownerId, Subject from Case where id IN :caseIds]);

Then get the case from the map and the ownerId. You can simplify your entire code. However, the setToAddress requires a list of email addresses not ownerIds

| improve this answer | |
  • ... and maybe use a clearer name than i e.g. id. – Keith C Aug 19 '16 at 12:56
  • Thank you! I will try this as soon as I can and update the post accordingly. I recognize my code is sloppy, but I chalk that up to inexperience, I just want to make sure it works first, then I can clean it up. – IcarusHubris Aug 19 '16 at 13:18
1

These lines lines should be rewritten:

Map<Id, Case> Ca=new Map<Id, Case>();
Ca.putAll([select id, Subject from Case where id IN :caseIds]);

and should look like the following:

Map<Id, Case> Ca=new Map<Id, Case>([select id, Subject from Case where id IN :caseIds]);

When you do the following, you're making the assumption there's only 1 record in trigger.new since you're not inside of a for loop at the point in your trigger. In doing so, your trigger isn't bulkified.

//runs the trigger that grabs the ID 
Attachment attach = trigger.new[0];

You didn't tell us for certain which line was line 20, but it appears it's in the following section:

 for(id i: caseIds){
     Id CaseID = i.id;
     Case_Owner.add(CaseId);
 } 
 For(case c: Case_Owner){
     string Owner = c.OwnerId;
     OwnerList.add(Owner);
 }

Case_Owner isn't an object, it's an Id. You should be iterating on the values in your map that you created above.

| improve this answer | |
0

instead of this

list<string> OwnerList = New List<string>();
List<case> Case_Owner = new List<case>();
 for(id i: caseIds){
     Id CaseID = i.id;
     Case_Owner.add(CaseId);
 } 
 For(case c: Case_Owner){
     string Owner = c.OwnerId;
     OwnerList.add(Owner);
 }

replace with this you made one line of operation to multiple lines, with unnecessary code and collections.

list<string> OwnerList = New List<string>();
    For(Case c: Ca.values()){

             OwnerList.add(c.OwnerId);
         }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.