If you are using Service cloud and have Email-to-Case functionality implemented, then this blog is useful for you.
In Lightning, Salesforce didn't provide standard functionality of Previous and Next actions on Email Message Objects which exist in Salesforce Classic version.
Lets take scenario, there is complaint case from user which is raised 10 days back and your servicing Team leader want to check all email history of that case.
This is common scenario happens in every organizations. If Team Leader is in Classic he can check all email history using Previous and Next buttons shown below.
But what about Lightning ???
There is no such standard functionality in Lightning, MUCH NEEDED !!
I have implemented workaround for this using Customization's.
Copy and paste below codes.
1. Create Apex Class: EmailMessageActions
public class EmailMessageActions {
public Id prevId {get;set;}
public Id nextId {get;set;}
public Id caseId {get;set;}
public Boolean isLex {get;set;}
public String prevURL {get;set;}
public String nextURL {get;set;}
public String emailMsgListURL {get;set;}
public EmailMessage eMsg;
public String statusMsg {get;set;}
public EmailMessageActions(ApexPages.StandardController stdController) {
this.eMsg = (EmailMessage)stdController.getRecord();
statusMsg = null;
getPreviousNextEMsgIds();
}
public PageReference previousMsg(){
//statusMsg = 'Loading...';
System.debug('In previousMsg');
if(prevId!=null){
PageReference page = new PageReference(prevURL);
page.setRedirect(true);
return page;
}
else
statusMsg = 'No More Previous Emails.';
return null;
}
public PageReference nextMsg(){
System.debug('In nextMsg');
if(nextId!=null){
PageReference page = new PageReference(nextURL);
page.setRedirect(true);
return page;
}
else
statusMsg = 'No More Next Emails.';
return null;
}
public PageReference emailMsgList(){
System.debug('In emailMsgList');
PageReference page = new PageReference(emailMsgListURL);
page.setRedirect(true);
return page;
}
public void getPreviousNextEMsgIds() {
System.debug('eMsg--'+eMsg.Id);
for(EmailMessage obj: [SELECT ParentId FROM EmailMessage WHERE Id =:eMsg.Id])
caseId = obj.ParentId;
System.debug('caseId--'+caseId);
List<Id> lstId = new List<Id>();
for(EmailMessage obj: [SELECT Id FROM EmailMessage WHERE ParentId =:caseId ORDER BY MessageDate ASC])
lstId.add(obj.Id);
System.debug('lstId--'+lstId);
Integer result = lstId.indexOf(String.valueOf(eMsg.Id));
System.debug('result--'+result);
if(result > 0){
prevId = lstId.get(result-1);
if(result < lstId.size()-1)
nextId = lstId.get(result+1);
}
else if(result == 0 && lstId.size() > 1)
nextId = lstId.get(result+1);
String uiThemeDisplayed = UserInfo.getUiThemeDisplayed();
if(uiThemeDisplayed.equalsIgnoreCase('Theme4d'))
isLex = true;
else
isLex = false;
System.debug('isLex--'+isLex);
String sfdcBaseURL = URL.getSalesforceBaseUrl().toExternalForm();
if(isLex){
if(prevId!=null)
prevURL = sfdcBaseURL+'/lightning/r/EmailMessage/'+prevId+'/view';
if(nextId!=null)
nextURL = sfdcBaseURL+'/lightning/r/EmailMessage/'+nextId+'/view';
emailMsgListURL = sfdcBaseURL+'/lightning/r/'+caseId+'/related/EmailMessages/view';
}
else{
if(prevId!=null)
prevURL = sfdcBaseURL+'/'+prevId;
if(nextId!=null)
nextURL = sfdcBaseURL+'/'+nextId;
emailMsgListURL = sfdcBaseURL+'/ui/email/EmailMessageListPage?id='+caseId;
}
System.debug('prevURL--'+prevURL);
}
}
2. Create VisualForce Pages: PreviousEmailMessage
<apex:page standardController="EmailMessage" extensions="EmailMessageActions" action="{!previousMsg}" showHeader="false" showQuickActionVfHeader="false">
<h1 style="font-size:1.125rem;">{!statusMsg}</h1>
<base target="_parent" />
</apex:page>
3. Create VisualForce Pages: NextEmailMessage
<apex:page standardController="EmailMessage" extensions="EmailMessageActions" action="{!nextMsg}" showHeader="false" showQuickActionVfHeader="false">
<h1 style="font-size:1.125rem;">{!statusMsg}</h1>
<base target="_parent" />
</apex:page>
4. Create VisualForce Pages: EmailMessageList
<apex:page standardController="EmailMessage" extensions="EmailMessageActions" action="{!emailMsgList}" showHeader="false" showQuickActionVfHeader="false">
<base target="_parent" />
</apex:page>
5. Create 3 Action Buttons for Email Message Object like Below
6. Give Profile Permissions to above Apex Class and VisualForce Pages.
7. Add these actions into Email Message Lightning Page Layout.
Thats It.
Enjoy! If you have any questions, comments etc. please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.
Subscribe blog for More updates !!!
Read More: WhatsApp on Salesforce Integration in 5 Steps
Which Salesforce API Do I Use in Integration?
When To Use Salesforce REST API
When To Use Salesforce SOAP API
When To Use Salesforce Bulk API
Latest Salesforce Interview Questions and Answers:
I am getting no more previous message. same for no more new message. the message list works and gives me the list of email messages. Can you help what needs to be fixed in the code u provided
ReplyDeleteHi,
DeleteThank you for sharing your feedback.
I have upadted apex class "EmailMessageActions" code. Use the above latest code and let em know if you are still facing same issue.
Enjoy! If you have any questions, comments etc. please feel free to let me know.
The new code is working as expected ... this is great and thank you for your time :)
DeleteI used your code in my sandbox and was able to create the three buttons with the code provided by you. I see the buttons too however the previous and next button does not move me to the next or previous email. it keeps giving me the message "No More Previous emails" when i click the previous button and "No more Next Emails " when i click the next butoon. I do have 10 emails in the list the list button does list out all the emails.
ReplyDeleteThe same happening with me as well . I have created the buttons, but it is not working, even after having many email messages
DeleteHi Athul,
DeleteThank you for sharing your feedback.
I have upadted apex class "EmailMessageActions" code. Use the above latest code and let em know if you are still facing same issue.
Enjoy! If you have any questions, comments etc. please feel free to let me know.
Hi, great solution.
ReplyDeleteis there no need for a test controller ?