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

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;
}

Leave a Reply