Thursday, 1 October 2020

ContentDocument and ContentDocumentLink trigger behavior in Classic and Lightning on delete trigger

Hi,

Let's have a look at how the triggers behave written on "ContentDocument and ContentDocumentLink" objects in Salesforce Classic and Salesforce Lightning.

Here we are going to discuss what is going to happen when we write a trigger for the "delete" event on the above objects.

In Classic:

ContentDocument triggers do not fire, as Salesforce only deletes the associated ContentDocumentLink record, not the ContentDocument record.

In Lightning Experience:

both the ContentDocument and related ContentDocumentLink records are deleted, and by design Salesforce only fires the trigger on ContentDocument, not the trigger on ContentDocumentLink.

This is working as designed and can be verified by following below steps:

1. Create two "before delete"  triggers: one on ContentDocument, and the other on ContenDocumentLink objects.

2. Place a "system.debug" statement in each which could be verified in the Debug logs.

3. Now upload 2 files to any Object record under 'Files' related list. Once done, you can observe both the uploaded documents under the 'Files' tab.

4. Execute the below queries in the Developer Console.

SELECT Id, LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId=<<Id og the document>>

2 records will be returned

SELECT Id, Title FROM ContentDocument WHERE Id=<<ContentDocumentId from the above query>>

2 rows will be returned

5. Set up the Debug logs

 IN CLASSIC: 

Delete one of the uploaded files, by clicking on the 'Del' link besides the document under 'Files' related list.

OBSERVATION:

In Debug logs you will see that only the ContentDocumentLinkTrigger has got fired and the Debug statement present in that Trigger will get displayed.

The document you have deleted will be available under the 'Files' tab

On executing the above 2 queries you will observe that only 1 row is returned for the 1st query and 2 rows for the second query. i.e. Only the ContentDocumentLink is getting removed.

 IN LIGHTNING:

Open the object record and delete the 2nd uploaded file, by clicking on the 'Del' link besides the document under 'Files' related list.

OBSERVATION:

In Debug logs you will see that only the ContentDocumentTrigger has got fired and the Debug statement present in that Trigger will get displayed.

The document you have deleted will no longer be available under the 'Files' tab

On executing the above 2 queries you will observe that no row is returned for the 1st query and 1 row for the second query (the one related to the 1st document). i.e. Both the ContentDocument and the ContentDocumentLink have got removed.

Note: 

We should remember if we are trying to write tirggers on "delete" event on these objects.

This content is from following Salesforce Link.

Reference:

https://help.salesforce.com/articleView?id=000312746&language=en_US&type=1&mode=1


1 comment:

  1. ETG is a Salesforce Commerce Cloud Implementation Partner. For 20 years, ETG Global Services, Inc. has successfully implemented eCommerce solutions for growth and enterprise clients.

    ReplyDelete

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...