0

I'm new with Apex. I'm downloading images from web with Apex that i'd like to add in Files. Already i'm just adding images to Attachment.

//Download image
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(linkImg);
req.setTimeout(60000);
req.setMethod('GET');

HttpResponse res = h.send(req);

Blob bodyImg = res.getBodyAsBlob();

//Add image to Attachment
Attachment att = new Attachment(Name = nameImg, Body = 
           bodyImg, ContentType = 'image/png',
           ParentId=parentid);
insert att;

Is there a way to add document directly in Files or convert/link automatically Attachment to Files ?

1
  • There are multiple applications to convert Attachments to Files after the fact. Doug Ayers has one; there are others.
    – David Reed
    Apr 19 '18 at 11:17
0

Use this apex code to create file.

ContentVersion file = new ContentVersion();
file.Title = 'File Name';
file.PathOnClient = 'file_' + Datetime.now().getTime() + '.txt';
file.VersionData = Blob.valueOf('My Content in file_' + 
Datetime.now().getTime() + '.txt');
file.Origin = 'H';
insert file; 

hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.