July 5, 2013

How to generate a Text file using apex in Salesforce?

Sample Code:

        Employee__c e = [SELECT Name from Employee__c WHERE Id = : eid];
       
        Attachment a = new Attachment();
        a.ParentId = eid;
        a.Name = e.Name + Date.Today();
        a.ContentType = 'text/plain';
        a.Body = Blob.valueOf(e.Name);
       
        insert a;

Cheers!!!

2 comments:

  1. Getting this exception while running this code from Anonymous window :ERRORInsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [FolderId]: [FolderId] null

    ReplyDelete
    Replies
    1. I tried this in a brand new org and I am not getting this error. Set debug log and check whether any custom logic is causing this.

      Delete