0

i have two list of records,now i want to put them in map,for one list i iterate it in for loop but for second it always get hardcoded. How can i do that,below is my code:-

public Map<id,ContentVersion> getContentVersion(set<id> setOfEntityDocId){

    set<id> SetOfDocLink = new Set<id>();
    Map<id,ContentVersion> mapOfCvForDoc = new Map<id,ContentVersion>() ;
    ContentVersion objContentVersion ;
    List<ContentDocumentLink > listOfContentDocumentLink = [Select Id, LinkedEntityId, ContentDocumentId from ContentDocumentLink where LinkedEntityId= :setOfEntityDocId];

    for(ContentDocumentLink cd : listOfContentDocumentLink ){
          SetOfDocLink.add(cd.ContentDocumentId);
    }
    List<ContentVersion> listOfContentVersion = [select id,Title,PathOnClient,ContentDocumentId,FirstPublishLocationId,LastModifiedById,CreatedDate,Category__c   from ContentVersion where ContentDocumentId =:SetOfDocLink ];

    List<Entity_Document__c> listOFEntityDocs = [select id,Name,Category__c,Document_Type__c,Expire_Date__c,Language_HRW__c,Description__c,Document_Title__c,Effective_Date__c,Entity__c from Entity_Document__c where id =:setOfEntityDocId];


    if(!listOfContentVersion.isEmpty() && !listOFEntityDocs.isEmpty()){

        for(Entity_Document__c ed : listOFEntityDocs){

            mapOfCvForDoc.put(ed.id, listOfContentVersion[0]);

        }
    }

    return mapOfCvForDoc;

}

as i want that i get all the related files in map from the object.i know i hardcoded the list in map..i want to know how can i get rid of that?

2
  • your question is not clear. What are the two list and in which map you want to put? what is the hardcoded list ? – Santanu Boral May 12 '18 at 8:08
  • sorry for inconvinence i have list listOFEntityDocs and listOfContentVersion and object is entity_Document__c.and i want every entity_Document__c has contentVersion.now when i put it in map mapOfCvForDoc.put(ed.id, listOfContentVersion[0]); then contentversion is hardcoded – SalesforceFolks May 12 '18 at 8:13
2

You need to start by considering what is related to what. I think the model is like this (where ^ or v means a reference to the parent):

Entity_Document__c
    ^
    ContentDocumentLink
    v
ContentDocument
    ^
    ContentVersion

This means that for each Entity_Document__c there can be many ContentDocument and for each ContentDocument there can be many ContentVersion.

So this return type doesn't make much sense (assuming the Id is of the Entity_Document__c):

public Map<Id,ContentVersion> getContentVersion(...)

unless there are some other constraints applied that e.g. an assumption that there is only ever one ContentDocument per Entity_Document__c and that you only want say the latest ContentVersion.

So best you define the specific logic you require first and then work on the code.

1
  • yes..i got my mistake and will correct it. Thank you @Keith C for suggetion. – SalesforceFolks May 12 '18 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.