I need a soql query to query to all contacts where lastname = firstname
This is not allowed in SOQL. Please refer the below link which confirms the same.
fieldExpression Syntax: The field expression syntax of the WHERE clause in a SOQL query consists of a field name, a comparison operator, and a value that’s used to compare with the value in the field name.
fieldName comparisonOperator value
fieldName The name of a field in the specified object. Use of single or double quotes around the name will result in an error. You must have at least read-level permissions to the field. It can be any field except a long text area field, encrypted data field, or base64-encoded field. It does not need to be a field in the fieldList.
comparisonOperator Case-insensitive operators that compare values.
value A value used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error.
As a workaround what you can do is:
- Create a formula field with return type as checkbox
- Set the formula to true if FirstName = LastName, else set it false
- Write a SOQL to query the contacts where the formula field is true
Select Id, FirstName, LastName from Contact where *formula_we_Created* = true