0

I have a Email custom page and everything seems working fine but when we select an template the merge fields we use in the template are not populating as expected but {!Contact.Name} is working fine all the straight forward names are populating fine but {!Contact.OwnerFirstName} or Last name or FullName are not populating its with all the objects.(I am using the Custom email page for Accounts,leads,opp,contacts) So this is with classic email templates when I go and create a Lightning Template we need to use the Recipient and Sender's Tag in which I try to change the {{{ to !} and }}} to } I basically converted to HTML in my apex code. But in vain still if I use the Lightning template nothing is populating. Below are images and Code. Any one help Guys. Cant see any sender Name.

    public void rMailSFTemplate() {
    
    Boolean isTemplateVF = false;
    
    strSFTemplate = '';
    strSFTempSubject = '';
    strSFTempBody = '';
    
    strSFTemplate = apexpages.currentPage().getParameters().get('templateID');
    Map<String,String> mapSubjectBody = new Map<String,String>();
    
    Boolean isAccessible = RMailHelper.checkIfObjectAccesible('EmailTemplate');
    Boolean isFieldsAccissible = RMailHelper.checkIfFieldsAccessible('EmailTemplate','Id,Name,BrandTemplateId,TemplateType,OwnerId,RelatedEntityType,UiType,Description,Markup,EnhancedLetterheadId,Subject,Body,HtmlValue,IsActive,FolderName');
    
    if(strSFTemplate != null || strSFTemplate != ''){
        
        if(isAccessible && isFieldsAccissible) {
            
            for(EmailTemplate emailTemp : [Select Id,Name,BrandTemplateId,TemplateType,UiType,OwnerId,RelatedEntityType,Description,Markup,EnhancedLetterheadId,Subject,Body,HtmlValue,IsActive,FolderName  From EmailTemplate WHERE Id =: strSFTemplate AND IsActive = true]) {
                
                strTemplateEmailSF = emailTemp.Name;
                
                if(!String.isEmpty(emailTemp.Subject)){
                   strSFTempSubject = emailTemp.Subject; 
                }
                
                if(strSFTempSubject.contains('{{{')){
                    strSFTempSubject = strSFTempSubject.replaceAll('{{{', '{!');
                    strSFTempSubject = strSFTempSubject.replaceAll('}}}', '}');  
                }else{
                    
                    strSFTempSubject = emailTemp.Subject;
                }
                
                //Lightning email template attachment
                if(emailTemp.UiType=='SFX'){
                    Set<String> setContactDocID = new Set<String>();
                    
                    Integer intAttach = [SELECT count() FROM ContentDocumentLink WHERE LinkedEntityId =: strSFTemplate];
                    lstDocumentWrapper = orderSortDocument(lstDocumentWrapper);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      lstDocumentWrapper = orderSortDocument(lstDocumentWrapper);
                    if(intAttach > 0){
                        
                        for(ContentDocumentLink objCDL : [SELECT ContentDocumentId,Id,LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId =: strSFTemplate]){
                            setContactDocID.add(objCDL.ContentDocumentId);
                        }
                        
                        for(ContentVersion objCV : [SELECT ContentDocumentId,ContentSize,ContentUrl,Description,FileExtension,FileType,Id,
                                                    Title,VersionData 
                                                    FROM   ContentVersion
                                                    WHERE  ContentDocumentId =: setContactDocID]){
                                                        
                                                        if(objCV != null){
                                                            documentWrapper docWrapper = new documentWrapper();
                                                            docWrapper.strTempDocName = objCV.Title;
                                                            docWrapper.strDocName = objCV.Title; 
                                                            docWrapper.order = intOrder;
                                                            docWrapper.strConVer = objCV.VersionData;
                                                            docWrapper.strConVerId = objCV.id;
                                                            docWrapper.strDocSize = objCV.ContentSize;
                                                            docWrapper.strContentType = objCV.FileType;
                                                            docWrapper.isFromAttachment = false;
                                                            
                                                            if((totalDeskTopDocSize + docWrapper.strDocSize) < intMaxDocSize  ){
                                                                
                                                                totalDeskTopDocSize = totalDeskTopDocSize + docWrapper.strDocSize;
                                                                isMaxFileSize = false;
                                                                lstDocumentWrapper.add(docWrapper);   
                                                                setDocID.add(docWrapper.strConVerId);
                                                                intOrder++;
                                                            }else {
                                                                
                                                                isMaxFileSize = true;
                                                            }
                                                            
                                                        }
                                                    }
                    }
                }
                
                //Classic email template attachment
                if(emailTemp.UiType=='Aloha'){
                    
                    Integer intAttach = [SELECT count() FROM Attachment WHERE ParentId =: strSFTemplate];
                    lstDocumentWrapper = orderSortDocument(lstDocumentWrapper);
                    
                    if(intAttach > 0){
                        for(Attachment objSavedAttach : [SELECT Body,ContentType,BodyLength,Id,Name,ParentId FROM Attachment WHERE ParentId =: strSFTemplate]){
                            if(objSavedAttach != null){
                                documentWrapper docWrapper = new documentWrapper();
                                docWrapper.strTempDocName = objSavedAttach.Name;
                                docWrapper.strDocName = objSavedAttach.Name; 
                                docWrapper.order = intOrder;
                                docWrapper.strConVer = objSavedAttach.Body;
                                docWrapper.strConVerId = objSavedAttach.id;
                                docWrapper.strDocSize = objSavedAttach.BodyLength;
                                docWrapper.strContentType = objSavedAttach.ContentType;
                                docWrapper.isFromAttachment = false;
                                
                                if((totalDeskTopDocSize + docWrapper.strDocSize) < intMaxDocSize){
                                    
                                    totalDeskTopDocSize = totalDeskTopDocSize + docWrapper.strDocSize;
                                    isMaxFileSize = false;
                                    lstDocumentWrapper.add(docWrapper);
                                    setDocID.add(docWrapper.strConVerId);
                                    intOrder++;
                                }else {
                                    
                                    isMaxFileSize = true;
                                }
                            }
                        }
                        
                    }
                }
                
                if(!String.isEmpty(strSFTempSubject)){
                    mapSubjectBody.put('subject',strSFTempSubject);
                }else{
                    mapSubjectBody.put('subject','');
                }
                
                if(!String.isEmpty(emailTemp.Markup)) {
                    
                    isTemplateVF = true;
                    strSFTempBody = emailTemp.Markup;
                    
                    if(!String.isEmpty(strSFTempBody)){
                        strSFTempBody = strSFTempBody.replaceAll('\n', '<br/>').replaceAll('\r\n', '<br/>');
                        
                        mapSubjectBody.put('body',strSFTempBody);
                    }
                    
                } 
                else if(!String.isEmpty(emailTemp.HtmlValue) && emailTemp.TemplateType == 'custom' && emailTemp.UiType=='SFX'){
                    
                    strSFTempBody = emailTemp.HtmlValue ; 
                    if(strSFTempBody.contains('{{{')){
                        strSFTempBody = strSFTempBody.replace('{{{', '{!');
                        strSFTempBody = strSFTempBody.replace('}}}', '}');  
                    }
                    
                    mapSubjectBody.put('body',strSFTempBody);
                    
                } 
                else if(!String.isEmpty(emailTemp.HtmlValue) && emailTemp.TemplateType == 'custom' && emailTemp.UiType=='Aloha'){
                    
                    if(emailTemp.HtmlValue.contains(']]>')) {
                        emailTemp.HtmlValue = emailTemp.HtmlValue.replaceAll(']]>','');
                    }
                    
                    strSFTempBody = emailTemp.HtmlValue ; 
                    
                    if(strSFTempBody.contains('{{{')){
                        strSFTempBody = strSFTempBody.replaceAll('{{{', '{!');
                        strSFTempBody = strSFTempBody.replaceAll('}}}', '}');  
                    }
                    
                    if(!String.isEmpty(strSFTempBody)){
                        strSFTempBody = strSFTempBody.replaceAll('\n', '<br/>').replaceAll('\r\n', '<br/>');
                        mapSubjectBody.put('body',strSFTempBody); 
                    }
                    
                }
                else if(!String.isEmpty(emailTemp.HtmlValue) && emailTemp.TemplateType == 'html' && emailTemp.UiType=='Aloha'){
                    
                    
                    if(emailTemp.HtmlValue.contains(']]>')) {
                        emailTemp.HtmlValue = emailTemp.HtmlValue.replaceAll('<!\\[CDATA\\[', '');
                        emailTemp.HtmlValue = emailTemp.HtmlValue.replaceAll('\\]\\]>', '');
                    }
                    strSFTempBody = emailTemp.HtmlValue ; 
                    
                    if(strSFTempBody.contains('{{{')){
                        strSFTempBody = strSFTempBody.replaceAll('{{{', '{!');
                        strSFTempBody = strSFTempBody.replaceAll('}}}', '}');  
                    }
                    
                    mapSubjectBody.put('body',strSFTempBody); 
                    
                } 
                else {
                    
                    strSFTempBody = emailTemp.Body;
                    
                    if(!String.isEmpty(strSFTempBody)) {
                        if(strSFTempBody.contains('{{{')){
                            strSFTempBody = strSFTempBody.replaceAll('{{{', '{!');
                            strSFTempBody = strSFTempBody.replaceAll('}}}', '}');  
                        }
                        strSFTempBody = strSFTempBody.replaceAll( '\r\n', '<br/>');
                        strSFTempBody = strSFTempBody.replaceAll( '\n', '<br/>');
                        mapSubjectBody.put('body',strSFTempBody);
                    }
                    
                }
                if(!String.isEmpty(strSFTempBody)) {

                    if(isTemplateVF) {
                        
                        mapSubjectBody = RMailHelper.renderVfEmailTemplate(setObjID, strObjectGlbName, mapSubjectBody);
                        
                    }else {
                        
                        mapSubjectBody = RMailHelper.replaceTemplateExpression(setObjID, strObjectGlbName, mapSubjectBody);
                    }
                    
                }
                
                if(mapSubjectBody != null && !mapSubjectBody.isEmpty()){
                    
                    for(String strKey : mapSubjectBody.keyset()){
                        
                        if(strKey == 'subject') {
                            strSFTempSubject = mapSubjectBody.get(strKey);
                        }
                        else{
                            
                            strSFTempBody = mapSubjectBody.get(strKey);
                        }
                    }
                }
                
                //Replacing body 
                if(!String.isEmpty(strSFTempBody))
                {
                    
                    if(!String.isEmpty(strSFTempBody)){
                        
                        String strRegex = '\\{!(.*?)\\}';
                        
                        Pattern regexPattern = Pattern.compile(strRegex);
                        Matcher regexMatcher = regexPattern.matcher(strSFTempBody);
                        
                        if(regexMatcher.find()) {
                            strSFTempBody = strSFTempBody.replaceAll(strRegex, ' ');
                        }
                        
                    }
                }
                
                //Replacing Subject 
                if(!String.isEmpty(strSFTempSubject)) {
                    
                    if(!String.isEmpty(strSFTempSubject)){
                        
                        String strRegex = '\\{!(.*?)\\}';
                        
                        Pattern regexPattern = Pattern.compile(strRegex);
                        Matcher regexMatcher = regexPattern.matcher(strSFTempSubject);
                        
                        if(regexMatcher.find()) {
                            strSFTempSubject = strSFTempSubject.replaceAll(strRegex, ' ');
                        }
                    }
                }
                
                if(!String.isEmpty(strSFTempBody)){
                    
                    strSFTempBody = strSFTempBody + '\n\n' +strTempUserSign; 
                }
                else {
                    
                    strSFTempBody = strTempUserSign;
                }
                
                if(setEmailId.contains(emailTemp.Id) && !setEmailId.isEmpty()) {
                    
                    isRuleCheck = true;
                    lstDocumentWrapperFile.clear();
                    if(isRMailRuleApplied == false){
                        
                        strEncryptCheck = new List<String>();
                        strEsignCheck = new List<String>();
                        
                        isRuleActiveCheck();
                        isRMailRuleApplied = true;
                        
                    }
                }
                else{
                    
                    if(isRMailRuleApplied == true){
                        
                        strEncryptCheck = new List<String>();
                        strEsignCheck = new List<String>();
                        
                        isSettingPresent();
                        
                        isRMailRuleApplied = false;
                        
                        if(isRMailRuleApplied == false){
                            strEncryptCheck = new List<String>();
                            strEsignCheck = new List<String>();
                            strRulePolicy ='';
                        }
                    }
                }
            
                
            }
        }
    }
    else if(strSFTemplate == null || strSFTemplate == '' || strSFTemplate == 'Select Template'){
        
        strSFTempSubject = '';
        strSFTempBody = '';
    }
}

