+ Start a Discussion
csbaacsbaa 

Create Attachment body in apex

Hello Helper

I wowuld like  to create  an  attchment  in an apex  code

I have a List<String>   variable  n my apex  and I want  somehow  to save the strings  in a file  and attach it  to a custom object

I  know the attachment  object  and I know  I can  link this  object  with another custom object

what I do not know  is how  to add my  data  to the attachment object

I sew the field  Body  of  the Attachment  object  but I do not know  how  to  pass my  string list data  to the body



any advice is appreciated

Best  Regards
Csaba


Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi

Use the below code snippet as reference:



list<string>strlst=new list<string>{'a','b'};
string body1='';
for(string s:strlst)
{
body1=body1+''+s;
}
Blob b = Blob.valueOf(body1);
Attachment attach1= new Attachment();
attach1.ParentId = parentId;
attach1.Name = 'Test Attachment for Parent';
attach1.Body = b;

insert attach1;

Thanks
Annkit gupta
Abhik SahaAbhik Saha
How can I create that content of the attachment as a zip file? I think they way code is working here it would be a text file. now, if I also want it to be zipped, how can I do that?