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
Øyvind Borgersen 10Øyvind Borgersen 10 

Trigger to check if file is attached

Hi!
I have a scenario where I need to check if a file has been added to a record. Does anyone have a sample code for this?

If there's an file attachment to the record, I'll update a checkbox.
Best Answer chosen by Øyvind Borgersen 10
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Use below code:
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) {
    
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    
    Set<ID> parentIds = new Set<ID>();
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    
    for (List<Account> account : [SELECT Id, (SELECT Id FROM ContentDocumentLinks LIMIT 1) 
                                  FROM Account WHERE Id IN :parentIds]) {
        
        for (Account acc : account) {
            acc.Cb__c = ( acc.ContentDocumentLinks.size() > 0 );
        }
        UPDATE account;   
    }   
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
trigger updateFieldAttachment on Attachment (after insert) {

Id pId;

for(Attachment att: Trigger.new){
pId=att.ParentId;
}

List<Account> acc = [SELECT Id , Checkbox__c FROM Account WHERE Id=:pId];

//assuming one record is fetched.
acc[0].Checkbox__c = true;

UPDATE acc[0];

}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Øyvind Borgersen 10Øyvind Borgersen 10
Hi Khan,

Thanks, but I'm looking for similar feature but towards files and not attachment. Do you have a sample of that as well?
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Use below code:
trigger ContentDocumentLinkTrigger on ContentDocumentLink ( after insert, after update, after delete ) {
    
    List<ContentDocumentLink> cdls = ( Trigger.new == null ? Trigger.old : Trigger.new );
    
    Set<ID> parentIds = new Set<ID>();
    
    for (ContentDocumentLink cdl : cdls) {
        parentIds.add( cdl.LinkedEntityId );
    }
    
    for (List<Account> account : [SELECT Id, (SELECT Id FROM ContentDocumentLinks LIMIT 1) 
                                  FROM Account WHERE Id IN :parentIds]) {
        
        for (Account acc : account) {
            acc.Cb__c = ( acc.ContentDocumentLinks.size() > 0 );
        }
        UPDATE account;   
    }   
}

I hope it helps you.

Kindly mark this as solved if the information was helpful. It will help to keep this community clean.

Regards,
Khan Anas
This was selected as the best answer
Megan Callaghan 1Megan Callaghan 1
This is good -- what about unchecking the box? I have a use case where I want to post to chatter every time an attachment/file is uploaded to a case so that I can alert my case agent/owner. I was heading down this path.
thoughts?