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
Shubham Deshmukh 15Shubham Deshmukh 15 

how to download docx file from url using apex

if it is pdf we can download using following code

 Pagereference pg = new Pagereference(url); 
                    Blob b = pg.getContentAsPDF(); //Here you can use getContent() too for another type of file
                    Attachment att = new Attachment(Name = extractedString, Body = b, ContentType = 'application/pdf', ParentId='a3KDx000000TXxBMAW');
                    insert (att);
If it is other than pdf like docx, image, recording etc etc. file is being inserted but it has nothing. Thanks in advance any help will be appreciated.

SubratSubrat (Salesforce Developers) 
Hello Shubham ,

To download a DOCX file from a URL using Apex, you can use the Http class to make a GET request and retrieve the file contents.
Please refer below code :
String url = 'https://example.com/path/to/file.docx';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(url);
request.setMethod('GET');

HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
    Blob fileContent = response.getBodyAsBlob();

    // Create an Attachment record and insert it
    Attachment attachment = new Attachment(
        Name = 'file.docx',
        Body = fileContent,
        ContentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        ParentId = 'a3KDx000000TXxBMAW' // Replace with appropriate ParentId
    );
    insert attachment;
} else {
    // Handle error if the request fails
    System.debug('Failed to download the file. Status code: ' + response.getStatusCode());
}
In the code above, the Http class is used to send a GET request to the specified URL. The response is then checked for a successful status code (200) to ensure that the request was successful. If the request is successful, the file contents are retrieved as a Blob using response.getBodyAsBlob(). Then, an Attachment record is created with the appropriate properties and inserted into Salesforce. Make sure to replace 'a3KDx000000TXxBMAW' with the correct ParentId for the record you want to associate the attachment with.

Note that in the example above, the ContentType is set to 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' for a DOCX file. You may need to adjust the content type based on the file type you are downloading.

If this helps , please mark this as Best Answer.
Thank you.
Shubham Deshmukh 15Shubham Deshmukh 15

Hello Subrat,
This is not working, it is inserting file but that file nas nothing inside, showing as shown in attached imageUser-added image

 

Eden WheelerEden Wheeler
To download a DOCX file from a URL using Apex, you can use the following code:
String url = 'https://example.com/path/to/file.docx';
HttpRequest request = new HttpRequest();
request.setEndpoint(url);
request.setMethod('GET');

Http http = new Http();
HttpResponse response = http.send(request);

if (response.getStatusCode() == 200) {
    Attachment att = new Attachment(
        Name = 'file.docx',
        Body = response.getBodyAsBlob(),
        ContentType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        ParentId = 'a3KDx000000TXxBMAW'
    );
    insert att;
}

In this code, we create an HTTP request with the URL of the DOCX file and perform a GET request to retrieve its contents. If the response status code is 200 (indicating a successful request), we create a new Attachment record with the retrieved body, specify the correct content type (application/vnd.openxmlformats-officedocument.wordprocessingml.document for DOCX files), and set the appropriate parent record ID. Finally, we insert the Attachment record. for more infor: azure development course (https://www.igmguru.com/cloud-computing/microsoft-azure-developer-az-203-certification-training/)
Make sure to replace 'https://example.com/path/to/file.docx' with the actual URL of the DOCX file you want to download, and 'a3KDx000000TXxBMAW' with the ID of the parent record where you want to attach the file.