+ Start a Discussion
TestingSFUserTestingSFUser 

Create Task using ContentVersion object in Apex

Hi..,
Whenever i uploaded a new file doc related to Case record, i want to create new Task which needs to be assigned to Case Owner.
so, suggest me any ideas to fulfill this requirement.
Thanks.
Best Answer chosen by TestingSFUser
ravi soniravi soni
hi ,
try below trigger.
trigger assignTaskOfCaseOwner on ContentDocumentLink (after insert) {
     if(trigger.isAfter && trigger.isInsert){
        Set<String> setCaseIds  = new Set<String>();
         list<task> lstTask = new list<task>();
         for(ContentDocumentLink cv : trigger.new){
            if(cv.LinkedEntityId != null && cv.LinkedEntityId.getSobjectType() == Case.sObjectType){
                setCaseIds.add(cv.LinkedEntityId);
            }
        }
         
         for(Case oCase : [SELECT Id,OwnerId From Case Where Id In : setCaseIds]){
               Task oTask= new Task(OwnerId =oCase.OwnerId,STATUS='Not Started',subject='new Task Subject',priority='Normal');
                lstTask.add(oTask);
         }
         
         if(lstTask.size() > 0){
             insert lstTask;
         }
    }

}

don't forget to mark it as best answer if it helps you.
Thank you​​​​​​​

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Sukumar,

The trigger code can be as below.
 
trigger UpdateCaseonFile on ContentVersion (after insert) {
Set<Id> contentDocumentIdSet = new Set<Id>();
    
    for(ContentVersion cv:trigger.new)
    {
        if(cv.ContentDocumentId != null)
        {
            contentDocumentIdSet.add(cv.ContentDocumentId);
        }
    }
    set<id>cdlsid= new set<id>();
    List<ContentDocumentLink> cdl = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:contentDocumentIdSet];
    for (ContentDocumentLink cdlist:cdl){
        cdlsid.add(cdlist.LinkedEntityId);
    }
    
    List<Case> caseList = [SELECT Id,OwnerId  FROM Case  where Id =:cdlsid];   
     List<ContentVersion> cvList = [SELECT Id, ContentDocumentId FROM ContentVersion where Id IN: Trigger.newMap.keySet()];
    
    for (ContentVersion cv:cvList){
        if(caseList.size()>0){
            for(CASE CS:caseList){
                Task ts= new Task(OwnerId =cs.OwnerId,STATUS='Not Started',subject='other',priority='Normal');
                insert ts;
            }
        }
        
    }
    
}

If this solution helps, Please mark it as best answer.

Thanks,
 
Maharajan CMaharajan C
Hi Sukumar,

Please try the below tigger code surely it will work:
 
trigger createTaskonFileUpload on ContentDocumentLink (after insert) {
    set<Id> caseIdSet = new set<Id>();
    List<Task> taskListtoInsert = new List<Task>();
    List<ContentDocumentLink> cdlList = new List<ContentDocumentLink>();
    for(ContentDocumentLink cn : Trigger.new){
        if(cn.LinkedEntityId != null && cn.LinkedEntityId.getSObjectType() == Case.sObjectType){
            caseIdSet.add(cn.LinkedEntityId);
            cdlList.add(cn);
        } 
    }
    
    if(!caseIdSet.isEmpty()){
        Map<Id,Case> caseMap = new Map<Id,Case>([Select Id, OwnerId from Case where ID IN : caseIdSet]);
        for(ContentDocumentLink c : cdlList){
            if(caseMap.containsKey(c.LinkedEntityId)){
                taskListtoInsert.add(new Task(OwnerId =caseMap.get(c.LinkedEntityId).OwnerId,whatId = caseMap.get(c.LinkedEntityId).Id,STATUS='Not Started',subject='File Uploaded in Case',priority='Normal'));
            } 
        }
    }
    
    if(!taskListtoInsert.isEmpty()){
        insert taskListtoInsert;
    }
    
}

1. Write the trigger in ContentDocumentLink for this scenario will helps to eliminate Additional SOQLS...

2. You have to the check the LinkedEntityId is case Object or not... 
if(cn.LinkedEntityId != null && cn.LinkedEntityId.getSObjectType() == Case.sObjectType){

3. Try to avoid for loops inside for loop... Trigger should be in bulkified...


Thanks,
Maharajan.C
ravi soniravi soni
hi ,
try below trigger.
trigger assignTaskOfCaseOwner on ContentDocumentLink (after insert) {
     if(trigger.isAfter && trigger.isInsert){
        Set<String> setCaseIds  = new Set<String>();
         list<task> lstTask = new list<task>();
         for(ContentDocumentLink cv : trigger.new){
            if(cv.LinkedEntityId != null && cv.LinkedEntityId.getSobjectType() == Case.sObjectType){
                setCaseIds.add(cv.LinkedEntityId);
            }
        }
         
         for(Case oCase : [SELECT Id,OwnerId From Case Where Id In : setCaseIds]){
               Task oTask= new Task(OwnerId =oCase.OwnerId,STATUS='Not Started',subject='new Task Subject',priority='Normal');
                lstTask.add(oTask);
         }
         
         if(lstTask.size() > 0){
             insert lstTask;
         }
    }

}

don't forget to mark it as best answer if it helps you.
Thank you​​​​​​​
This was selected as the best answer
ravi soniravi soni
hi Sukumar,
Please mark it as best answer if it helps you.
your like and best mark give us motivation to help others.
Thank you