function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vive SubramaniVive Subramani 

Insert csv attachment to task from apex class

Hey, I need to attach a csv file to the task record. I could insert it to Account object, but it is not working for Tasks. Could you please let me know if it is possible to insert attachment to Task object.
Sample code below,

ContentVersion conVer = new ContentVersion();
        conVer.ContentLocation = 'S';
        conVer.PathOnClient = 'taskFile.csv'; 
        conVer.Title = 'Task Test Files'; 
        conVer.VersionData = Blob.valueOf(attString);
        insert conVer;  
        
        
        Id conDoc = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
        ContentDocumentLink conDocLink = New ContentDocumentLink();
        conDocLink.LinkedEntityId = '00T5g00000IbMnyEAF'; //Task Id
        conDocLink.ContentDocumentId = conDoc;  
        conDocLink.shareType = 'C';
        insert conDocLink;

Thanks in Advance!
Best Answer chosen by Vive Subramani
mukesh guptamukesh gupta
Hi  Vive,

Please follow below code it's tested my side
 
trigger TaskTrigger on Task (after insert) {
	List<Task> lstTask = trigger.new;
for(Task task : lstTask) {
Attachment att = new Attachment();
att.ContentType = 'application/xml';
att.Body = Blob.valueOf('<a><b>Test B</b></a>');
att.ParentId = task.Id;
att.Name = 'TaskAttachment.xml';
system.debug('task.OwnerId : '+task.OwnerId);
system.debug('task.OwnerId : '+task.OwnerId);
att.OwnerId = task.OwnerId;

insert att;
}

}
if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

SfdcMaxSfdcMax
Hi Vive,

Thanks for the question!

What error do you get here? I tried the above code and was able to insert the record with Task id as entity id. Please refer to the below link which states that Task is the supported object for the entityId field on the content document link.

Click Here: (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm)

Please choose this as the best answer or upvote if it was helpful.

Kind regards,
Vive SubramaniVive Subramani
Hi SfdcMax,

Thanks for your response!
I just checked in the query editor. The record is actually saved. But, I couldn't see the attachment in the attachment section under the related list tab of the respective Task record.
Could you please let me know why I'm not able to see the attachment in Task related list?

Thank you!
mukesh guptamukesh gupta
Hi  Vive,

Please follow below code it's tested my side
 
trigger TaskTrigger on Task (after insert) {
	List<Task> lstTask = trigger.new;
for(Task task : lstTask) {
Attachment att = new Attachment();
att.ContentType = 'application/xml';
att.Body = Blob.valueOf('<a><b>Test B</b></a>');
att.ParentId = task.Id;
att.Name = 'TaskAttachment.xml';
system.debug('task.OwnerId : '+task.OwnerId);
system.debug('task.OwnerId : '+task.OwnerId);
att.OwnerId = task.OwnerId;

insert att;
}

}
if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer
Vive SubramaniVive Subramani
Hi Mukesh,

Thank you so much! It worked :)