SOQL Query on combination of two fields

By | November 21, 2013

Few days back, we had a requirement in one of our projects wherein we needed to list the records based on summation of two fields of an object. We tried to use SOQL query to filter the records, but we found that SOQL query of salesforce natively does not allow the arithmetic operation of two fields in the where clause.

Following is the query which we tried that gave us an error of malformed query –

List<Object> Objs = [Select field1, field2 from Object where (field1+field2)>=25000];

New Stuff:  Excel Export in Salesforce

We had to look for some workaround to solve this issue, for which we created a new formula field in that object which would be a summation of the two fields.

formulafield = field1 + field2

Now, we can use this newly created field to query on this object to get the  required records.

New SOQL query will be,

List<Object> Objs = [Select field1, field2 from Object where formulafield>=25000];

Similarly, we also wanted to list the records of the object where field1= field2 which is not supported in salesforce.

For this purpose, we created a new formula field in that object of text type whose formula is as follows.

If field1=field2 then newformulafield=’True’

Else newformulafield=’False’

Then, we used this newformulafield in the SOQL query to get all those records from the object whose field1=field2

SOQL query to get the records is as follows –

List<Object> Objs = [Select field1, field2 from Object where newformulafield =’True’];

About Us
Greytrix as a Salesforce Product development partner offers a wide variety of integration products and services to the end users as well as to the Partners across the globe. We offers Consultation, Configuration, Training and support services in out-of-the-box functionality as well as customizations to incorporate custom business rules and functionalities that requires apex code incorporation into the Salesforce platform.

Greytrix has some unique solutions for Cloud CRM such as Salesforce integration with Sage Enterprise Management (Sage X3), Sage Intacct, Sage 100 and Sage 300 (Sage Accpac). We also offer best-in-class Cloud CRM Salesforce customization and development services along with services such as Salesforce Data Migration, Integrated App development, Custom App development and Technical Support to business partners and end users.

Greytrix GUMU™ integration for Sage ERP – Salesforce is a 5-star app listed on Salesforce AppExchange.

For more information, please contact us at We will be glad to assist you.

Related Posts