0

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;

}

4
  • do you have the debug log enabled for the context user associated with email to case (which might be different than your userid)?
    – cropredy
    Commented Aug 4, 2015 at 20:03
  • @crop1645 Thank you for responding.I do have debug enabled under my name(Sys admin). If i am wrong how enable the debug for context user.
    – sravan
    Commented Aug 4, 2015 at 20:34
  • @crop1645 Yea i see what you are saying.
    – sravan
    Commented Aug 4, 2015 at 20:53
  • Settings | Support Settings | Automated Case User if using on-demand email to case
    – cropredy
    Commented Aug 4, 2015 at 21:00

1 Answer 1

1

Are you testing this in your sandbox? Are you using the email to case address for sandbox?

Go to Setup > Customize > Cases > Email to Case to check.

1
  • Thank you for responding.Yes i am working in Sandbox. With the suggestion of @crop1645 i came to see my Debug log and figure out whats going on over there. Actually i went to Settings | Support Settings | Automated Case User. So the cases created from Email to Case are created under that Automated Case User. Basically the Context User is different from what i see in Debug. So i activated Debug for that Automated Case User and it worked. And i figured out my issue too.
    – sravan
    Commented Aug 5, 2015 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .