0
    Select op.Id, op.Name, op.Install_Date__c, op.Scheduled_Go_Live_Date__c, 
(SELECT Id, Field, OldValue, NewValue, OpportunityId FROM OpportunityFieldHistory) 
FROM Opportunity op WHERE op.Scheduled_Go_live_Change_Count__c != 'null' AND op.Scheduled_Go_live_Change_Count__c != '' AND op.Boolean_check_install_date__c = 
TRUE

Here's my issue.

I have had fields modified in....600-800 opportunities, that WHERE is the check to see if they have been modified. What I want to do is pull the opphistory (it's an object that has a field opporunityId that matches with the opportunity object Id field it also has the old data)

So how would I go about getting the old data on the op.Install_Date__c and the op.Scheduled_Go_Live_Date__c fields from the opportunity? Because my SOQL statement comes up with this error:

NewValue, OpportunityId FROM OpportunityFieldHistory) FROM Opportunity ^ ERROR at Row:1:Column:136
Didn't understand relationship 'OpportunityFieldHistory' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

9

The child relationship name is Histories. You can find the correct API Name of the Child Relationship for any such subquery using a script like the below:

for (ChildRelationship relation : SObjectType.Opportunity.getChildRelationships())
    if (relation.getChildSObject() == OpportunityFieldHistory.sObjectType)
        system.debug(relation.getRelationshipName());
  • 1
    +1 for providing the script to get the relationship name. – Jarvis Aug 13 '18 at 17:22
  • Select op.Name, op.Id (Select OldValue, NewValue FROM Histories) From Opportunity op where op.Boolean_check_install_date__c = TRUE should work? Because that leaves me with an 'Unknown Error parsing query – enossir Aug 13 '18 at 17:53
  • 1
    You need a comma after the op.Id. Also, there's really not much point to Aliasing your object. Since there is a character count limit in Apex (although it is admittedly quite high). I'd recommend not doing this as it unnecessarily bloats character count and adds nothing to the code. Select Name, Id, (Select OldValue, NewValue FROM Histories) From Opportunity where Boolean_check_install_date__c = TRUE Lastly, you can get the child relationship names from the WSDL as well – gNerb Aug 13 '18 at 18:07

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.