You need to sign in to do that
Don't have an account?

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!
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!
Please follow below code it's tested my side
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
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,
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!
Please follow below code it's tested my side
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Thank you so much! It worked :)