+ Start a Discussion
RICARDO PALMARICARDO PALMA 

How to add a relation in a dynamic Query.

Hi,
I have the following dynamic query in a batch process:
I have a lookup relation between Tracking_Number__c  and Account but I'm getting this error:

Didn't understand relationship 'Account_Id__r' 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 appropri

String query;
global Database.QueryLocator start(Database.BatchableContext BC) {
query = 'SELECT Id, Marchex_Campaign_Id__c, Tracking_Phone_Number__c, Opportunity_Product_Id__c (Select Marchex_Account_Id__c From Account_Id__r) FROM Tracking_Number__c Where  Deactivated_Date__c <= Today and Deactivation_Processed__c = true';
return Database.getQueryLocator(query);
}
Thanks
Irvine DelacroixIrvine Delacroix
Try doing (Select Marchex_Account_Id__c From Accounts)
RICARDO PALMARICARDO PALMA
Thanks Irvine,
I tryied adding Account_Id__r.Marchex_Account_Id__c and it is working.