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
bsil bsilbsil bsil 

How to attach one file to multiple tasks from visualforce page

Hi Guys,

How to attache one file to multiple tasks at time from visualforce.

Here i am developing visualforce page for mass task creation and i am going to attache one file for all the tasks.
Here i am uploading file at one time, that file only i am going to attach to multiple tasks

Thanks in advance.....................
Best Answer chosen by bsil bsil
Vinit_KumarVinit_Kumar
Happy to help!!

Mark it as best answer to help others :)

All Answers

Ramu_SFDCRamu_SFDC
Even though it is a single file as we attach it seperately to individual records it is treated as individual file as a new reference id is generated for each of these attachments. Due to this, I do not think it is possible to attach the single file to multiple records at a time. 
Vinit_KumarVinit_Kumar
Try belwo code,this should work :-
List<Account> accList = [select id from Account limit 10];
List<Attachment> attList = new List<Attachment>();

for(ineteger i=0;i<accList.size();i++)
{
for(Account acc: accList)
{
	Attachment att = new Attachment(body='<your boby>',name='test doc',ParentId=acc.id); //Attaching to 10 Accounts
	attList.add(att);
}
}

if(attList.size()>0)
{
	insert attList;
}

If this helps,please mark it as best answer to help others :)
bsil bsilbsil bsil
Thanks Vinit ...............!
Vinit_KumarVinit_Kumar
Happy to help!!

Mark it as best answer to help others :)
This was selected as the best answer