0

I have a requirement to check the record type of a custom object being iterated over in a for loop (ContentDocumentLink) as part of an after insert trigger.

I have tried the following, assign Id variable to hold record type Id

Id RTId = Schema.SObjectType.Custom_Object__c.getRecordTypeInfosByName().get('Record Type Name').getRecordTypeId();

However, I am confused as to how to access the record type Id of the object instance in the for loop for when I do a conditional check in an if statement:

  if( //Some Condition && **cd.LinkedEntityId.getRecordTypeId() !== RTId**)
        {
            PAIds.add(cd.LinkedEntityId);
            documentIds.add(cd.Id);

        }

I keep getting various compile errors depending on what I try, most recently "Method does not exist or incorrect signature: void getRecordTypeId() from the type Id" I'm sure it's some simple ignorance on my part of how to access from for loop object 'cd'.

2
  • Have you tried cd.LinkedEntityId.RecordTypeId or cd.LinkedEntity.RecordTypeIdin your if statement? I think one of these should work even though the LinkedEntityId is polymorphic. Remember that RecordTypeId is a standard field on any object that allows record types. getRecordTypeId()` definitely will not work because it is only for RecordTypeInfo. Commented Aug 26 at 17:58
  • 1
    Yes I have tried cd.LinkedEntityId.RecordTypeId, upon compile I get the error: "A non foreign key field cannot be referenced in a path expression: LinkedEntityId" Commented Aug 26 at 18:01

1 Answer 1

1

Since the system won't allow you traverse the LinkedEntityId relationship, you have to take several steps to do what you want.

You must loop through the ContentDocumentLink records to collect the LinkedEntityId values, do a query on the custom object filtering on the record type ID, and put those object IDs into a second collection. Then in your ContentDocumentLink loop, you can compare the LinkedEntity ID to your second collection because those are of the the desired record type.

1
  • Thank you this worked - I already had the parts in place and they just needed some adjustment. Commented Aug 29 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .