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
mahesh p 54mahesh p 54 

How to send a single mail with List of Strings as a merge field

/**
    * @description To send an Email when Additional Documents are added
    * @param valueSet   
    * @param oppId   
    * @param stageName   
    */
    @AuraEnabled
    public static void additionalDocumentsEmailIntimation(List<String> valueSet, Id oppId, String stageName){
        List<String> fileList = new List<String>();
        List<String> toAddress = new List<String> ();
        List<Verification_Documents__c> vd = new List<Verification_Documents__c> ([SELECT Id,Name,docType__c,Document_Name__c,Approved__c,Document_Master__r.Document_Name__c,Document_Master__r.DocumentType__c,Document_Master__r.User__c,Comment__c,
                                                                                   Document_Master__r.Stages__c,Opportunity__r.customer__r.email,Opportunity__r.OwnerId,Opportunity__r.Sales_Contractor__r.Id, 
                                                                                   Opportunity__r.Sales_Contractor__r.Email FROM Verification_Documents__c where Opportunity__r.Id = :oppId AND Document_master__r.Stages__c =: stageName AND Document_Master__r.DocumentType__c = 'Additional']);
        Map<String, Verification_Documents__c> mapDocNameVsVerificationDoc = new Map<String, Verification_Documents__c>();
        
        for(Verification_Documents__c vdc: vd){
            toAddress.clear();
            if(valueSet.contains(vdc.Document_Master__r.Document_Name__c) && vdc.Document_Master__r.User__c == 'Customer' && vdc.Document_Master__r.DocumentType__c == 'Additional'){
                system.debug('selectedVal'+valueSet.contains(vdc.Document_Master__r.Document_Name__c));
                System.debug('****For Customer ');
                toAddress.clear();
                toAddress.add(vdc.Opportunity__r.customer__r.Email);
                system.debug('vdc.Opportunity__r.customer__r.Email***'+vdc.Opportunity__r.customer__r.Email+'vdc.em'+vdc.Document_Name__c);
                System.debug('****In Email'+vdc.Opportunity__r.customer__r.Email);
                EmailTemplate emailTemp = [SELECT Id, Name, Body, HtmlValue FROM EmailTemplate where name='AdditionalDocumentsIntimation']; 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(toAddress);
                email.setSenderDisplayName('OakStar Team');
                if(vdc.Document_Master__r.User__c  == 'Customer'){
                    email.setTargetObjectId(vdc.Opportunity__r.customer__r.Id);
                }
                email.setWhatId(vdc.Id);
                email.setTemplateId(emailTemp.id); 
                system.debug('vdc'+email);
                if(email != null){                
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); 
                }
            }
            else if(valueSet.contains(vdc.Document_Master__r.Document_Name__c) && vdc.Document_Master__r.User__c == 'Contractor' && vdc.Document_Master__r.DocumentType__c == 'Additional'){
                system.debug('selectedVal'+valueSet.contains(vdc.Document_Master__r.Document_Name__c));
                System.debug('****for Contractor ');
                toAddress.add(vdc.Opportunity__r.Sales_Contractor__r.Email);
                system.debug('vdc.Opportunity__r.Sales_Contractor__r.Email***'+vdc.Opportunity__r.Sales_Contractor__r.Email+'vdc.em'+vdc.Document_Name__c);
                System.debug('****In Email'+vdc.Opportunity__r.Sales_Contractor__r.Email);
                EmailTemplate emailTemp = [SELECT Id, Name, Body, HtmlValue FROM EmailTemplate where name='AdditionalDocumentsIntimation']; 
                Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                email.setToAddresses(toAddress);
                email.setSenderDisplayName('OakStar Team');
                if(vdc.Document_Master__r.User__c  == 'Contractor'){
                    email.setTargetObjectId(vdc.Opportunity__r.Sales_Contractor__r.Id);
                }
                email.setWhatId(vdc.Id);
                email.setTemplateId(emailTemp.id); 
                system.debug('vdc'+email);
                if(email != null){                
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }); 
                }
            }
            //}
        }  
    }
I have a master-detail relationship between Verification Documents and Document Master custom objects,master-detail relationship between Verification Documents and Opportunity.What i am doing through my code is when ValueSet contains 2 values 2 emails are sent.If ValueSet Contains the previous 2 values and if an additional value is added it is sending email to the previous 2 values and new additional value.Is there any possibility of sending a single mail with all the List of values in the email to be sent through code.