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
RC_48RC_48 

Cant able to compare related objects fields in soql

Hi,

I have a batch class which is taking all the pending orders in the org, which are around 20,000 records. When this batch job is running I am getting limit exceptions for process builders due to which orders are not processing.

Using below query we are fetching all the pending orders in the org.
Query: SELECT Id, OrderNumber, Stage__c, RecordTypeId, Pricebook2Id, Order_Hospital__r.Retail_Price_Zone__c, Order_Hospital__r.Service_Price_Zone__c FROM Order WHERE Stage__c = \'Pending\' AND Order_Hospital__c != null'

I need to modify the query by using the below filter.

Filter:
o.RecordTypeId == RETAIL_REC_TYPE_ID && o.Pricebook2Id != o.Order_Hospital__r.Retail_Price_Zone__c ||
                o.RecordTypeId == SERVICE_REC_TYPE_ID && o.Pricebook2Id != o.Order_Hospital__r.Service_Price_Zone__c 

But I am not able to include the filter in the query because salesforce is not allowing to query related records. 

Can anyone please help me on how I can include the filter in the query.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you please share what excatly error you are facing while updating the query and the updated query so experts can share their inputs on this.

Thanks,
 
RC_48RC_48
Hi Praveen,

I want to modify query as below.. 

Query:
SELECT Id, OrderNumber, Stage__c, RecordTypeId, Pricebook2Id, Order_Hospital__r.Retail_Price_Zone__c, Order_Hospital__r.Service_Price_Zone__c FROM Order WHERE Stage__c = 'Pending' AND Order_Hospital__c != null and Pricebook2Id != Order_Hospital__r.Retail_Price_Zone__c

Error:
Unknown error parsing query

Thanks,
Ragadeep
AnkaiahAnkaiah (Salesforce Developers) 
Hi RC,

There might be whitespace in the query.
Can you please try with workbench ?

Thanks!!