function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DrebinDrebin 

Trigger to delete a chatter file when the status of a lead change

Hi everyone,

I realized an app which take photos and create leads with it. I save the image in a ContentVersion. To show it in my new lead, i created two custom fields, one which store the ContentVersion ID of the image and another one with the formula to display the image in the lead detail with the following formula : IMAGE('/sfc/servlet.shepherd/version/download/' & PhotoLead_Image__c, "Chatter Image", 250, 250).

Now, i need that when the status of a lead changes to "Converted", the image corresponding to this lead is deleted.

I tried to create a trigger for this but I'm completely blocked.

Can you help please me with this trigger ?

Thanks.
Best Answer chosen by Drebin
DrebinDrebin
Hi SidhantAgarwal,

Thanks for your reply.
In the meantime, I managed to find an answer to my question, which I share the solution :
List<ContentDocument> myFeed = [SELECT LatestPublishedVersionId FROM ContentDocument WHERE Id In (SELECT ContentDocumentId FROM ContentVersion WHERE Id =:l.PhotoLead_Image__c)];
delete myFeed;
As you stated, I had to get the ContentDocumentID and not the ContentVersionId.

All Answers

SidhantSidhant

Hi Frederic,

How are you storing the ContentVersion ID to the field? You need to save the ContentDocument Id, as Delete DML operation is not allowed on ContentDocument as per the SOAP API developer guide: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentversion.htm.

So, you need to save the ContentDocument Id(069) instead of ContentVersionId (068). 

Reply if you need help with trigger logic.
Thanks.

Kindly mark it as an answer if it works so that we can close the thread.

DrebinDrebin
Hi SidhantAgarwal,

Thanks for your reply.
In the meantime, I managed to find an answer to my question, which I share the solution :
List<ContentDocument> myFeed = [SELECT LatestPublishedVersionId FROM ContentDocument WHERE Id In (SELECT ContentDocumentId FROM ContentVersion WHERE Id =:l.PhotoLead_Image__c)];
delete myFeed;
As you stated, I had to get the ContentDocumentID and not the ContentVersionId.
This was selected as the best answer