I come to ask for help to solve this problem I'm having with a flow. I have the custom Object "EnsaiosdeDM" that has a standard related list, what I need is that when a document is added to this list, a checkbox field that I created is checked, and when this list is empty, this field is unchecked. First I created a flow, which would do this, but it doesn't recognize the insertion of a document in the related list as an update, so it doesn't trigger the flow. Then I created a trigger:
trigger HasAttachment on ContentDocumentLink (after insert, before delete) {
String tempParentId;
Set<Id> setParentId = new Set<Id>();
List<Ensaios_de_DM__c> Essayolst = new List<Essaios_de_DM__c>();
if(trigger.isBefore){
for (ContentDocumentLink cdl : trigger.old ) {
tempParentId = cdl.LinkedEntityId;
if (tempParentId.left(3) =='a1f') {
System.debug('Debug : found a1f');
System.debug('Debug : content document id ' + cdl.ContentDocumentId );
setParentId.add(cdl.LinkedEntityId);
}
}
Essayolst = [select Id , Has_Annex__c from Essays_de_DM__c where Id IN :setParentId];
For(Ensaios_de_DM__c and : Essayolst)
{
e.Tem_Annexo__c = false;
}
update Essayolst;
}
else{
for (ContentDocumentLink cdl : trigger.new ) {
tempParentId = cdl.LinkedEntityId;
if (tempParentId.left(3) =='a1f') {
System.debug('Debug : found a1f');
System.debug('Debug : content document id ' + cdl.ContentDocumentId );
setParentId.add(cdl.LinkedEntityId);
}
}
Essayolst = [select Id , Has_Annex__c from Essays_de_DM__c where Id IN :setParentId];
For(Ensaios_de_DM__c and : Essayolst)
{
e.Tem_Anexo__c = true;
}
update Essayolst;
}
}
It's working only to check the checkbox, but it doesn't uncheck when the related list is empty... does anyone know what I can do? If you know how to solve with flow I would also like to know lol