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
George Laird 39George Laird 39 

Map Question

I'm trying to create a class based on files, which is ContentDocument object.  I'm trying to create a map where the key is the ID of the ContentDocument and the value is a list of LinkedEntityId that are related to the key.  

I'm not sure how to do this.  I took a shot here but I don't think it's correct.  

I'm thinking that the key is correct, but I don't know how to get the values for each key.  Each key will have a few Ids for the value. 


***I'm passing a list called 'docs' from a trigger.  The list is essentially all of the docs that were passed from trigger.new


  public static void ProcessFiles(List<ContentDocument> docs){
        
Map<Id,List<ContentDocumentLink>> FilesMap = New Map<Id,List<ContentDocumentLink>>();
 
List<ContentDocumentLink> l = [SELECT LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN : docs];


 
        for(ContentDocument c : docs){
            FilesMap.put(c.Id,l);
                
        }
        
    }  



Any help would be awesome!
Best Answer chosen by George Laird 39
Deepali KulshresthaDeepali Kulshrestha
Hi George,

Greetings to you!

This code will help you to create map correctly.
public static void ProcessFiles(List<ContentDocument> docs){   
Map<Id,List<ContentDocumentLink>> ContentIdVSContentDocListMap = new ap<Id,List<ContentDocumentLink>>();
List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN : docs];
for(ContentDocumentLink cdl : cdlList){
if(!ContentIdVSContentDocListMap.containsKey(cdl.ContentDocumentId)){
    ContentIdVSContentDocListMap.put(cdl.ContentDocumentId, new List<ContentDocumentLink>());
    }
    ContentIdVSContentDocListMap.get(cdl.ContentDocumentId).add(cdl); 
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Rounak SharmaRounak Sharma
Hi George,
Please try out this way

public class ContactUpdateHandler {
public static void updateContact(List<Account> accounts) {
Map < Id, Account > mapAccount = new Map < Id, Account >();
List<Contact> listContact = new List<Contact>();
for(Account acct : accounts)
mapAccount.put(acct.Id, acct);
listContact = [ SELECT MailingStreet, MailingCity, AccountId FROM Contact WHERE AccountId IN : mapAccount.keySet() ];
if ( listContact.size() > 0 ) {
for ( Contact con : listContact ) {
con.MailingStreet = mapAccount.get(con.AccountId).BillingStreet;
con.MailingCity = mapAccount.get(con.AccountId).BillingCity;
}
update listContact;
} } }

Try to follow the similar approach for your map condition.
Please let me know if still having doubt
thanks
George Laird 39George Laird 39
The issues I'm having is spedific to this ContentDocumentLink object.  I get this error:  ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.
Deepali KulshresthaDeepali Kulshrestha
Hi George,

Greetings to you!

This code will help you to create map correctly.
public static void ProcessFiles(List<ContentDocument> docs){   
Map<Id,List<ContentDocumentLink>> ContentIdVSContentDocListMap = new ap<Id,List<ContentDocumentLink>>();
List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN : docs];
for(ContentDocumentLink cdl : cdlList){
if(!ContentIdVSContentDocListMap.containsKey(cdl.ContentDocumentId)){
    ContentIdVSContentDocListMap.put(cdl.ContentDocumentId, new List<ContentDocumentLink>());
    }
    ContentIdVSContentDocListMap.get(cdl.ContentDocumentId).add(cdl); 
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
George Laird 39George Laird 39
@Deepali Kulshrestha THANK YOU!
Manuj Pandey 18Manuj Pandey 18
I think the ask was to create a Map with ContendDocumentID as Key and related LinkedEntityID as attribute(collection). Can someone explain to me why are we associating ContentDocumentLink in the map? Shouldn't we use an aggregate query on ContentDocumentLink to store separate collection of LinkedEntityID for each ContentDocument ?