public with sharing class IFB_ALL_FileDownloadQueueable implements Queueable, Database.AllowsCallouts {
private final String preSignedUrl;
private final String fileName;
private final Id recordId;
private ContentDocumentLink conDocLink;
private Document_Detail__c docDetails;
private Integration_Message__c IntMsg;
public IFB_ALL_FileDownloadQueueable(String preSignedUrl, String fileName, Id recordId, ContentDocumentLink conDocLink, Document_Detail__c docDetails, Integration_Message__c IntMsg) {
this.preSignedUrl = preSignedUrl;
this.fileName = fileName;
this.recordId = recordId;
this.conDocLink = conDocLink;
this.docDetails = docDetails;
this.IntMsg = IntMsg;
}
public void execute(QueueableContext context) {
if (String.isBlank(preSignedUrl)) {
throw new IllegalArgumentException(‘preSignedUrl is required.’);
}
if (String.isBlank(fileName)) {
throw new IllegalArgumentException(‘fileName is required.’);
}
if (recordId == null) {
throw new IllegalArgumentException(‘recordId is required.’);
}
HttpRequest req = new HttpRequest();
req.setEndpoint(preSignedUrl);
req.setMethod(‘GET’);
req.setTimeout(120000);
Http http = new Http();
try {
HttpResponse res = http.send(req);
if (res.getStatusCode() != 200) {
throw new CalloutException(‘Failed to download file. Status: ‘ + res.getStatusCode() +’ – ‘ + res.getStatus());
}
ContentVersion cv = new ContentVersion();
cv.Title = fileName;
cv.PathOnClient = fileName;
cv.VersionData = res.getBodyAsBlob();
cv.Document_Name__c = docDetails.Document_Name__c;
cv.Document_Type__c = docDetails.Document_Type__c;
cv.Uploaded_by_API__c = false;
cv.Loan_Application__c = IntMsg.Reference_Id__c;
cv.Applicant__c = IntMsg.Applicant_Id__c;
cv.FileNet_Unique_Id__c = ((IntMsg.Document_Id__c).replace(‘{‘,”)).replace(‘}’,”);
if (recordId != null) {
if( conDocLink != null){
cv.ContentDocumentId=conDocLink.ContentDocumentId;
}
//cv.FirstPublishLocationId = recordId;
insert cv;
}
} catch (Exception ex) {
System.debug(LoggingLevel.ERROR,’File download failed. Error: ‘ + ex.getMessage());
throw ex;
}
}
/**
* Helper method to enqueue the job.
*/
public static Id enqueueJob(String preSignedUrl, String fileName, Id recordId, ContentDocumentLink conDocLink, Document_Detail__c docDetails, Integration_Message__c IntMsg) {
return System.enqueueJob(new IFB_ALL_FileDownloadQueueable(preSignedUrl, fileName, recordId, conDocLink, docDetails, IntMsg));
}
}
- none/plain text
- 1h