0

I am trying to increase test coverage of an after-trigger on ContentDocumentLink. The problem is that the trigger gets fired twice. I found out from this answer that the trigger fires one extra time because the system automatically inserts a ContentDocumentLink to share the ContentDocument to its owner. Due to this, the running User Id is assigned to ContentDocumentLink.LinkedEntityId. This generates error ahead in the trigger. The trigger needs ContentDocumentLink.LinkedEntityId to be a Case Id only.

Trigger Code:

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert, after update) {
    list<ContentDocumentLink> ContentDocumentLinklist = Trigger.new;
    String ContentDocumentLinklistStr = JSON.serialize(ContentDocumentLinklist[0]);
    CalloutService.postDocuments(ContentDocumentLinklistStr);//This method needs ContentDocumentLink.LinkedEntityId to be a Case Id only
}

Test Class Code: I am referring to this site for test class code.

Is there a way by which I can stop the first trigger execution?

How can I make sure in the trigger that the method CalloutService.postDocuments() is called only when ContentDocumentLink.LinkedEntityId is a Case Id? Please guide. Thank You!

1 Answer 1

1

In your trigger code you will have to put a guard above the line CalloutService.postDocuments(ContentDocumentLinklistStr); so that it fires only when the linkedEntityId is a caseId.

You can write an if statement like this.

if(ContentDocumentLinklist[0].linkedEntityId.startsWith('500') {
    String ContentDocumentLinklistStr = JSON.serialize(ContentDocumentLinklist[0]);
    CalloutService.postDocuments(ContentDocumentLinklistStr);
}

I would recommend that you bulkify the trigger code.

You must log in to answer this question.

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