+ Start a Discussion
Michael MMichael M 

Help with SOQL query in Trigger

My use case is that I have a picklist on a custom object, and one of the values on the picklist is "files uploaded". So I need to build an 'after insert' trigger that when a file is uploaded to this object (not any other objects), the value of that picklist automatically gets updated to "files uploaded". So 2 questions:
1. What Object should the trigger be built on?
2. How would the SOQL query be written to say that it's only for files related to this one specific custom object's records?

Any help is greatly appreciated. 
Best Answer chosen by Michael M
SarvaniSarvani
Hi Michael,

I am assuming you are trying to upload files on the custom object using "Upload file' functionality on files related list. If thats the case the trigger would be on "ContentDocumentlink" Object. When you upload a file to custom object record the value in LinkedEntityId is the record to which the file is uploaded to.

Below SOQL query gives you all the files linked to the Custom Object.
SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Custom_Object__c ) 

Please refer to sample codes below on Account Object. Modify the code as per your requirement on custom object. In  below example when a new file is uploaded to account object record the "CustomField__c " field is updated with File Uploaded text. When you try to update your picklist make sure you give correct API names to the field. To identify only files uploaded to Account object we are using String.valueOf(nt.LinkedEntityId ).startsWith('001'). As the Account record Id starts with 001, same way give your custom record id start digits.

Trigger code:
trigger TestOnFIle on ContentDocumentLink (before insert) {
     UpdateAccwithLastNoteClass.Method(Trigger.new);

}

Apex Class:
public class UpdateAccwithLastNoteClass {
    
    public static void Method(list<contentdocumentlink> Newfiles){
       
        set<id> Accountids=new set<id>();
        list<Account> Accountstobeupdated=new list<Account>();
    
         for(contentdocumentlink nt : Newfiles){
// Replace your Custom object id starting 3 digits
            if(String.valueOf(nt.LinkedEntityId ).startsWith('001')){
			
			Accountids.add(nt.LinkedEntityId );
                
           }   
        }
        if(Accountids.size()>0){
        Accountstobeupdated=[Select id,CustomField__c from Account where id in :Accountids];
        }
        
        
        for(contentdocumentlink nt : Newfiles){
	       for(integer i=0;i< Accountstobeupdated.size();i++)
	        {
                if(nt.LinkedEntityId ==Accountstobeupdated.get(i).id)
                {
                Accountstobeupdated.get(i).CustomField__c='File Uploaded';
				}
			}
	  }

 
    if(!Accountstobeupdated.isEmpty()){
        try{
            update Accountstobeupdated;
        }catch(DmlException de ){
            System.debug(de);
        }
    } 
        
        
    }

}

Hope this helps! Please mark as best if it does

Thanks

All Answers

SarvaniSarvani
Hi Michael,

I am assuming you are trying to upload files on the custom object using "Upload file' functionality on files related list. If thats the case the trigger would be on "ContentDocumentlink" Object. When you upload a file to custom object record the value in LinkedEntityId is the record to which the file is uploaded to.

Below SOQL query gives you all the files linked to the Custom Object.
SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Custom_Object__c ) 

Please refer to sample codes below on Account Object. Modify the code as per your requirement on custom object. In  below example when a new file is uploaded to account object record the "CustomField__c " field is updated with File Uploaded text. When you try to update your picklist make sure you give correct API names to the field. To identify only files uploaded to Account object we are using String.valueOf(nt.LinkedEntityId ).startsWith('001'). As the Account record Id starts with 001, same way give your custom record id start digits.

Trigger code:
trigger TestOnFIle on ContentDocumentLink (before insert) {
     UpdateAccwithLastNoteClass.Method(Trigger.new);

}

Apex Class:
public class UpdateAccwithLastNoteClass {
    
    public static void Method(list<contentdocumentlink> Newfiles){
       
        set<id> Accountids=new set<id>();
        list<Account> Accountstobeupdated=new list<Account>();
    
         for(contentdocumentlink nt : Newfiles){
// Replace your Custom object id starting 3 digits
            if(String.valueOf(nt.LinkedEntityId ).startsWith('001')){
			
			Accountids.add(nt.LinkedEntityId );
                
           }   
        }
        if(Accountids.size()>0){
        Accountstobeupdated=[Select id,CustomField__c from Account where id in :Accountids];
        }
        
        
        for(contentdocumentlink nt : Newfiles){
	       for(integer i=0;i< Accountstobeupdated.size();i++)
	        {
                if(nt.LinkedEntityId ==Accountstobeupdated.get(i).id)
                {
                Accountstobeupdated.get(i).CustomField__c='File Uploaded';
				}
			}
	  }

 
    if(!Accountstobeupdated.isEmpty()){
        try{
            update Accountstobeupdated;
        }catch(DmlException de ){
            System.debug(de);
        }
    } 
        
        
    }

}

Hope this helps! Please mark as best if it does

Thanks
This was selected as the best answer
Michael MMichael M
Thank you, Sarvani!