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
aottaruaottaru 

SOQL in Apex Trigger

Does anyone know why I am getting the error "Didn't understand relationship 'dispatch__c' 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."

Here is the line of the code with the error.

List<Charter__c> Dispatch = [select id,(Select Id from dispatch__c WHERE Date_Scheduled__c != null)
                                 from Charter__c where ID IN: Trigger.NewMap.KeySet() ORDER BY Date_Scheduled__c];

Thanks.
Best Answer chosen by aottaru
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
Go the Dispatch custom object and then to the field in that object that links it to the Charter custom object (this will be a Master /Detail or Lookup field). Click on the field to open its information and then mid-way down on the right you will see a section called ' Child Relationship name'. Add __r to the end of this name and use that in your nested query and you should be fine. 

All Answers

neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
When you nest a query the object nested should end in __r and not __c. 
Try the below. 

List<Charter__c> Dispatch = [select id,(Select Id from dispatch__r WHERE Date_Scheduled__c != null)
                                 from Charter__c where ID IN: Trigger.NewMap.KeySet() ORDER BY Date_Scheduled__c];

Note: sometimes the relationship field in your child object may not have the same name as the object itself. Let me know if the above doesnt work then I'll show you how to check. 
aottaruaottaru
The above example did not work. Can you please show me how to check? Thanks Thanks, Andrew
neil_edwards_fcca1.389781386123482E12neil_edwards_fcca1.389781386123482E12
Go the Dispatch custom object and then to the field in that object that links it to the Charter custom object (this will be a Master /Detail or Lookup field). Click on the field to open its information and then mid-way down on the right you will see a section called ' Child Relationship name'. Add __r to the end of this name and use that in your nested query and you should be fine. 
This was selected as the best answer
aottaruaottaru
Thank you so much Neil. This is working fine now.