services/data/vXX.X/sobjects/ContentVersion
//Create Document
//Note: This example creates the content in Apex
//We could also load content via Data Loader.
ContentVersion cv = new ContentVersion();
cv.Title = 'Test Document';
cv.PathOnClient = 'TestDocument.pdf';
cv.VersionData = Blob.valueOf('Test Content');
cv.IsMajorVersion = true;
Insert cv;
//Get Content Documents
Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cv.Id].ContentDocumentId;
//Create ContentDocumentLink
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = conDocId;
//Label would contain the user id of the cloud integration user (obtained separately)
cdl.LinkedEntityId = Label.Platform_User_Id;
//V - Viewer permission. C - Collaborator permission. I - Inferred permission
cdl.ShareType = 'V';
Insert cdl;
CloudIntegrationUser.Le nom de l’utilisateur de l’intégration Cloud est cloud@ + ID de l’organisation à 18 caractères en minuscules. Voir la requête SOQL ci-dessous
SELECT Id, UserName from User WHERE username like 'cloud@00d%'
// Purposefully selecting another file NOT owned by the platform integration user
// (Hardcoded platform integration user Id - to be replaced with a more dynamic ID
// merge in the query)
let contentQuery = "SELECT Id
FROM ContentVersion
WHERE OwnerId != '0055f0000057ROYAA2'
ORDER BY CreatedDate DESC LIMIT 1";
let queryResults = await context.org.dataApi.query(contentQuery);
let contId = "";
for (const downItem of queryResults.records) {
contId = downItem.fields["Id"];
break;
}
// File Id for download
logger.info("contId: " + JSON.parse(JSON.stringify(contId)));
// Print
logger.info("DOWNLOAD LATEST FILE NOT OWNED BY FUNCTIONS USER");
await request
.get(
context.org.baseUrl +
"/services/data/v54.0/sobjects/ContentVersion/" +
contId +
"/VersionData"
)
.auth(null, null, true, context.org.dataApi.accessToken)
.on("error", function (err) {
logger.info("Exception: " + err);
})
.pipe(
fs.createWriteStream("./tempFiles/downFile.csv", { encoding: "utf8" })
)
.on("finish", doSomethingAfterDownload);
}
Platform_User_Id de l’exemple précédent qui est nécessaire pour la création du lien de document).trigger ContentVersionTrigger on ContentVersion (after insert) {
ContentVersionTriggerHelper.triggerHelper(
Trigger.operationType,
Trigger.new,
Trigger.oldMap);
}
public with sharing class ContentVersionTriggerHelper {
public static void triggerHelper(
System.TriggerOperation operationType,
List<ContentVersion> newList,
Map<Id, ContentVersion> oldMap
) {
switch on operationType {
when AFTER_INSERT{
List<ContentVersion> docsToProcess = getDocsToShare(newList);
if(!docsToProcess.isEmpty()){
shareWithIntegrationUser(docsToProcess);
}
}
}
}
public static List<ContentVersion> getDocsToShare(List<ContentVersion> allDocuments){
List<ContentVersion> docsToProcess = new List<ContentVersion>();
for(ContentVersion cVer : allDocuments){
if(cDoc.OwnerId != Label.Platform_User_Id){
docsToProcess.add(cVer);
}
}
return docsToProcess;
}
public static void shareWithIntegrationUser(List<ContentVersion> documents){
List<ContentDocumentLink> shareLinks = new List<ContentDocumentLink>();
for(ContentVersion cVer : documents){
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = cVer.ContentDocumentId;
cdl.LinkedEntityId = Label.Platform_User_Id;
cdl.ShareType = 'V';
shareLinks.add(cdl);
}
Database.insert(shareLinks,false);
}
}
000393095

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.