I have created a rest API that returns the list of opportunities as well as files attached to the opportunities. I am able to call the API and retrieve the data via POSTMAN. This is the code:
@RestResource(urlMapping='/api/getOpportunities')
global with sharing class GetOpportunities {
@HttpGet
global static List<sObject> doGet() {
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
List<sObject> result=new List<sObject>();
List<sObject> opps = [
Select Id, Name, ........
FROM Opportunity
WHERE RecordType.Name = 'Test' AND
StageName = 'XYZ'
];
Set<Id> contentDocIds = new Set<Id>();
for (ContentDocumentLink cdl : [
SELECT id, LinkedEntityId, ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId IN (
SELECT Id
FROM Opportunity
WHERE StageName = 'XYZ'
)
]) {
contentDocIds.add(cdl.ContentDocumentId);
}
List<sObject> cvList = [
select id, Checksum, ContentBodyId,
ContentDocumentId, ContentLocation, ContentModifiedById,
ContentModifiedDate, ContentSize, ContentUrl, Description,
ExternalDataSourceId, ExternalDocumentInfo1,
ExternalDocumentInfo2, FeaturedContentBoost, FeaturedContentDate,
FileExtension, FileType, FirstPublishLocationId, IsAssetEnabled,
IsLatest,IsMajorVersion, NegativeRatingCount, Origin, OwnerId, PathOnClient,
PositiveRatingCount, PublishStatus, RatingCount, ReasonForChange,
SharingOption,SharingPrivacy, TagCsv, TextPreview, Title, VersionNumber,
VersionData
FROM ContentVersion
WHERE ContentDocumentId IN :contentDocIds
];
result.addAll(opps);
result.addAll(cvList);
return result;
}
}
Question:
The versiondata gives me some link right now e.g. /services/data/v47.0/sobjects/ContentVersion/0686C0000000i9OQAQ/VersionData
How do I convert it to actual content? I need the actual file e.g. pdf, ppt, jpg e.t.c.
Thanks