I have a custom object Professor

Professor { Name(Text) , Salary(Number) , Bonus(Number)}

I wrote the query in Developer console

select Name from Professor__c where (salary__c + bonus__c) > 7

and got below error

Unknown error parsing query:
Professor__c where (salary__c + bonus__c) > 7 ^ ERROR at Row:1:Column:57 unexpected token: ')'

How to write expression in SOQL WHERE and SELECT clauses?

  • you cannot write any expressions in query. Nov 5 '19 at 6:21
  • @TariqueShamim does this limitation apply to apex code also.. (i mean query rewritten from apex code) Nov 5 '19 at 6:28
  • Yes @DineshKumar Nov 5 '19 at 6:34
  • You may have to create an extra field on the same object which store's the sum of Salary and Bonus and then you can use that field to add a condition in SOQL query. Nov 5 '19 at 6:35
  • Either you can create a formula field to hold the sum of those fields Or You can query the records, iterate and form a new one where those two fields sum is greater than 7. Nov 5 '19 at 6:39

In SOQL query you are not allowed to mix expression in where clause. What you can add here is, you can create an extra formula field of type number which will have the sum of Salary__c and Bonus__c field value and then you can use the new field to compare.

Let's say the new field is Gross__c having formula (Salary__c + Bonus__c) Now you can use it in query,

SELECT Name FROM Professor__c WHERE Gross__c > 7

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.