Skip to main content
added 1 character in body
Source Link
ZAR
  • 2k
  • 5
  • 25
  • 41

I think I found a simpler solution: add the Apex user to the contentdocument..

Here is the code:

//First, get all the CDLs for each object
ContentDocumentLink[] cdls = [SELECT
                                contentdocumentid,
                                linkedentity.name, 
                                linkedentityid,
                                linkedentity.type 
                              FROM ContentDocumentLink 
                              WHERE linkedentityid in (SELECT id FROM User)];
cdls.addAll([SELECT
                contentdocumentid,
                linkedentity.name, 
                linkedentityid,
                linkedentity.type 
             FROM ContentDocumentLink 
             WHERE linkedentityid in (SELECT id FROM Contact)]);

//...// repeat for each object

//Get unique document ids (since you can have multiple CDLs for a single Doc)
set<id> docIds = new set<id>();
for(ContentDocumentLink cdl : cdls){
    docIds.add(cdl.contentdocumentid);
}

//get apex user id
id apexUser = UserInfo.getUserId();

//create new CDLs
ContentDocumentLink[] newCdls = new list<ContentDocumentLink>();
for(ContentDocumentLinkid cdldocId : cdlsdocIds){
  ContentDocumentLink newCdl = new ContentDocumentLink(
        ContentDocumentId='0690W000003iLIwQAM'ContentDocumentId=docId, 
        linkedentityid=apexUser, 
        Visibility='SharedUsers',
        ShareType='C');
  newCdls.add(newCdl);
}
insert newCdls;

//check new results
ContentDocument[] Cds = [SELECT id FROM ContentDocument];

//Profit

I think I found a simpler solution: add the Apex user to the contentdocument..

Here is the code:

//First, get all the CDLs for each object
ContentDocumentLink[] cdls = [SELECT
                                contentdocumentid,
                                linkedentity.name, 
                                linkedentityid,
                                linkedentity.type 
                              FROM ContentDocumentLink 
                              WHERE linkedentityid in (SELECT id FROM User)];
cdls.addAll([SELECT
                contentdocumentid,
                linkedentity.name, 
                linkedentityid,
                linkedentity.type 
             FROM ContentDocumentLink 
             WHERE linkedentityid in (SELECT id FROM Contact)]);

//...// repeat for each object

//Get unique document ids (since you can have multiple CDLs for a single Doc)
set<id> docIds = new set<id>();
for(ContentDocumentLink cdl : cdls){
    docIds.add(cdl.contentdocumentid);
}

//get apex user id
id apexUser = UserInfo.getUserId();

//create new CDLs
ContentDocumentLink[] newCdls = new list<ContentDocumentLink>();
for(ContentDocumentLink cdl : cdls){
  ContentDocumentLink newCdl = new ContentDocumentLink(
        ContentDocumentId='0690W000003iLIwQAM', 
        linkedentityid=apexUser, 
        Visibility='SharedUsers',
        ShareType='C');
  newCdls.add(newCdl);
}
insert newCdls;

//check new results
ContentDocument[] Cds = [SELECT id FROM ContentDocument];

//Profit

I think I found a simpler solution: add the Apex user to the contentdocument..

Here is the code:

//First, get all the CDLs for each object
ContentDocumentLink[] cdls = [SELECT
                                contentdocumentid,
                                linkedentity.name, 
                                linkedentityid,
                                linkedentity.type 
                              FROM ContentDocumentLink 
                              WHERE linkedentityid in (SELECT id FROM User)];
cdls.addAll([SELECT
                contentdocumentid,
                linkedentity.name, 
                linkedentityid,
                linkedentity.type 
             FROM ContentDocumentLink 
             WHERE linkedentityid in (SELECT id FROM Contact)]);

//...// repeat for each object

//Get unique document ids (since you can have multiple CDLs for a single Doc)
set<id> docIds = new set<id>();
for(ContentDocumentLink cdl : cdls){
    docIds.add(cdl.contentdocumentid);
}

//get apex user id
id apexUser = UserInfo.getUserId();

//create new CDLs
ContentDocumentLink[] newCdls = new list<ContentDocumentLink>();
for(id docId : docIds){
  ContentDocumentLink newCdl = new ContentDocumentLink(
        ContentDocumentId=docId, 
        linkedentityid=apexUser, 
        Visibility='SharedUsers',
        ShareType='C');
  newCdls.add(newCdl);
}
insert newCdls;

//check new results
ContentDocument[] Cds = [SELECT id FROM ContentDocument];

//Profit
Source Link
ZAR
  • 2k
  • 5
  • 25
  • 41

I think I found a simpler solution: add the Apex user to the contentdocument..

Here is the code:

//First, get all the CDLs for each object
ContentDocumentLink[] cdls = [SELECT
                                contentdocumentid,
                                linkedentity.name, 
                                linkedentityid,
                                linkedentity.type 
                              FROM ContentDocumentLink 
                              WHERE linkedentityid in (SELECT id FROM User)];
cdls.addAll([SELECT
                contentdocumentid,
                linkedentity.name, 
                linkedentityid,
                linkedentity.type 
             FROM ContentDocumentLink 
             WHERE linkedentityid in (SELECT id FROM Contact)]);

//...// repeat for each object

//Get unique document ids (since you can have multiple CDLs for a single Doc)
set<id> docIds = new set<id>();
for(ContentDocumentLink cdl : cdls){
    docIds.add(cdl.contentdocumentid);
}

//get apex user id
id apexUser = UserInfo.getUserId();

//create new CDLs
ContentDocumentLink[] newCdls = new list<ContentDocumentLink>();
for(ContentDocumentLink cdl : cdls){
  ContentDocumentLink newCdl = new ContentDocumentLink(
        ContentDocumentId='0690W000003iLIwQAM', 
        linkedentityid=apexUser, 
        Visibility='SharedUsers',
        ShareType='C');
  newCdls.add(newCdl);
}
insert newCdls;

//check new results
ContentDocument[] Cds = [SELECT id FROM ContentDocument];

//Profit