+ Start a Discussion
davidleckenbydavidleckenby 

Cannot pull data into a list from a SOQL query referencing a set variable. Hardcoding the value works fine

public class ELOUpload_Class {
   
    public static void ELOUpload_Method1 (List<ContentDocument> lstContentDoc) {
      
       //Create a set of all the incoming IDs
        Set<Id> contDocId = new Set<Id> (); 
            
        for (ContentDocument contDoc : lstContentDoc ) {
            
           contDocId.add(contDoc.Id);
          
        }
        system.debug('contDocId = ' + contDocId );  
        
        List <ContentDocumentLink> contDoclink = [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId = '0692O0000003CSTQA2']; 
        
        system.debug('contDoclink   = ' + contDoclink );      

    }
}
Hi Guys - I am working with ContentFiles and am trying to get a list to populate with some values from the ContentDocumentLink object. The code above (with hardcoded value) works fine and returns the values. However when trying to do it referencing the set variable (contDocId) in the code below I get no values returned. The contDocId is being pulled correctly too (which is the hardcoded value).
public class ELOUpload_Class {
   
    public static void ELOUpload_Method1 (List<ContentDocument> lstContentDoc) {
      
       //Create a set of all the incoming IDs
        Set<Id> contDocId = new Set<Id> (); 
            
        for (ContentDocument contDoc : lstContentDoc ) {
            
           contDocId.add(contDoc.Id);
          
        }
        system.debug('contDocId = ' + contDocId );  
        
        List <ContentDocumentLink> contDoclink = [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN :contDocId]; 
        
        system.debug('contDoclink   = ' + contDoclink );      

    }
}
Now sure how to proceed? - many thanks for any assistance


 
Abdulla d 5Abdulla d 5
Hi david,
Please check whether lstContentDoc is geetting data or not, if not go and check whether the query returns the data or not.
code looks everything is ok.
 public static void ELOUpload_Method1 (List<ContentDocument> lstContentDoc) {
System.debug('datat===>'+lstContentDoc );
davidleckenbydavidleckenby
Thanks Abdulla - 'lstContentDoc' is essentially getting the Trigger.new value passed from a simple Trigger and as the set 'contDocId' (system.debug('contDocId = ' + contDocId ) is being pulled through and displaying ok it seems to be fine?
Abdulla d 5Abdulla d 5
if it is trigger please make sure that it's not before insert, because in before insert we are not getting any ids.
So you should use the after insert.
Abdulla d 5Abdulla d 5
It is helpful to analyze if u can post the trigger too.
davidleckenbydavidleckenby
Here is the trigger
trigger ELOUpload_Trigger on ContentDocument (after insert) {
     ELOUpload_Class.ELOUpload_Method1 (Trigger.new);               
  }

thanks
Abdulla d 5Abdulla d 5
Hi David, This one will work, because after the content document inserted, in databsae it will take some time to create the contentDocumentLink.
so, immediately resource is not available, to use the resources we should use the future method. I hope this one will help you.


public class ELOUpload_Class {   
    public static void ELOUpload_Method1 (List<ContentDocument> lstContentDoc) {      
       //Create a set of all the incoming IDs
        Set<Id> contDocId = new Set<Id> ();  
        
        for (ContentDocument contDoc : lstContentDoc ) {            
           contDocId.add(contDoc.Id);          
        }            
        getDataOf(contDocId);
    }
    @future
    public static void getDataOf(Set<id> contDocId){
        List <ContentDocumentLink> contDoclink = new  List <ContentDocumentLink> ();
        system.debug('contDocId = ' + contDocId );          
        contDoclink = [SELECT Id, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId IN :contDocId];         
        system.debug('contDoclink   = ' + contDoclink );
    }
}