+ Start a Discussion
raman123raman123 

how can i use map in my below code where i hav to send mail changed owner of case

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
List<ID>ownerids=new List<ID>();

List<String> sendTo = new List<String>();
List<User>users=new List<User>();

for (case mycase : Trigger.new) {

    case oldcon = Trigger.oldMap.get(mycase.Id)
        
    
 if (mycase.ownerid != oldcon.ownerid ) {

     ownerids.add(oldcon.ownerid) ; 

     }
    }

   users=[select name,id,email from user where id in:ownerids];
    for(User u:users){

      sendTo.add(u.Email); 

    }

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSenderDisplayName('Email alert');
mail.setSubject('Owner change');
String body = 'Dear User Owner changed';
mail.setToAddresses(sendTo);
mail.setPlainTextBody(body);
mails.add(mail);
Messaging.sendEmail(mails);
Best Answer chosen by raman123
SFDC Prime SquadSFDC Prime Squad
Please try this and see if it works for you.

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
List<ID>ownerids=new List<ID>();
 
List<String> sendTo = new List<String>();
Map<Id, User> users = new Map<Id, User>();
 
Map<Id, Case> CaseMap = new Map<Id, Case>(Trigger.new);
 
for (case mycase : Trigger.new) {
    case oldcon = Trigger.oldMap.get(mycase.Id);
    if (mycase.ownerid != oldcon.ownerid ) {
        ownerids.add(oldcon.ownerid) ;
     }
}
 
if(!ownerids.isEmpty()){
   users = new Map<Id, User>([select name,id,email from user where id in:ownerids]);
    for(Case cs : CaseMap.values()){
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[]{(users.get(cs.OwnerId)).email });
        mail.setSenderDisplayName('Email alert');
        mail.setSubject('Owner change');
        String body = 'Dear User Owner changed';
        body += '<a href='+system.URL.getSalesforceBaseUrl().toExternalForm()+'/'+cs.Id+'>click here.</a> <br/><br/>';
        mail.setPlainTextBody(body);
        mails.add(mail);
 
    }
 
}
Messaging.sendEmail(mails);


Please mark this as the BEST ANSWER if it resolves the issue
 

All Answers

Ajvad AjuAjvad Aju
Hi yash, can you just clear your query.
Do you want to use map instead of list that you used in your code?
Where do you want to use map?
instead of this "List<String> sendTo = new List<String>();"?
farukh sk hdfarukh sk hd
Not able to understand from your query where you want to use map.
raman123raman123
i want to optimize code that if i change multiple cases owner the mail should go seperatly and also in that mail case link should be included
SFDC Prime SquadSFDC Prime Squad
Please try this and see if it works for you.

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
List<ID>ownerids=new List<ID>();
 
List<String> sendTo = new List<String>();
Map<Id, User> users = new Map<Id, User>();
 
Map<Id, Case> CaseMap = new Map<Id, Case>(Trigger.new);
 
for (case mycase : Trigger.new) {
    case oldcon = Trigger.oldMap.get(mycase.Id);
    if (mycase.ownerid != oldcon.ownerid ) {
        ownerids.add(oldcon.ownerid) ;
     }
}
 
if(!ownerids.isEmpty()){
   users = new Map<Id, User>([select name,id,email from user where id in:ownerids]);
    for(Case cs : CaseMap.values()){
       
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[]{(users.get(cs.OwnerId)).email });
        mail.setSenderDisplayName('Email alert');
        mail.setSubject('Owner change');
        String body = 'Dear User Owner changed';
        body += '<a href='+system.URL.getSalesforceBaseUrl().toExternalForm()+'/'+cs.Id+'>click here.</a> <br/><br/>';
        mail.setPlainTextBody(body);
        mails.add(mail);
 
    }
 
}
Messaging.sendEmail(mails);


Please mark this as the BEST ANSWER if it resolves the issue
 
This was selected as the best answer