4

I wrote a trigger on ContentDocumentlink to get a count of number of files uploaded against Service_Request__c (Custom Object) record.It works fine for new files are uploaded on Service Request record.

But I want to get the count of number files already uploaded against Service_Request__c record .For that am running below code in developer console.

And am having difficulty in retrieving number of files using contentDocumentLink.

 list<ContentDocumentLink> contentDocumentLinkList = [SELECT id,linkedEntityId 
                        FROM ContentDocumentLink Using Scope Everything WHERE 
                         linkedEntityId IN :srIds];

 system.debug('contentDocumentLinkList Size'+contentDocumentLinkList.size());

returns only 350 records where as

list<ContentDocument> ContentDocumentList= [Select Id, Title,
                        FileExtension From ContentDocument USING SCOPE Team];

system.debug('ContentDocumentList size'+ContentDocumentList.size());

returns 4500 records.

All files in the org are related to Service_Request__c.I am not sure why query on ContentDocumentLink returns only 350.

If I try to update Query on ContentDocumentLink from Using Scope Everything to Using Scope Team then am getting

Internal Salesforce.com Error

Using Scope Everything and Using Scope Mine on ContentDocumentLink returns same number of records i.e 350.

Here is the code that am running in developer console.

Set<ID> srIds = new Set<ID>();

for(Service_Request__c sr : [select id,file_count__c from Service_Request__c] ) {
          sIds.add( sr.id );
}

map<id,Service_Request__c>srMap = new map<id,Service_Request__c>();

Schema.DescribeSObjectResult r = Service_Request__c.sObjectType.getDescribe();
String keyPrefix = r.getKeyPrefix();

srMap = new map<Id,Service_Request__c>([select Id,name,File_Count__c from Service_Request__c where Id in:srIds]);

list<ContentDocumentLink> contentDocumentLinkList = [SELECT id, linkedEntityId FROM ContentDocumentLink Using Scope Everything WHERE linkedEntityId IN :srIds];

system.debug('contentDocumentLinkList Size'+contentDocumentLinkList.size());

for(ContentDocumentLink a: contentDocumentLinkList)
        {

               if((String.valueOf(a.LinkedEntityId)).startsWith(keyPrefix))
                {
                   Service_Request__c sr = new Service_Request__c();
                   sr = srMap.get(a.LinkedEntityId);
                   if(sr.File_Count__c >0){
                   sr.File_Count__c = sr.File_Count__c + 1;
                   }
                   else{
                     sr.File_Count__c = 1;  
                   }
                }
        }


update srMap.values();

UPDATE:

Is it because of access issue ?. When I clicked on the file sharing settings against file then I see 'Any user of the Company' - 'No Access' and files are uploaded by Community Users.

  • What happens if you omit the USING SCOPE from the ContentDocumentLink SOQL query? Have you tried examining one of the ContentDocument that isn't coming back in the first result of 350 records? – Daniel Ballinger May 22 '17 at 2:06
  • @DanielBallinger I tried removing USING SCOPE from ContentDocumentLink query and am getting same result i.e 350 records – sfdc May 22 '17 at 2:20
  • How certain are you that there are more than 350 ContentDocumentLink records total in the org? How many records does the Storage Usage page under setup say there are? – Daniel Ballinger May 22 '17 at 2:32
  • I ran this query and got 4500 records .. select id,title from contentdocument using scope team – sfdc May 22 '17 at 2:50
  • So there are 350 Service_Request___c records and a corresponding number of ContentDocumentLink records connecting them to ContentDocument records. Is the problem now that you don't know what the remaining 4,150 ContentDocument records are for? – Daniel Ballinger May 22 '17 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.