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
Adil_SFDCAdil_SFDC 

Help in Formatting List

Hi All 

 

I am trying to get documents from an external website in a list  in the descending order of last updated field. 

 

Now the document might have been updated many times since the date it is created. So my list is displaying the documents same number of times, I want it to be displayed  only once w.r.t to Id How do I Map it.

pLEASE HELP.

  public List<Document> defaultSortdocIds(List<Document> OdocList ){
     List<String> docId = new List<String>();
     List<String> finaldocId = new List<String>();
     List<Document> tempdoc = new List<Document>();
     for(Document d : OdocList){
     
      docId.add(d.lastupdated);
     }
      docId.sort();
      
      for(Integer i=docId.size()-1;i>=0;i--){
      
       finaldocId.add(docId.get(i));
      }
      
      
        
         for(String s : finaldocId){
          for(Document d : OdocList){
         
           if(d.lastupdated==s){
           tempdoc.add(d);
           }
         
         }
       
       }
       return tempdoc;
    }
 

 

.Thanks in Advance

vishal@forcevishal@force

What field type is lastUpdated? There's no such field on Document.

 

Also, you're having a list of string and then sorting them, it won't sort it by date anyway.

 

Try this, it's a similar code (only difference is I have written it for sorting Accounts in descending order of their Last Modified Date)

 

private void sortListDesc(List<Account> tempList){
	 List<DateTime> docDate = new List<DateTime>(); // CHANGED
     List<DateTime> finaldocId = new List<DateTime>();	// CHANGED
     List<Account> tempdoc = new List<Account>();
     for(Account d : tempList){
     
      docDate.add(d.LastModifiedDate); // CHANGED
     }
      docDate.sort();
      
      for(Integer i=docDate.size()-1;i>=0;i--){
      
       finaldocId.add(docDate.get(i));
      }
      
      
        
         for(DateTime s : finaldocId){ 
          for(Account d : tempList){
         
           if(d.LastModifiedDate == s){
           tempdoc.add(d);
           }
         
         }
       
       }
       lstAccount = tempdoc;
    }

 

Adil_SFDCAdil_SFDC

Hi Vishal 

 

This is not Salesforce field or object and my lastupdate value is a string . These values are coming from an external website through integration 

 

Here is what i tried and i get error TOO MANY SCRIPTS

 public List<Document> defaultSortdocIds(List<Document> OdocList ){
     List<String> docId = new List<String>();
     List<String> finaldocId = new List<String>();
     List<Document> tempdoc = new List<Document>();
     Map<String,Document> documentIdMap = new Map<String,Document>();
      Map<String,String> IdsLastmodifiedmap = new Map<String,String>();
     for(Document d : OdocList){
     
      docId.add(d.lastupdated);
       documentIdMap.put(d.Id,d);
       IdsLastmodifiedmap.put(d.Id,d.lastupdated);
     }
      docId.sort();
      
     /* for(Document d : OdocList){
      
       documentIdMap.put(d.Id,d);
      }*/
      
      
      for(Integer i=docId.size()-1;i>=0;i--){
      
       finaldocId.add(docId.get(i));
      }
      
     
     
      
       List<String> docListasc = new List<String>();
        
         for(String s : finaldocId){
          
           for(String k : IdsLastmodifiedmap.Values()){
           
            if(s.equals(k)){
              for(String key : IdsLastmodifiedmap.keyset())
              if(IdsLastmodifiedmap.get(key).equals(k))
               docListasc.add(key);
              }
              
              for(String docId1 : docListasc){
              
               tempdoc.add(documentIdMap.get(docId1));
                
              
              }
           }
          }
          
          /*
          for(Document d : OdocList){
           if(d.lastupdated==s){
           
           tempdoc.add(d);
           }
         
         }*/
           return tempdoc;
       }