-
ChatterFeed
-
0Best Answers
-
0Likes Received
-
0Likes Given
-
5Questions
-
9Replies
My email to Feeditem code is failing for few Pdf files
I wrote a code to catch the email replies into case feeditem object. below is my code and its working fine for all the attachments but failing for few PDF files. could anyone please if am Missing anything here.
Trigger :
trigger emailToPostChatter on EmailMessage (After insert) {
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
if(email.FromAddress !='ssp-support-test@sureify.com'){
List<User> u = [select Id, Name, Email from user where Email =: email.FromAddress AND isActive = true];
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.createdById = u[0].Id;
post.Title = 'EmailMessageFeed';
string emailBody = email.TextBody;
post.Body = '\n' + emailBody;
insert post;
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id, u[0].Id, email.ParentId);
}
else{
emailToPostTochatterAlerts.fireExternalEmail(email.ParentId, post.Id);
emailToPostTochatterAlerts.fireInternalEmail(email.ParentId, post.Id);
}
}
}
}
===== > Helper class to handle attachments
public class emailToPostChatterHelper {
@future
Public static void InsertFeedAttachment(Id emailId, Id postId, Id userId, Id CaseId){
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: emailId];
List<ContentDocument> cntdList = new List<ContentDocument>();
List<ContentVersion> cntvList = new List<ContentVersion>();
List<FeedAttachment> FeedAtchlist = new List<FeedAttachment>();
set<Id> FeedId = new set<Id>();
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contvrsn = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
ContentDocument cntd = new ContentDocument();
cntd.Id = contD.ContentDocumentId;
cntd.OwnerId = userId;
cntdList.add(cntd);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = postId; //Id of FeedItem
feedAttachment.RecordId = contvrsn.Id;
system.debug('emailToPostChatterHelper feedAttachment.RecordId ='+feedAttachment.RecordId);
feedAttachment.Type = 'Content';
FeedAtchlist.add(feedAttachment);
}
insert FeedAtchlist;
update cntdList;
}
}
}
Trigger :
trigger emailToPostChatter on EmailMessage (After insert) {
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
if(email.FromAddress !='ssp-support-test@sureify.com'){
List<User> u = [select Id, Name, Email from user where Email =: email.FromAddress AND isActive = true];
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.createdById = u[0].Id;
post.Title = 'EmailMessageFeed';
string emailBody = email.TextBody;
post.Body = '\n' + emailBody;
insert post;
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id, u[0].Id, email.ParentId);
}
else{
emailToPostTochatterAlerts.fireExternalEmail(email.ParentId, post.Id);
emailToPostTochatterAlerts.fireInternalEmail(email.ParentId, post.Id);
}
}
}
}
===== > Helper class to handle attachments
public class emailToPostChatterHelper {
@future
Public static void InsertFeedAttachment(Id emailId, Id postId, Id userId, Id CaseId){
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: emailId];
List<ContentDocument> cntdList = new List<ContentDocument>();
List<ContentVersion> cntvList = new List<ContentVersion>();
List<FeedAttachment> FeedAtchlist = new List<FeedAttachment>();
set<Id> FeedId = new set<Id>();
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contvrsn = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
ContentDocument cntd = new ContentDocument();
cntd.Id = contD.ContentDocumentId;
cntd.OwnerId = userId;
cntdList.add(cntd);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = postId; //Id of FeedItem
feedAttachment.RecordId = contvrsn.Id;
system.debug('emailToPostChatterHelper feedAttachment.RecordId ='+feedAttachment.RecordId);
feedAttachment.Type = 'Content';
FeedAtchlist.add(feedAttachment);
}
insert FeedAtchlist;
update cntdList;
}
}
}
- Satish Kumar Lalam
- November 07, 2023
- Like
- 0
Custom email alerts are not working for Customer Community users
I have Created a custom email alert notification with below code. we send email alerts when ever a comment is made on feeditem of case. My code is working fine for all users except Customer Community users. It is throwing below error when the user try to comment on feed from Customer portel. Kindly advice on how to resolve this issue.
FeedItemJiraIntegration: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Class.emailAlertsOnCaseFeedItem.fireExternalEmail: line 69, column 1 Class.emailAlertsOnCaseFeedItem.fireEmailalerts: line 17, column 1 Trigger.FeedItemJiraIntegration: line 5, column 1
TRIGGER :
trigger FeedItemJiraIntegration on FeedItem (After insert) {
if(trigger.isinsert && trigger.isafter){
FeedItemTiggerHelper.afterInsert(trigger.newMap.keyset());
emailAlertsOnCaseFeedItem.fireEmailalerts(trigger.newMap.keyset());
}
}
Helper :
public without sharing class emailAlertsOnCaseFeedItem {
//@InvocableMethod(label='emailAlertsOnCaseFeedItem')
//public static void fireEmailalerts(List<Id> feedItemIds){
public static void fireEmailalerts(Set<Id> feedItemIds){
Set<Id> caseIds = new Set<Id>();
List<FeedItem> feedList = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedItemIds ];
Map<Id,FeedItem> caseFeedmap = new Map<Id,FeedItem>();
system.debug('feedList = '+feedList.size());
for(FeedItem fd1: feedList){
caseFeedmap.put(fd1.ParentId,[select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:fd1.Id]);
caseIds.add(fd1.ParentId);
system.debug('caseIds = '+caseIds);
if(fd1.Visibility == 'AllUsers'){
system.debug('fd1.Visibility = '+fd1.Visibility);
fireExternalEmail(fd1.ParentId, fd1.Id);
fireInternalEmail(fd1.ParentId, fd1.Id);
}else{
system.debug('fd1.Visibility = '+fd1.Visibility);
fireInternalEmail(fd1.ParentId, fd1.Id);
}
}
}
Public static Void fireExternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
string caseUrl = 'https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id;
List<Id> conIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('External conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails1 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(toEmails);
mymail.setBccAddresses(bccEmails);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('PlainText');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('RichText');
}
System.debug('Statement after external insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
Public static Void fireInternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
List<Id> conIds = new List<Id>();
List<Id> teamIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Group> lstPublicGroup = [SELECT Id FROM Group WHERE Name= 'Support Team' and Type = 'Regular' LIMIT 1];
if(lstPublicGroup != null && !lstPublicGroup.isEmpty()){
for(GroupMember objMember : [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE GroupId = :lstPublicGroup[0].Id]){
if(String.valueOf(objMember.UserOrGroupId).startsWith('005')){
System.debug('User Id : ' +objMember.UserOrGroupId);
teamIds.add(objMember.UserOrGroupId);
}
}
}
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('Internal conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails2 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(teamIds);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
System.debug('Statement after insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
}
FeedItemJiraIntegration: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Class.emailAlertsOnCaseFeedItem.fireExternalEmail: line 69, column 1 Class.emailAlertsOnCaseFeedItem.fireEmailalerts: line 17, column 1 Trigger.FeedItemJiraIntegration: line 5, column 1
TRIGGER :
trigger FeedItemJiraIntegration on FeedItem (After insert) {
if(trigger.isinsert && trigger.isafter){
FeedItemTiggerHelper.afterInsert(trigger.newMap.keyset());
emailAlertsOnCaseFeedItem.fireEmailalerts(trigger.newMap.keyset());
}
}
Helper :
public without sharing class emailAlertsOnCaseFeedItem {
//@InvocableMethod(label='emailAlertsOnCaseFeedItem')
//public static void fireEmailalerts(List<Id> feedItemIds){
public static void fireEmailalerts(Set<Id> feedItemIds){
Set<Id> caseIds = new Set<Id>();
List<FeedItem> feedList = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedItemIds ];
Map<Id,FeedItem> caseFeedmap = new Map<Id,FeedItem>();
system.debug('feedList = '+feedList.size());
for(FeedItem fd1: feedList){
caseFeedmap.put(fd1.ParentId,[select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:fd1.Id]);
caseIds.add(fd1.ParentId);
system.debug('caseIds = '+caseIds);
if(fd1.Visibility == 'AllUsers'){
system.debug('fd1.Visibility = '+fd1.Visibility);
fireExternalEmail(fd1.ParentId, fd1.Id);
fireInternalEmail(fd1.ParentId, fd1.Id);
}else{
system.debug('fd1.Visibility = '+fd1.Visibility);
fireInternalEmail(fd1.ParentId, fd1.Id);
}
}
}
Public static Void fireExternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
string caseUrl = 'https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id;
List<Id> conIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('External conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails1 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(toEmails);
mymail.setBccAddresses(bccEmails);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('PlainText');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('RichText');
}
System.debug('Statement after external insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
Public static Void fireInternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
List<Id> conIds = new List<Id>();
List<Id> teamIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Group> lstPublicGroup = [SELECT Id FROM Group WHERE Name= 'Support Team' and Type = 'Regular' LIMIT 1];
if(lstPublicGroup != null && !lstPublicGroup.isEmpty()){
for(GroupMember objMember : [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE GroupId = :lstPublicGroup[0].Id]){
if(String.valueOf(objMember.UserOrGroupId).startsWith('005')){
System.debug('User Id : ' +objMember.UserOrGroupId);
teamIds.add(objMember.UserOrGroupId);
}
}
}
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('Internal conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails2 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(teamIds);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
System.debug('Statement after insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
}
- Satish Kumar Lalam
- July 10, 2023
- Like
- 0
SOQL query returns no results Trigger but returns a row in Developer Console for ContentDocumentLink Object.
I have wrote a trigger on EmailMessage Object to get the attachments from email replies to case FeedItems. Below is my code Snippet. I am facing an issue with SOQL query on ContnentDocumentLink object. Where the the same query is working fine in Anonymous window but not in trigger. Could you please advise on this. Below is my Code Snippet. Kindly advice on this.
trigger emailToPostChatter on EmailMessage (After insert) {
List<FeedItem> itemList = new List<FeedItem>();
List<FeedItem> upItemList = new List<FeedItem>();
List<ContentVersion> conVersionList = new List<ContentVersion>();
List<FeedAttachment> atchList = new List<FeedAttachment>();
List<FeedAttachment> FeedAtchList = new List<FeedAttachment>();
List<ContentDocumentLink> contDocLinks = new List<ContentDocumentLink>();
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.Body = email.FromName + '\n' + email.TextBody;
system.debug('Body = '+post.Body);
insert post;
system.debug('EmailMessage Id= '+ email.Id);
system.debug('message.ContentDocumentIds = '+email.ContentDocumentIds);
system.debug('Post ID 1= '+ post.Id);
system.debug('message.HasAttachment = '+email.HasAttachment);
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
//emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id);
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: email.Id];
//List<ContentDocumentLink> contDocLinks2 = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :email.Id];
system.debug('contDocLinks = '+contDocLinks);
//system.debug('contDocLinks2 = '+contDocLinks2);
system.debug('LinkedEntityId = '+email.Id);
system.debug('contDocLinks Size = '+contDocLinks.size());
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contv = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
system.debug('contv ='+contv);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = post.Id; //Id of FeedItem
feedAttachment.RecordId = contv.Id;
system.debug('feedAttachment.RecordId ='+feedAttachment.RecordId);
//feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT';
insert feedAttachment;
}
}
}
}
}
trigger emailToPostChatter on EmailMessage (After insert) {
List<FeedItem> itemList = new List<FeedItem>();
List<FeedItem> upItemList = new List<FeedItem>();
List<ContentVersion> conVersionList = new List<ContentVersion>();
List<FeedAttachment> atchList = new List<FeedAttachment>();
List<FeedAttachment> FeedAtchList = new List<FeedAttachment>();
List<ContentDocumentLink> contDocLinks = new List<ContentDocumentLink>();
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.Body = email.FromName + '\n' + email.TextBody;
system.debug('Body = '+post.Body);
insert post;
system.debug('EmailMessage Id= '+ email.Id);
system.debug('message.ContentDocumentIds = '+email.ContentDocumentIds);
system.debug('Post ID 1= '+ post.Id);
system.debug('message.HasAttachment = '+email.HasAttachment);
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
//emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id);
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: email.Id];
//List<ContentDocumentLink> contDocLinks2 = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :email.Id];
system.debug('contDocLinks = '+contDocLinks);
//system.debug('contDocLinks2 = '+contDocLinks2);
system.debug('LinkedEntityId = '+email.Id);
system.debug('contDocLinks Size = '+contDocLinks.size());
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contv = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
system.debug('contv ='+contv);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = post.Id; //Id of FeedItem
feedAttachment.RecordId = contv.Id;
system.debug('feedAttachment.RecordId ='+feedAttachment.RecordId);
//feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT';
insert feedAttachment;
}
}
}
}
}
- Satish Kumar Lalam
- June 27, 2023
- Like
- 0
- Satish Kumar Lalam
- June 09, 2023
- Like
- 0
I am working on Integration from JIra to Salesforce and vice versa. Now, How do we built custom Rest API for getting issue comment body(Richtext/htmlbody) and comment attachments from jira issue to salesforce case Feeditem record?
I could able to implement the custom Rest API to receive the text only format form Jira issue comment to case feeditem record. which is working fine with the below code.
@RestResource(urlMapping='/api/webhooks/pushDetails/*')
global class jiraWebhookListner {
@HttpPost
global static Void receiveJiraIssueComments(){
system.debug('API Request--> ' +RestContext.request);
system.debug('API Request Body --> ' +RestContext.request.requestBody);
RestRequest request = RestContext.request;
Blob body = request.requestBody;
//string requestBody = request.requestBody.toString();
String bodyString = body.toString();
system.debug('String body = '+bodyString);
Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(bodyString);
system.debug('payload = ' +payload);
string commentText = (String)payload.get('comment');
string issueKey = (String)payload.get('issueKey');
system.debug('Data display = ' + (string)payload.get('Data'));
system.debug('commentText = ' + commentText);
system.debug('issueKey = ' + issueKey);
case caseObj = [Select Id from case WHERE SL_JIRA_Key__c = : issueKey LIMIT 1];
system.debug('case = ' + caseObj);
IF(caseObj != null){
FeedItem feedItemObj = new FeedItem();
feedItemObj.IsRichText = True;
feedItemObj.ParentId = caseObj.Id;
feedItemObj.Body = commentText;
insert feedItemObj;
RestContext.response.statuscode = 200;
RestContext.response.responseBody = Blob.valueOf('Comment added successfully');
} else{
RestContext.response.statuscode = 404;
RestContext.response.responseBody = Blob.valueOf('Case not found for issue key: ' + issueKey);
}
}
}
Now I would like to send RichText/Html body like text having BOLD, Italic style...etc. along with attachments. Kindly advice on how to acheive this.
Also, Below is the code that I can able to send only the text body of FeedItem of case object to Jira Issue comment.
@future(callout=true)
public static void createJiraComment(Set<Id> FeedItemIds){
FeedItemtinsert = true;
List<FeedItem> FeedCommentList = [select id, ParentId, Body, CreatedById from FeedItem where id IN: FeedItemIds AND CreatedById !='005S000000RstyT '];
If(FeedCommentList!=null){
Case PerentCase = [Select Id, SL_JIRA_Key__c from case where Id =: FeedCommentList[0].ParentId ];
User cmntUser = [Select Id, Name FROM User where Id =: FeedCommentList[0].CreatedById];
string Username = cmntUser.Name;
String endpointUrl='https://sureify.atlassian.net/rest/api/3/issue/'+PerentCase.SL_JIRA_Key__c+ '/comment';
If(FeedCommentList[0].Body!=null){
for(FeedItem FeedCommentRec : FeedCommentList){
string cmntbody = 'Commented by ' + cmntUser.Name +' : ' + FeedCommentRec.Body;
system.debug('FeedCommentRec.Body == ' + FeedCommentRec.Body);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
Blob headerValue = Blob.valueOf('jira-intg-user@sureify.com' + ':' + Label.Jira_Int_Token);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
String body='{"body":{"content":[{"content":[{"text": "'+cmntbody+'","type":"text"}],"type": "paragraph"}],"type": "doc","version": 1}}';
request.setBody(body);
HttpResponse response = http.send(request);
system.debug('----response--'+response);
system.debug('---response body--'+response.getBody());
if (response.getStatusCode() == 201){
system.debug('---couponResults--'+response.getStatusCode());
}
}
}
}
}
}
I would like to send RichText/Html FeedItem body like text having BOLD, Italic style...etc. along with attachments to JIra Issue comments. Kindly advice on how to acheive this.
@RestResource(urlMapping='/api/webhooks/pushDetails/*')
global class jiraWebhookListner {
@HttpPost
global static Void receiveJiraIssueComments(){
system.debug('API Request--> ' +RestContext.request);
system.debug('API Request Body --> ' +RestContext.request.requestBody);
RestRequest request = RestContext.request;
Blob body = request.requestBody;
//string requestBody = request.requestBody.toString();
String bodyString = body.toString();
system.debug('String body = '+bodyString);
Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(bodyString);
system.debug('payload = ' +payload);
string commentText = (String)payload.get('comment');
string issueKey = (String)payload.get('issueKey');
system.debug('Data display = ' + (string)payload.get('Data'));
system.debug('commentText = ' + commentText);
system.debug('issueKey = ' + issueKey);
case caseObj = [Select Id from case WHERE SL_JIRA_Key__c = : issueKey LIMIT 1];
system.debug('case = ' + caseObj);
IF(caseObj != null){
FeedItem feedItemObj = new FeedItem();
feedItemObj.IsRichText = True;
feedItemObj.ParentId = caseObj.Id;
feedItemObj.Body = commentText;
insert feedItemObj;
RestContext.response.statuscode = 200;
RestContext.response.responseBody = Blob.valueOf('Comment added successfully');
} else{
RestContext.response.statuscode = 404;
RestContext.response.responseBody = Blob.valueOf('Case not found for issue key: ' + issueKey);
}
}
}
Now I would like to send RichText/Html body like text having BOLD, Italic style...etc. along with attachments. Kindly advice on how to acheive this.
Also, Below is the code that I can able to send only the text body of FeedItem of case object to Jira Issue comment.
@future(callout=true)
public static void createJiraComment(Set<Id> FeedItemIds){
FeedItemtinsert = true;
List<FeedItem> FeedCommentList = [select id, ParentId, Body, CreatedById from FeedItem where id IN: FeedItemIds AND CreatedById !='005S000000RstyT '];
If(FeedCommentList!=null){
Case PerentCase = [Select Id, SL_JIRA_Key__c from case where Id =: FeedCommentList[0].ParentId ];
User cmntUser = [Select Id, Name FROM User where Id =: FeedCommentList[0].CreatedById];
string Username = cmntUser.Name;
String endpointUrl='https://sureify.atlassian.net/rest/api/3/issue/'+PerentCase.SL_JIRA_Key__c+ '/comment';
If(FeedCommentList[0].Body!=null){
for(FeedItem FeedCommentRec : FeedCommentList){
string cmntbody = 'Commented by ' + cmntUser.Name +' : ' + FeedCommentRec.Body;
system.debug('FeedCommentRec.Body == ' + FeedCommentRec.Body);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
Blob headerValue = Blob.valueOf('jira-intg-user@sureify.com' + ':' + Label.Jira_Int_Token);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
String body='{"body":{"content":[{"content":[{"text": "'+cmntbody+'","type":"text"}],"type": "paragraph"}],"type": "doc","version": 1}}';
request.setBody(body);
HttpResponse response = http.send(request);
system.debug('----response--'+response);
system.debug('---response body--'+response.getBody());
if (response.getStatusCode() == 201){
system.debug('---couponResults--'+response.getStatusCode());
}
}
}
}
}
}
I would like to send RichText/Html FeedItem body like text having BOLD, Italic style...etc. along with attachments to JIra Issue comments. Kindly advice on how to acheive this.
- Satish Kumar Lalam
- June 08, 2023
- Like
- 0
My email to Feeditem code is failing for few Pdf files
I wrote a code to catch the email replies into case feeditem object. below is my code and its working fine for all the attachments but failing for few PDF files. could anyone please if am Missing anything here.
Trigger :
trigger emailToPostChatter on EmailMessage (After insert) {
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
if(email.FromAddress !='ssp-support-test@sureify.com'){
List<User> u = [select Id, Name, Email from user where Email =: email.FromAddress AND isActive = true];
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.createdById = u[0].Id;
post.Title = 'EmailMessageFeed';
string emailBody = email.TextBody;
post.Body = '\n' + emailBody;
insert post;
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id, u[0].Id, email.ParentId);
}
else{
emailToPostTochatterAlerts.fireExternalEmail(email.ParentId, post.Id);
emailToPostTochatterAlerts.fireInternalEmail(email.ParentId, post.Id);
}
}
}
}
===== > Helper class to handle attachments
public class emailToPostChatterHelper {
@future
Public static void InsertFeedAttachment(Id emailId, Id postId, Id userId, Id CaseId){
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: emailId];
List<ContentDocument> cntdList = new List<ContentDocument>();
List<ContentVersion> cntvList = new List<ContentVersion>();
List<FeedAttachment> FeedAtchlist = new List<FeedAttachment>();
set<Id> FeedId = new set<Id>();
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contvrsn = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
ContentDocument cntd = new ContentDocument();
cntd.Id = contD.ContentDocumentId;
cntd.OwnerId = userId;
cntdList.add(cntd);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = postId; //Id of FeedItem
feedAttachment.RecordId = contvrsn.Id;
system.debug('emailToPostChatterHelper feedAttachment.RecordId ='+feedAttachment.RecordId);
feedAttachment.Type = 'Content';
FeedAtchlist.add(feedAttachment);
}
insert FeedAtchlist;
update cntdList;
}
}
}
Trigger :
trigger emailToPostChatter on EmailMessage (After insert) {
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
if(email.FromAddress !='ssp-support-test@sureify.com'){
List<User> u = [select Id, Name, Email from user where Email =: email.FromAddress AND isActive = true];
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.createdById = u[0].Id;
post.Title = 'EmailMessageFeed';
string emailBody = email.TextBody;
post.Body = '\n' + emailBody;
insert post;
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id, u[0].Id, email.ParentId);
}
else{
emailToPostTochatterAlerts.fireExternalEmail(email.ParentId, post.Id);
emailToPostTochatterAlerts.fireInternalEmail(email.ParentId, post.Id);
}
}
}
}
===== > Helper class to handle attachments
public class emailToPostChatterHelper {
@future
Public static void InsertFeedAttachment(Id emailId, Id postId, Id userId, Id CaseId){
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: emailId];
List<ContentDocument> cntdList = new List<ContentDocument>();
List<ContentVersion> cntvList = new List<ContentVersion>();
List<FeedAttachment> FeedAtchlist = new List<FeedAttachment>();
set<Id> FeedId = new set<Id>();
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contvrsn = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
ContentDocument cntd = new ContentDocument();
cntd.Id = contD.ContentDocumentId;
cntd.OwnerId = userId;
cntdList.add(cntd);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = postId; //Id of FeedItem
feedAttachment.RecordId = contvrsn.Id;
system.debug('emailToPostChatterHelper feedAttachment.RecordId ='+feedAttachment.RecordId);
feedAttachment.Type = 'Content';
FeedAtchlist.add(feedAttachment);
}
insert FeedAtchlist;
update cntdList;
}
}
}
- Satish Kumar Lalam
- November 07, 2023
- Like
- 0
Custom email alerts are not working for Customer Community users
I have Created a custom email alert notification with below code. we send email alerts when ever a comment is made on feeditem of case. My code is working fine for all users except Customer Community users. It is throwing below error when the user try to comment on feed from Customer portel. Kindly advice on how to resolve this issue.
FeedItemJiraIntegration: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Class.emailAlertsOnCaseFeedItem.fireExternalEmail: line 69, column 1 Class.emailAlertsOnCaseFeedItem.fireEmailalerts: line 17, column 1 Trigger.FeedItemJiraIntegration: line 5, column 1
TRIGGER :
trigger FeedItemJiraIntegration on FeedItem (After insert) {
if(trigger.isinsert && trigger.isafter){
FeedItemTiggerHelper.afterInsert(trigger.newMap.keyset());
emailAlertsOnCaseFeedItem.fireEmailalerts(trigger.newMap.keyset());
}
}
Helper :
public without sharing class emailAlertsOnCaseFeedItem {
//@InvocableMethod(label='emailAlertsOnCaseFeedItem')
//public static void fireEmailalerts(List<Id> feedItemIds){
public static void fireEmailalerts(Set<Id> feedItemIds){
Set<Id> caseIds = new Set<Id>();
List<FeedItem> feedList = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedItemIds ];
Map<Id,FeedItem> caseFeedmap = new Map<Id,FeedItem>();
system.debug('feedList = '+feedList.size());
for(FeedItem fd1: feedList){
caseFeedmap.put(fd1.ParentId,[select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:fd1.Id]);
caseIds.add(fd1.ParentId);
system.debug('caseIds = '+caseIds);
if(fd1.Visibility == 'AllUsers'){
system.debug('fd1.Visibility = '+fd1.Visibility);
fireExternalEmail(fd1.ParentId, fd1.Id);
fireInternalEmail(fd1.ParentId, fd1.Id);
}else{
system.debug('fd1.Visibility = '+fd1.Visibility);
fireInternalEmail(fd1.ParentId, fd1.Id);
}
}
}
Public static Void fireExternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
string caseUrl = 'https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id;
List<Id> conIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('External conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails1 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(toEmails);
mymail.setBccAddresses(bccEmails);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('PlainText');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('RichText');
}
System.debug('Statement after external insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
Public static Void fireInternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
List<Id> conIds = new List<Id>();
List<Id> teamIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Group> lstPublicGroup = [SELECT Id FROM Group WHERE Name= 'Support Team' and Type = 'Regular' LIMIT 1];
if(lstPublicGroup != null && !lstPublicGroup.isEmpty()){
for(GroupMember objMember : [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE GroupId = :lstPublicGroup[0].Id]){
if(String.valueOf(objMember.UserOrGroupId).startsWith('005')){
System.debug('User Id : ' +objMember.UserOrGroupId);
teamIds.add(objMember.UserOrGroupId);
}
}
}
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('Internal conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails2 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(teamIds);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
System.debug('Statement after insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
}
FeedItemJiraIntegration: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, null: [] Class.emailAlertsOnCaseFeedItem.fireExternalEmail: line 69, column 1 Class.emailAlertsOnCaseFeedItem.fireEmailalerts: line 17, column 1 Trigger.FeedItemJiraIntegration: line 5, column 1
TRIGGER :
trigger FeedItemJiraIntegration on FeedItem (After insert) {
if(trigger.isinsert && trigger.isafter){
FeedItemTiggerHelper.afterInsert(trigger.newMap.keyset());
emailAlertsOnCaseFeedItem.fireEmailalerts(trigger.newMap.keyset());
}
}
Helper :
public without sharing class emailAlertsOnCaseFeedItem {
//@InvocableMethod(label='emailAlertsOnCaseFeedItem')
//public static void fireEmailalerts(List<Id> feedItemIds){
public static void fireEmailalerts(Set<Id> feedItemIds){
Set<Id> caseIds = new Set<Id>();
List<FeedItem> feedList = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedItemIds ];
Map<Id,FeedItem> caseFeedmap = new Map<Id,FeedItem>();
system.debug('feedList = '+feedList.size());
for(FeedItem fd1: feedList){
caseFeedmap.put(fd1.ParentId,[select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:fd1.Id]);
caseIds.add(fd1.ParentId);
system.debug('caseIds = '+caseIds);
if(fd1.Visibility == 'AllUsers'){
system.debug('fd1.Visibility = '+fd1.Visibility);
fireExternalEmail(fd1.ParentId, fd1.Id);
fireInternalEmail(fd1.ParentId, fd1.Id);
}else{
system.debug('fd1.Visibility = '+fd1.Visibility);
fireInternalEmail(fd1.ParentId, fd1.Id);
}
}
}
Public static Void fireExternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
string caseUrl = 'https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id;
List<Id> conIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('External conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails1 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(toEmails);
mymail.setBccAddresses(bccEmails);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('PlainText');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Click on the link to access the case: '+'<a href="https://sureifyqa-supportportal.cs1.force.com/Communityv1/s/detail/'+parentCase.Id+'">Case Url</a>'+'<br/>'+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
system.debug('RichText');
}
System.debug('Statement after external insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
Public static Void fireInternalEmail(Id caseId, Id feedId){
Case parentCase = [Select Id, AccountId, CaseNumber, Subject, ContactEmail from Case where Id =:caseId];
FeedItem feedObj = [select Id, IsRichText, Body, Visibility, parentId from FeedItem where id =:feedId];
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'sureifylabs@gmail.com'];
List<Id> conIds = new List<Id>();
List<Id> teamIds = new List<Id>();
List<string> toEmails = new List<string>();
List<string> bccEmails = new List<string>();
List<Group> lstPublicGroup = [SELECT Id FROM Group WHERE Name= 'Support Team' and Type = 'Regular' LIMIT 1];
if(lstPublicGroup != null && !lstPublicGroup.isEmpty()){
for(GroupMember objMember : [SELECT GroupId, UserOrGroupId FROM GroupMember WHERE GroupId = :lstPublicGroup[0].Id]){
if(String.valueOf(objMember.UserOrGroupId).startsWith('005')){
System.debug('User Id : ' +objMember.UserOrGroupId);
teamIds.add(objMember.UserOrGroupId);
}
}
}
List<Contact> conList = [Select Id from Contact where AccountId =: parentCase.AccountId AND SL_Support_Contact__c = true];
system.debug('Internal conList = '+conList);
for (Contact con: conList){
conIds.add(con.Id);
bccEmails.add(con.Email);
system.debug('External con ID = '+conIds);
}
toEmails.add(parentCase.ContactEmail);
system.debug('toEmails2 = '+toEmails);
Messaging.reserveSingleEmailCapacity(2);
Messaging.SingleEmailMessage mymail = new Messaging.SingleEmailMessage();
mymail.setToAddresses(teamIds);
mymail.setWhatId(parentCase.Id);
mymail.setOrgWideEmailAddressId(owea.get(0).Id);
mymail.setSubject('[Sureify Support]'+' '+ parentCase.CaseNumber +' '+ parentCase.Subject);
if(feedObj.IsRichText == false){
mymail.setPlainTextBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
else{
mymail.setHtmlBody('You have a new comment on Case #'+parentCase.CaseNumber+ '<br/>'+ 'New Comment:'+'<br/>'+ feedObj.Body+'<br/>'+'Thank you.'+'<br/>'+'Sureify Support');
}
System.debug('Statement after insert.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mymail });
}
}
- Satish Kumar Lalam
- July 10, 2023
- Like
- 0
SOQL query returns no results Trigger but returns a row in Developer Console for ContentDocumentLink Object.
I have wrote a trigger on EmailMessage Object to get the attachments from email replies to case FeedItems. Below is my code Snippet. I am facing an issue with SOQL query on ContnentDocumentLink object. Where the the same query is working fine in Anonymous window but not in trigger. Could you please advise on this. Below is my Code Snippet. Kindly advice on this.
trigger emailToPostChatter on EmailMessage (After insert) {
List<FeedItem> itemList = new List<FeedItem>();
List<FeedItem> upItemList = new List<FeedItem>();
List<ContentVersion> conVersionList = new List<ContentVersion>();
List<FeedAttachment> atchList = new List<FeedAttachment>();
List<FeedAttachment> FeedAtchList = new List<FeedAttachment>();
List<ContentDocumentLink> contDocLinks = new List<ContentDocumentLink>();
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.Body = email.FromName + '\n' + email.TextBody;
system.debug('Body = '+post.Body);
insert post;
system.debug('EmailMessage Id= '+ email.Id);
system.debug('message.ContentDocumentIds = '+email.ContentDocumentIds);
system.debug('Post ID 1= '+ post.Id);
system.debug('message.HasAttachment = '+email.HasAttachment);
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
//emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id);
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: email.Id];
//List<ContentDocumentLink> contDocLinks2 = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :email.Id];
system.debug('contDocLinks = '+contDocLinks);
//system.debug('contDocLinks2 = '+contDocLinks2);
system.debug('LinkedEntityId = '+email.Id);
system.debug('contDocLinks Size = '+contDocLinks.size());
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contv = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
system.debug('contv ='+contv);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = post.Id; //Id of FeedItem
feedAttachment.RecordId = contv.Id;
system.debug('feedAttachment.RecordId ='+feedAttachment.RecordId);
//feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT';
insert feedAttachment;
}
}
}
}
}
trigger emailToPostChatter on EmailMessage (After insert) {
List<FeedItem> itemList = new List<FeedItem>();
List<FeedItem> upItemList = new List<FeedItem>();
List<ContentVersion> conVersionList = new List<ContentVersion>();
List<FeedAttachment> atchList = new List<FeedAttachment>();
List<FeedAttachment> FeedAtchList = new List<FeedAttachment>();
List<ContentDocumentLink> contDocLinks = new List<ContentDocumentLink>();
Set<Id> EmailId = new Set<Id>();
for (EmailMessage email : trigger.new) {
FeedItem post = new FeedItem();
post.ParentId = email.ParentId;
post.Visibility = 'AllUsers';
post.Body = email.FromName + '\n' + email.TextBody;
system.debug('Body = '+post.Body);
insert post;
system.debug('EmailMessage Id= '+ email.Id);
system.debug('message.ContentDocumentIds = '+email.ContentDocumentIds);
system.debug('Post ID 1= '+ post.Id);
system.debug('message.HasAttachment = '+email.HasAttachment);
if(email.HasAttachment){
system.debug('EmailMessage Id inside If = '+ email.Id);
//emailToPostChatterHelper.InsertFeedAttachment(email.Id, Post.Id);
List<ContentDocumentLink> contDocLinks = [select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId =: email.Id];
//List<ContentDocumentLink> contDocLinks2 = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersion.VersionData FROM ContentDocumentLink WHERE LinkedEntityId = :email.Id];
system.debug('contDocLinks = '+contDocLinks);
//system.debug('contDocLinks2 = '+contDocLinks2);
system.debug('LinkedEntityId = '+email.Id);
system.debug('contDocLinks Size = '+contDocLinks.size());
if(contDocLinks.size()!=null){
for(ContentDocumentLink contD : contDocLinks){
ContentVersion contv = [select Id from ContentVersion where ContentDocumentId =:contD.ContentDocumentId];
system.debug('contv ='+contv);
FeedAttachment feedAttachment = new FeedAttachment();
feedAttachment.FeedEntityId = post.Id; //Id of FeedItem
feedAttachment.RecordId = contv.Id;
system.debug('feedAttachment.RecordId ='+feedAttachment.RecordId);
//feedAttachment.Title = 'FileName';
feedAttachment.Type = 'CONTENT';
insert feedAttachment;
}
}
}
}
}
- Satish Kumar Lalam
- June 27, 2023
- Like
- 0
I am working on Integration from JIra to Salesforce and vice versa. Now, How do we built custom Rest API for getting issue comment body(Richtext/htmlbody) and comment attachments from jira issue to salesforce case Feeditem record?
I could able to implement the custom Rest API to receive the text only format form Jira issue comment to case feeditem record. which is working fine with the below code.
@RestResource(urlMapping='/api/webhooks/pushDetails/*')
global class jiraWebhookListner {
@HttpPost
global static Void receiveJiraIssueComments(){
system.debug('API Request--> ' +RestContext.request);
system.debug('API Request Body --> ' +RestContext.request.requestBody);
RestRequest request = RestContext.request;
Blob body = request.requestBody;
//string requestBody = request.requestBody.toString();
String bodyString = body.toString();
system.debug('String body = '+bodyString);
Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(bodyString);
system.debug('payload = ' +payload);
string commentText = (String)payload.get('comment');
string issueKey = (String)payload.get('issueKey');
system.debug('Data display = ' + (string)payload.get('Data'));
system.debug('commentText = ' + commentText);
system.debug('issueKey = ' + issueKey);
case caseObj = [Select Id from case WHERE SL_JIRA_Key__c = : issueKey LIMIT 1];
system.debug('case = ' + caseObj);
IF(caseObj != null){
FeedItem feedItemObj = new FeedItem();
feedItemObj.IsRichText = True;
feedItemObj.ParentId = caseObj.Id;
feedItemObj.Body = commentText;
insert feedItemObj;
RestContext.response.statuscode = 200;
RestContext.response.responseBody = Blob.valueOf('Comment added successfully');
} else{
RestContext.response.statuscode = 404;
RestContext.response.responseBody = Blob.valueOf('Case not found for issue key: ' + issueKey);
}
}
}
Now I would like to send RichText/Html body like text having BOLD, Italic style...etc. along with attachments. Kindly advice on how to acheive this.
Also, Below is the code that I can able to send only the text body of FeedItem of case object to Jira Issue comment.
@future(callout=true)
public static void createJiraComment(Set<Id> FeedItemIds){
FeedItemtinsert = true;
List<FeedItem> FeedCommentList = [select id, ParentId, Body, CreatedById from FeedItem where id IN: FeedItemIds AND CreatedById !='005S000000RstyT '];
If(FeedCommentList!=null){
Case PerentCase = [Select Id, SL_JIRA_Key__c from case where Id =: FeedCommentList[0].ParentId ];
User cmntUser = [Select Id, Name FROM User where Id =: FeedCommentList[0].CreatedById];
string Username = cmntUser.Name;
String endpointUrl='https://sureify.atlassian.net/rest/api/3/issue/'+PerentCase.SL_JIRA_Key__c+ '/comment';
If(FeedCommentList[0].Body!=null){
for(FeedItem FeedCommentRec : FeedCommentList){
string cmntbody = 'Commented by ' + cmntUser.Name +' : ' + FeedCommentRec.Body;
system.debug('FeedCommentRec.Body == ' + FeedCommentRec.Body);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
Blob headerValue = Blob.valueOf('jira-intg-user@sureify.com' + ':' + Label.Jira_Int_Token);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
String body='{"body":{"content":[{"content":[{"text": "'+cmntbody+'","type":"text"}],"type": "paragraph"}],"type": "doc","version": 1}}';
request.setBody(body);
HttpResponse response = http.send(request);
system.debug('----response--'+response);
system.debug('---response body--'+response.getBody());
if (response.getStatusCode() == 201){
system.debug('---couponResults--'+response.getStatusCode());
}
}
}
}
}
}
I would like to send RichText/Html FeedItem body like text having BOLD, Italic style...etc. along with attachments to JIra Issue comments. Kindly advice on how to acheive this.
@RestResource(urlMapping='/api/webhooks/pushDetails/*')
global class jiraWebhookListner {
@HttpPost
global static Void receiveJiraIssueComments(){
system.debug('API Request--> ' +RestContext.request);
system.debug('API Request Body --> ' +RestContext.request.requestBody);
RestRequest request = RestContext.request;
Blob body = request.requestBody;
//string requestBody = request.requestBody.toString();
String bodyString = body.toString();
system.debug('String body = '+bodyString);
Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(bodyString);
system.debug('payload = ' +payload);
string commentText = (String)payload.get('comment');
string issueKey = (String)payload.get('issueKey');
system.debug('Data display = ' + (string)payload.get('Data'));
system.debug('commentText = ' + commentText);
system.debug('issueKey = ' + issueKey);
case caseObj = [Select Id from case WHERE SL_JIRA_Key__c = : issueKey LIMIT 1];
system.debug('case = ' + caseObj);
IF(caseObj != null){
FeedItem feedItemObj = new FeedItem();
feedItemObj.IsRichText = True;
feedItemObj.ParentId = caseObj.Id;
feedItemObj.Body = commentText;
insert feedItemObj;
RestContext.response.statuscode = 200;
RestContext.response.responseBody = Blob.valueOf('Comment added successfully');
} else{
RestContext.response.statuscode = 404;
RestContext.response.responseBody = Blob.valueOf('Case not found for issue key: ' + issueKey);
}
}
}
Now I would like to send RichText/Html body like text having BOLD, Italic style...etc. along with attachments. Kindly advice on how to acheive this.
Also, Below is the code that I can able to send only the text body of FeedItem of case object to Jira Issue comment.
@future(callout=true)
public static void createJiraComment(Set<Id> FeedItemIds){
FeedItemtinsert = true;
List<FeedItem> FeedCommentList = [select id, ParentId, Body, CreatedById from FeedItem where id IN: FeedItemIds AND CreatedById !='005S000000RstyT '];
If(FeedCommentList!=null){
Case PerentCase = [Select Id, SL_JIRA_Key__c from case where Id =: FeedCommentList[0].ParentId ];
User cmntUser = [Select Id, Name FROM User where Id =: FeedCommentList[0].CreatedById];
string Username = cmntUser.Name;
String endpointUrl='https://sureify.atlassian.net/rest/api/3/issue/'+PerentCase.SL_JIRA_Key__c+ '/comment';
If(FeedCommentList[0].Body!=null){
for(FeedItem FeedCommentRec : FeedCommentList){
string cmntbody = 'Commented by ' + cmntUser.Name +' : ' + FeedCommentRec.Body;
system.debug('FeedCommentRec.Body == ' + FeedCommentRec.Body);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(endpointUrl);
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
Blob headerValue = Blob.valueOf('jira-intg-user@sureify.com' + ':' + Label.Jira_Int_Token);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
request.setHeader('Authorization', authorizationHeader);
String body='{"body":{"content":[{"content":[{"text": "'+cmntbody+'","type":"text"}],"type": "paragraph"}],"type": "doc","version": 1}}';
request.setBody(body);
HttpResponse response = http.send(request);
system.debug('----response--'+response);
system.debug('---response body--'+response.getBody());
if (response.getStatusCode() == 201){
system.debug('---couponResults--'+response.getStatusCode());
}
}
}
}
}
}
I would like to send RichText/Html FeedItem body like text having BOLD, Italic style...etc. along with attachments to JIra Issue comments. Kindly advice on how to acheive this.
- Satish Kumar Lalam
- June 08, 2023
- Like
- 0
How to insert a feedItem with attachment linked by using RelatedRecordId in FeedItem
Hello Everyone,
I have a requirment to insert a feed with attachment link when any email is received in salesforce from EmailMessage [ Like if any customer is sending an email with attachment link so Feed should be inserted and attachment will link in feed ] like below image.
I have a requirment this by using a field -RelatedRecordId from FeedItem Object.
Please help me to get the answer....
Regards,
Sangeet
I have a requirment to insert a feed with attachment link when any email is received in salesforce from EmailMessage [ Like if any customer is sending an email with attachment link so Feed should be inserted and attachment will link in feed ] like below image.
I have a requirment this by using a field -RelatedRecordId from FeedItem Object.
Please help me to get the answer....
Regards,
Sangeet
- Sangeet kasera
- March 23, 2021
- Like
- 0
SOQL query returns no results Trigger but returns a row in Developer Console
I have the following code snippet in a Helper class that's being called by an After Insert trigger (modified to demonstrate my objective):
The debug output is showing me that I'm getting zero results. However, when I run the same query in the debug console, I see the single row returned that I'm expecting.
I have included without sharing on the trigger Helper class even though my understanding is that's not necessary in a trigger, but it doesn't change my results.
Can anyone offer any clues as to what I may be experiencing here?
Set<Id> reuseIds = new Set<Id>(); reuseIds.add('a0g0R000001VnjMQAS'); List<ContentDocumentLink> allLinks = [SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN :reuseIds]; System.debug('allLinks size: ' + allLinks.size());
The debug output is showing me that I'm getting zero results. However, when I run the same query in the debug console, I see the single row returned that I'm expecting.
I have included without sharing on the trigger Helper class even though my understanding is that's not necessary in a trigger, but it doesn't change my results.
Can anyone offer any clues as to what I may be experiencing here?
- Mike Chandler
- January 22, 2020
- Like
- 0