0

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?

6
  • you cannot write any expressions in query. Nov 5, 2019 at 6:21
  • @TariqueShamim does this limitation apply to apex code also.. (i mean query rewritten from apex code) Nov 5, 2019 at 6:28
  • Yes @DineshKumar Nov 5, 2019 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, 2019 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, 2019 at 6:39

1 Answer 1

2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .