Trigger to create an attachment and attach it along with the record in Salesforce

Sample Trigger:

trigger sendEmail on Employee__c (after insert)  {    
    List<Attachment> listAttachments = new List<Attachment>();
    for(Employee__c e : trigger.new) {   
        Attachment a = new Attachment();
        a.ParentId = e.Id;
        a.Name = e.Name + Date.Today();
        a.ContentType = 'text/plain';
        a.Body = Blob.valueOf(e.Name + ' is added successfully');
        listAttachments.add(a);
    }
    insert listAttachments;
}

Cheers!!!

1 comment:

  1. Is it possible to write a trigger to just modify the title of an existing Attachment, as opposed to creating a new one?

    ReplyDelete