You need to sign in to do that
Don't have an account?
Intigration v
Error message should be displayed when there is already file attached to Lead object.
Hi,
can someone please help me to resolve the issue.An error message should be displayed when there is already file attached to Lead object.
I have written trigger to display error message but it is giving error when am trying to upload the file first time as well.
It should display the message only there is already document uploaded to that Lead record.
trigger ContentDocumentTrigger on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
for (ContentDocumentLink cdl : trigger.new ) {
setParentId.add(cdl.LinkedEntityId);
}
Doclst= [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:setParentId];
system.debug('Doclst.size()'+Doclst.size());
for (ContentDocumentLink cdl : trigger.new ) {
if(Doclst.size()>0){
cdl.adderror('document already exists');
}
}
}
can someone please help me to resolve the issue.An error message should be displayed when there is already file attached to Lead object.
I have written trigger to display error message but it is giving error when am trying to upload the file first time as well.
It should display the message only there is already document uploaded to that Lead record.
trigger ContentDocumentTrigger on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
for (ContentDocumentLink cdl : trigger.new ) {
setParentId.add(cdl.LinkedEntityId);
}
Doclst= [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =:setParentId];
system.debug('Doclst.size()'+Doclst.size());
for (ContentDocumentLink cdl : trigger.new ) {
if(Doclst.size()>0){
cdl.adderror('document already exists');
}
}
}
Yeah got the issue. You need to check if the set is non empty then only query the content document i guess.
Can you check the below code.
Thanks,
All Answers
Can you add the highlited line to your trigger and the issue would resolve.
If this solution helps, Please mark it as best answer.
Thanks,
Try Below Code Please Mark It As Best Answer if It Helps
Thank You!
am getting the below error when i run the code.
Can you let me know what is the error you are facing.
Thanks,
am getting the below error when i run the code.
Error: There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger ContentDocumentTriggerrrr caused an unexpected exception, contact your administrator: ContentDocumentTriggerrrr: execution of BeforeInsert caused by: System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.: Trigger.ContentDocumentTriggerrrr: line 10, column 1".
Can you add In operator in the soql query as below and check.
Thanks,
Still getting the same error.
Yeah got the issue. You need to check if the set is non empty then only query the content document i guess.
Can you check the below code.
Thanks,