public List<documentWrapper> orderSortDocument(List<documentWrapper> lstDocWrapper){
    Integer intNewOrder =1;
    totalDeskTopDocSize = 0;
    List<documentWrapper> lstTempWrapper = new List<documentWrapper>();
    
    for(documentWrapper objDoc : lstDocWrapper){
        
        if(objDoc.isFromAttachment == true){
            
            if((totalDeskTopDocSize + objDoc.strDocSize) < intMaxDocSize){
                documentWrapper docWrapper = new documentWrapper();
                docWrapper.strTempDocName = objDoc.strTempDocName;
                docWrapper.strDocName = objDoc.strDocName; 
                docWrapper.order = intNewOrder;
                docWrapper.strConVer = objDoc.strConVer;
                docWrapper.strConVerId = objDoc.strConVerId;
                docWrapper.strDocSize = objDoc.strDocSize;
                docWrapper.isFromAttachment = true;
                totalDeskTopDocSize = totalDeskTopDocSize + docWrapper.strDocSize;
                lstTempWrapper.add(docWrapper);
                intNewOrder++;
                isMaxFileSize = false;
            }else{
                isMaxFileSize = true;
            }
        }
    }
    intOrder = intNewOrder ;
    return lstTempWrapper;
}

Result of the template when added

1
  • Welcome to Salesforce Stack Exchange. I was going to edit your question to better format the code, but I cannot tell whether you meant to add another image that is not showing. Please edit your question to make it easier to read by separating the code into code blocks. I often find it easier to explicitly set apart the code block by starting a new line with "```" (the three ticks without the double quotes), entering all the code - ensuring proper indentation, then another new line with the three ticks.
    – Moonpie
    Jun 3 at 13:29

Your Answer

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

Browse other questions tagged or ask your own question.