You need to sign in to do that
Don't have an account?
Jeffrey 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.
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.
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
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.
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
You have to modify your code based requirement.
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"?