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
Intigration vIntigration 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');  
    }  
      }
 }
Best Answer chosen by Intigration v
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

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.
 
trigger ContentDocumentTriggerrrr on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
    list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
    for (ContentDocumentLink cdl : trigger.new ) {
        if(cdl.LinkedEntityId.getSObjectType() == Lead.sObjectType)
        setParentId.add(cdl.LinkedEntityId);        
    }
    system.debug('linkjid'+setParentId);
    if(setParentId.size()>0){
   Doclst= [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId  in :setParentId];
    system.debug('Doclst.size()'+Doclst.size());
    
    
      for (ContentDocumentLink cdl : trigger.new ) {
          if(Doclst.size()>0){
            cdl.adderror('document already exists');  
    }  
      }
    }
}
Thanks,

 

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you add the highlited line to your trigger and the issue would resolve.
 
trigger ContentDocumentTriggerrrr on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
    list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
    for (ContentDocumentLink cdl : trigger.new ) {
        if(cdl.LinkedEntityId.getSObjectType() == Lead.sObjectType)
        setParentId.add(cdl.LinkedEntityId);        
    }
    system.debug('linkjid'+setParentId);
   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');  
    }  
      }
}

If this solution helps, Please mark it as best answer.

Thanks,
 
CharuDuttCharuDutt
Hi Intigration 
Try Below Code
trigger ContentDocumentTrigger on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
    list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
    for (ContentDocumentLink cdl : trigger.new ) {
        if(cdl.LinkedEntityId.getSObjectType() == Lead.sObjectType)
        setParentId.add(cdl.LinkedEntityId);        
    }
    system.debug('linkjid'+setParentId);
   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');  
    }  
      }
}
Please Mark It As Best Answer if It Helps
Thank You!
Intigration vIntigration v
Hi Praveen,

am getting the below error when i run the code.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you let me know what is the error you are facing.

Thanks,
 
Intigration vIntigration v
Hi Praveen,

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".
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you add In operator in the soql query as below and check.
 
Doclst= [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId  in :setParentId];

Thanks,
​​​​​​​
Intigration vIntigration v
Hi Praveen,

Still getting the same error.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

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.
 
trigger ContentDocumentTriggerrrr on ContentDocumentLink (before insert) {
Set<Id> setParentId = new Set<Id>();
    list<ContentDocumentLink>Doclst= new list<ContentDocumentLink>();
    for (ContentDocumentLink cdl : trigger.new ) {
        if(cdl.LinkedEntityId.getSObjectType() == Lead.sObjectType)
        setParentId.add(cdl.LinkedEntityId);        
    }
    system.debug('linkjid'+setParentId);
    if(setParentId.size()>0){
   Doclst= [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId  in :setParentId];
    system.debug('Doclst.size()'+Doclst.size());
    
    
      for (ContentDocumentLink cdl : trigger.new ) {
          if(Doclst.size()>0){
            cdl.adderror('document already exists');  
    }  
      }
    }
}
Thanks,

 
This was selected as the best answer
Intigration vIntigration v
Thank you Praveen! Its working fine now