0

I am trying to get the record type name of the related/linked entity id in the trigger of the Content Document Link like below

trigger setVisibilityOnNotesinCase on ContentDocumentLink (before insert) {
    for(ContentDocumentLink related_rec :Trigger.new) {
        if((related_rec.LinkedEntityId.getSObjectType() == Case.sObjectType) && (related_rec.LinkedEntityId.recordType.Name == 'Support' )) {
            related_rec.Visibility  = 'SharedUsers';
        }
    }}

But I get error Variable recordType doesnot exist I tried to change like

Id caseRecordType = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Support').getRecordTypeId();
if((related_rec.LinkedEntityId.getSObjectType() == Case.sObjectType) && (related_rec.LinkedEntityId.RecordTypeId  == caseRecordType )) 

I get A non foreign key field cannot be referenced in a path expression: LinkedEntityId is there any way get the recod type name/id of the linked/related record in the trigger without writing the query/apex

I tried changing the for loop to query the LinkedEntityId like

for(ContentDocumentLink c : [SELECT Id, LinkedEntityId FROM ContentDocumentLink WHERE Id IN :Trigger.new]) 

This throws error like

Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.

3
  • 1
    I've reopened this question because it's no longer a duplicate. – David Reed Oct 7 '20 at 16:50
  • 1
    @DavidReed looks like one that would be answered by that proposed canonical Q&A I created on meta... :) – Phil W Oct 7 '20 at 17:23
  • @user81642 take a look at "I just need data from directly related records but I'm in a trigger" in this proposed answer and let me know if that solves your problem. – Phil W Oct 7 '20 at 17:27
1

To use related record information you must query it

The most simple solution toyour problem is:

  • iterate (make a loop )on the trigger new, put the Id from the current Linked Entity records on a set/list and use it to query on your object, using a map to be able to reference it later.
  • iterate the trigger new, gething the information from result query, checking record types, apply tour logic as needed.

A personal consideration/opinion

  • Cast your trigger at the beginning of the code, so it will be easier to get field values.
  • Use Record Type DeveloperName
Set<id> stCaseIds = new Set<id>();

for(ContentDocumentLink related_rec :trgNew){
    if(related_rec.LinkedEntityId.getSObjectType() == Case.sObjectType){
        stCaseIds.add(related_rec.LinkedEntityId);
    }
}
if(!stCaseIds.isEmpty()){
    Map<Id, Case> mpCase = new Map<Id, Case>([SELECT Id, RecordType.DeveloperName, RecordType.Name FROM Case WHERE Id IN: stCaseIds]);
    for(ContentDocumentLink cdl : trgNew)){
        if(mpCase.get(cdl.LinkedEntityId).DeveloperName == 'Support'){
            cdl..Visibility  = 'SharedUsers';
        }
    }
}

Content document Link requires that you use a ContentDocumentId or its own Id as filter tha is why you are getting this error

Ps. This may not be the most performatic way, but it's the most simple to understand

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.