0

I have a list of Market__history that track the changes in my custom object market__c Since I am manipulating a lot this object instead of making multiple SOQL call I made one big SOQL call to extract ALL the history data for my specific markets ans now I can manipulate the data without calling too much time the servers.

What I would like to acheive is extract that LAST CREATED history object for each market but I am not sure how to acheive that.. I tried [-1] like in python without success

any idea ???

My code look the following;

  1. extract all history data :

    List<Market__History> MarketHistory = [SELECT id, ParentId, OldValue, NewValue, Field, CreatedById, CreatedDate FROM Market__History where ParentId IN :RelevantMarketData];

2)looping my market and get the history data:

for(Market__c marketItem: RelevantMarketData ){
List<Market__History> MarketHistoryList = new List<Market__History>();
            for(Market__History marketHistoryItem: MarketHistory){
                if(marketHistoryItem.parentId == marketItem.id && marketHistoryItem.CreatedDate >= begDate && marketHistoryItem.CreatedDate >= endDate ){
                    MarketHistoryList.add(marketHistoryItem);
                }                
            }
            if(MarketHistoryList.size()>=1){
                //system.debug('test');
                system.debug('test: ' + MarketHistoryList.last());
                    
                } 
        }
1
  • Could you please explain in detail because you are talking about all changes to history objects are specifically one. You can use order by with desc createddate like that
    – Pavan tej
    Sep 29, 2021 at 16:59

1 Answer 1

1

In the History table, CreatedDate field is when the change was made Order your query by CreatedDate

List<Market__History> MarketHistory = [SELECT id, ParentId, OldValue, 
NewValue, Field, CreatedById, CreatedDate FROM Market__History where 
ParentId IN :RelevantMarketData order by CreatedDate desc];

Then the first item in your list will be the most recently created history record.

MarketHistory.get(0);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .