0

From below code i am going to send an email with modified records. But i need to send an email with both old values and new values.

Here is my code:

public static void onAfterUpdate_updatedAnswers(Map<Id, Question_Answer__c> lNewQAs, Map<Id, Question_Answer__c> mOldQAs){
    set<id> AccountList = new set<id>();
    Map<id,Question_Answer__c> answrsUpdateMap_updated = new Map<id,Question_Answer__c>();       
    Map<id,List<Question_Answer__c>> updatedAnswer_Account = new Map<id,List<Question_Answer__c>>();
    List<Account> ModifiedQuestionsList = New  List<Account>();

    for(id  ChangedQuestions : mOldQAs.Keyset()){
        system.debug('========1=========');
        if(mOldQAs.get(ChangedQuestions).Answer__c != lNewQAs.get(ChangedQuestions).Answer__c){
           answrsUpdateMap_updated.put(ChangedQuestions, lNewQAs.get(ChangedQuestions));   
            system.debug('=======2=========='+ answrsUpdateMap_updated.size());
        }
    }
    if(!answrsUpdateMap_updated.isEmpty()){
        for(id Qid : answrsUpdateMap_updated.keySet()){
            AccountList.add(answrsUpdateMap_updated.get(Qid).Account__c);    
             system.debug('========3========='+ AccountList.size());
    }
    if(!AccountList.isEmpty()){
                    ModifiedQuestionsList = [SELECT Id, Name, (SELECT Id, Name, Account__c, Answer__c,Question__c FROM Questions_Answers__r where id IN :answrsUpdateMap_updated.keySet()) FROM Account Where ID IN : AccountList limit 1];         

    if(!ModifiedQuestionsList.isEmpty()){
        for(Account Acc : ModifiedQuestionsList ){

            updatedAnswer_Account.put(Acc.id, Acc.Questions_Answers__r);                
        }

        system.debug('=========4========'+ updatedAnswer_Account.size());
          }
        }
      }
     if(!updatedAnswer_Account.isEmpty())                        
        genericSendanEmailClass.sendEmailToAuthor(updatedAnswer_Account);
   }

sending email:

 public class genericSendanEmailClass {
public static void sendEmailToAuthor(Map<id,List<Question_Answer__c>> sendingMap){
    for(id aid : sendingMap.keySet()){
        // Step 1: Create a new Email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSubject('Quesitons updated');
        string Subject =  '<br/>';
        system.debug('+++++++'+ sendingMap.keySet().size());
        for(Question_Answer__c bk : sendingMap.get(aid)){

         system.debug('====aid==='+ aid);
            String Recordlink = URL.getSalesforceBaseUrl().toExternalForm()+'/'+bk.id;               
             Subject = Subject +'<br/>'+  Recordlink;
             Subject = Subject +'<br/>'+'Question: <br/>'+ bk.Question__c;
             Subject = Subject + '<br/>'+'Answer: <br/>'+ bk.Answer__c + '<br/>'+'<br/>'; 

        }       
         list<id> LegalTeamId = New list<id>();
        List<String> mailAddresses = new List<String>();
         list<GroupMember>  LegalTeam  = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId = '00GU0000002TUao'];
        for(GroupMember u : LegalTeam){
          LegalTeamId.add(u.UserOrGroupId);
        }
        List<user> Lteam = [Select email FROM user WHERE id IN : LegalTeamId];
        for(user Lt : Lteam){
          mailAddresses.add(Lt.email);               
        }
         system.debug('======='+ mailAddresses);

        String body = 'Dear Leagal <br/> Please review the FCPA changes for ' + Subject ;
        mail.setHtmlBody(body);
        mail.setToAddresses(mailAddresses);           
        Messaging.sendEmail(new Messaging.Email[] {mail});
    }   
}
}
  • I think because of this line, updatedAnswer_Account.put(Acc.id, Acc.Questions_Answers__r); you are getting all records – Santanu Boral Jun 29 '16 at 13:57
  • why you give limit 1 to account query. what error you are facing here? – Tushar Sharma Jun 29 '16 at 16:48
0

I would suggest to create a separate map which will contain all the attributes which you will try to display to the user.

Map<String, String> updatedAttributesMap = new Map<String, String>();
updatedAttributesMap.put('ACCOUT_NAME', <get the name from query>);
updatedAttributesMap.put('QUESTION',<get the question>);
updatedAttributesMap.put ('ANSWER',<get the answer>);
updatedAttributesMap.put('SUBJECT', <get the subject>);

updatedAnswer_Account.put(Acc.id, updatedAttributesMap); 

Change the signature as follows:

sendEmailToAuthor(Map<id,Map<String,String>> sendingMap){} 

And in that method extract the values from the map. I think that will be easy.

|improve this answer|||||

Your Answer

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