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
Jeffrey UyJeffrey Uy 

Apex trigger when a file is attached to an account record with file name starting with DA

Hello everyone.
I am not a Salesforce developer but have some basic knowledge on SOQL and Apex Triggers.  I need help from someone on an Apex trigger.  We'd like to update a checkbox field on Account object when there is a File that starts with "DA" that is added/attached to the specific account record.  is this doable?  any help will be greatly appreciated.  thank you.
Best Answer chosen by Jeffrey Uy
AbhishekAbhishek (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;   
    }   
}


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.

Thanks.

All Answers

AbhishekAbhishek (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;   
    }   
}


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.

Thanks.
This was selected as the best answer
SUCHARITA MONDALSUCHARITA MONDAL

Hi Jeffrey,

You need to query on ContentDocumentLink and check LinkedEntityType field for AccountId. Based on the result you can update you checkbox value on Account object.

[SELECT ContentDocument.title FROM ContentDocumentLink WHERE LinkedEntityId = '//Account Id'];

Hope this will give you some idea!

Thanks,
Sucharita

Jeffrey UyJeffrey Uy
Abhishek, I used your code and there were no syntax errors.  However, I get an error when uploading/adding a file to the account.  It says "can't add file to the account".  am I missing something?
AbhishekAbhishek (Salesforce Developers) 
Jeffery,

You have to modify your code based requirement.
Jeffrey UyJeffrey Uy
Abhishek, sorry but where in the code should I change?  the only thing I changed was the custom field to match what I have.
Jeffrey UyJeffrey Uy

Nevermind.,.. the error was due to a Process Builder not on your code.  Now, where in the code do I add that it will only trigger if the file name starts with "DA"?