+ Start a Discussion
Vimarsh SaxenaVimarsh Saxena 

Trigger on Content Document to Create New Task Record


I am trying to create an after insert trigger that would work on ContentDocument and create a new task record based on the type of ContentDocument. I have to go this route of creating trigger because Process Builder and Flows cannot be used with ContentDocument. I am a newbie to development and had given a shot in my Sandbox first. I believe the code is right because I don't see any syntax errors but it's actually not working. Can't figure out what's wrong. Any help is greatly appreciated! Below is my trigger:


trigger EmailUpload on ContentDocument (after insert) {
  List <Task> newtask = new List <Task> ();
    for (ContentDocument c : Trigger.new) {
        // here is where you check if content document uploaded is of MSG format
        if (c.FileExtension == 'msg') {
        Task t = new Task (); //instantiate the object to put values for future record
        // now assign field values to Task object that is being created
        t.Subject = 'New Email';
        t.ActivityDate = SYSTEM.TODAY();
        t.OwnerId = c.CreatedById;
        }    //end if
    }        //end for loop
    //once loop is done, insert new records in SF
    // dml operations might cause an error, so catch it with try/catch block.
    try {
        insert newtask; 
    } catch (system.Dmlexception e) {
        system.debug (e);

AbhinavAbhinav (Salesforce Developers) 
Hi Vimarsh,

Please refer below link for similar scenario
Please marks its best answer if it helps.
Vimarsh SaxenaVimarsh Saxena
Thanks Abhinav but the link you shared didn't help