Basically i have an requirement which has to convert attachments in to feed attachments which is working fine when i write trigger on attachment object. But it is giving me Email to case related errors. So i modified my code and executed. But the strange thing is i have my debug log turned on and i am sending an email with attachment and can't see the debug for that. i will share my code here. Any suggestion's would be a great help.
trigger ConvertAttachmentToFile on Attachment (after insert) {
List<FeedItem> listOfFeedFiles = new List<FeedItem>();
Set<Id> attchmntids = new Set<Id>();
Map<Id,EmailMessage> emailMessageMp= new Map<Id,EmailMessage>();
for(EmailMessage em:[select id, ParentId from EmailMessage])
{
emailMessageMp.put(em.ParentId,em);
system.debug('emess@@'+ emailMessageMp);
}
for(Attachment att:trigger.new){
if(Schema.SObjectType.EmailMessage.isFeedEnabled()){
FeedItem fed = new FeedItem();
fed.ParentId =att.ParentId;
fed.type = 'ContentPost';
fed.ContentData = att.body;
fed.Title = att.Name.substring(0, att.Name.LastindexOf('.'));
fed.ContentFileName = att.Name;
fed.Body ='Attached File - ' + att.Name.substring(0, att.Name.LastindexOf('.'));
system.debug('name111@@'+fed.Body);
listOfFeedFiles.add(fed);
system.debug('name222@@'+ listOfFeedFiles);
attchmntids.add(att.id);
}
else{
EmailMessage emM = emailMessageMp.get(att.ParentId);
system.debug('emess11@@'+ emM);
Schema.DescribeSObjectResult r = EmailMessage.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();
FeedItem fed = new FeedItem();
if(emM!=null && keyPrefix=='02s'){
fed.ParentId=emM.ParentId;
}else{
fed.ParentId=att.ParentId;
}
fed.type = 'ContentPost';
fed.ContentData = att.body;
fed.Title = att.Name.substring(0, att.Name.LastindexOf('.'));
fed.ContentFileName = att.Name;
fed.Body ='Attached File - ' + att.Name.substring(0, att.Name.LastindexOf('.'));
listOfFeedFiles.add(fed);
attchmntids.add(att.id);
}
}
if(listOfFeedFiles.size()>0) insert listOfFeedFiles;
list<Attachment> attchmnts =[select id,name from Attachment where Id IN:attchmntids];
if(!attchmnts.isempty()) Delete attchmnts;
}
Settings | Support Settings | Automated Case User
if using on-demand email to case