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
lalitha_sfdclalitha_sfdc 

How do I query New Value field on custom object history in SOQL?

I'm trying to query the date from the history object where the last status (field) is 'Request Approved' I tried the following query but unable to query on NewValue field on the history object.
error: NewValue' can not be filtered in query call".
SELECT CreatedById,CreatedDate,Field,Id,IsDeleted, NewValue,OldValue,ParentId FROM custobj__History WHERE ParentId = 'a1228000000fagQ' AND Field = 'Status__c' AND NewValue='Request Approved'

Can Someone Help Me on this? Any other WorkAround?
Best Answer chosen by lalitha_sfdc
Apoorv Saxena 4Apoorv Saxena 4
Hi Lalitha,

Here's the workaround , please try this code:
 
List<custobj__History> historyList =[SELECT CreatedById,CreatedDate,Field,Id,IsDeleted, NewValue,OldValue,ParentId FROM custobj__History WHERE ParentId = 'a1228000000fagQ' AND Field = 'Status__c'];
List<custobj__History> newhistoryList = new List<custobj__History>();
for(custobj__History h:historyList){
    if(h.newvalue=='Request Approved'){
        newhistoryList.add(h);
    }
}
system.debug('FilteredList: '+newhistoryList);

Please mark this question as solved if this helps you.

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Lalitha,

Here's the workaround , please try this code:
 
List<custobj__History> historyList =[SELECT CreatedById,CreatedDate,Field,Id,IsDeleted, NewValue,OldValue,ParentId FROM custobj__History WHERE ParentId = 'a1228000000fagQ' AND Field = 'Status__c'];
List<custobj__History> newhistoryList = new List<custobj__History>();
for(custobj__History h:historyList){
    if(h.newvalue=='Request Approved'){
        newhistoryList.add(h);
    }
}
system.debug('FilteredList: '+newhistoryList);

Please mark this question as solved if this helps you.

Thanks,
Apoorv
This was selected as the best answer
lalitha_sfdclalitha_sfdc
Hi apoorv,
This worked well.

For the new value field vote for the below idea
https://success.salesforce.com/ideaView?id=08730000000l6PSAAY


 
Dnyaneshwar Narwade 6Dnyaneshwar Narwade 6
Try with this ....

SELECT CreatedById,CreatedDate,Field,Id,IsDeleted, NewValue,OldValue,ParentId FROM custobj__History WHERE ParentId =: 'a1228000000fagQ'
Dnyaneshwar Narwade 6Dnyaneshwar Narwade 6
SELECT CreatedById,CreatedDate,Field,Id,IsDeleted, NewValue,OldValue,ParentId FROM custobj__History WHERE ParentId =: 'a1228000000fagQ'

you just need to add colun(:) after equals operator