• Richa Sharma 45
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi all,

In my org, when a customer service agent is working on a case and hits the send email button to use the standard send email functionality, I hav a requirement that is:
- Capture the body of the email and create with it a Case Comment related to the parent case of the email. Also, get all the attachments related to the email and relate them to the case. Finally, delete the email message.

I am able to do everything except for the attachments part. I have created a trigger on emailMessage that creates the case comment without trouble, but another part of my trigger is created to get the attachments, but it always returns 0 elements in the list. This is the trigger on email message related to the attachment part(just getting the attachment of the first email to check if it works):
 
trigger EmailMessageAfterOperationTrigger on EmailMessage (after delete, after insert, after undelete, after update) {

if (trigger.isInsert){
    	Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Id];
    	Attachment[] insertAttList = new Attachment[]{};

        system.debug('Has the email attachments?:' + Trigger.new[0].HasAttachment);
    	system.debug('list of attachments realted to the email message: ' + attList);

    	for(Attachment a: attList){
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
               insertAttList.add(att);
        }
       if(insertAttList.size() > 0){
            insert insertAttList;
       }
    	
    }
This is the debug log the system shows:
 
09:45:00.579 (1579755455)|USER_DEBUG|[146]|DEBUG|Has the email attachments?:true
09:45:00.579 (1579760206)|SYSTEM_METHOD_EXIT|[146]|System.debug(ANY)
09:45:00.579 (1579778024)|SYSTEM_METHOD_ENTRY|[147]|String.valueOf(Object)
09:45:00.579 (1579802168)|SYSTEM_METHOD_EXIT|[147]|String.valueOf(Object)
09:45:00.579 (1579814171)|SYSTEM_METHOD_ENTRY|[147]|System.debug(ANY)
09:45:00.579 (1579819259)|USER_DEBUG|[147]|DEBUG|list of attachments realted to the email message: ()

I have also created a trigger on attachment before insert but it doesnt get fired when sending an email with attachment:
 
trigger AttachmentBeforeOperationTrigger on Attachment (before delete, before insert, before update) {

	if (Trigger.isInsert || Trigger.isUpdate){
		for (Attachment a: Trigger.new){
			system.debug('el attachment: ' + a);
		}
	}

}

I need to get this attachments related to the email message, can please anyone help me?

Thanks,

Antonio





 
Hi all,

In my org, when a customer service agent is working on a case and hits the send email button to use the standard send email functionality, I hav a requirement that is:
- Capture the body of the email and create with it a Case Comment related to the parent case of the email. Also, get all the attachments related to the email and relate them to the case. Finally, delete the email message.

I am able to do everything except for the attachments part. I have created a trigger on emailMessage that creates the case comment without trouble, but another part of my trigger is created to get the attachments, but it always returns 0 elements in the list. This is the trigger on email message related to the attachment part(just getting the attachment of the first email to check if it works):
 
trigger EmailMessageAfterOperationTrigger on EmailMessage (after delete, after insert, after undelete, after update) {

if (trigger.isInsert){
    	Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Id];
    	Attachment[] insertAttList = new Attachment[]{};

        system.debug('Has the email attachments?:' + Trigger.new[0].HasAttachment);
    	system.debug('list of attachments realted to the email message: ' + attList);

    	for(Attachment a: attList){
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
               insertAttList.add(att);
        }
       if(insertAttList.size() > 0){
            insert insertAttList;
       }
    	
    }
This is the debug log the system shows:
 
09:45:00.579 (1579755455)|USER_DEBUG|[146]|DEBUG|Has the email attachments?:true
09:45:00.579 (1579760206)|SYSTEM_METHOD_EXIT|[146]|System.debug(ANY)
09:45:00.579 (1579778024)|SYSTEM_METHOD_ENTRY|[147]|String.valueOf(Object)
09:45:00.579 (1579802168)|SYSTEM_METHOD_EXIT|[147]|String.valueOf(Object)
09:45:00.579 (1579814171)|SYSTEM_METHOD_ENTRY|[147]|System.debug(ANY)
09:45:00.579 (1579819259)|USER_DEBUG|[147]|DEBUG|list of attachments realted to the email message: ()

I have also created a trigger on attachment before insert but it doesnt get fired when sending an email with attachment:
 
trigger AttachmentBeforeOperationTrigger on Attachment (before delete, before insert, before update) {

	if (Trigger.isInsert || Trigger.isUpdate){
		for (Attachment a: Trigger.new){
			system.debug('el attachment: ' + a);
		}
	}

}

I need to get this attachments related to the email message, can please anyone help me?

Thanks,

Antonio