4

I have this apex code:

//testing code for above
List<Contact> contacts = new List<Contact>();
for (MtgPlanner_CRM__Transaction_Property__c lo : [SELECT MtgPlanner_CRM__Borrower_Name__c, MtgPlanner_CRM__Status__c FROM MtgPlanner_CRM__Transaction_Property__c]){
        if (lo.MtgPlanner_CRM__Status__c == 'Closed'){
            Contact c = [SELECT of_Closed_Loans, id FROM Contact WHERE id = lo.MtgPlanner_CRM__Borrower_Name__c];
            if (!String.isBlank(c.of_Closed_Loans){
                c.of_Closed_Loans= Integer.valueOf(String.valueOf(c.of_Closed_Loans) + 1); 
            } else {
                c.of_Closed_Loans= 1;
            }
            contacts.add(c);
        }
    }
update contacts

In workbench's Apex Execute,

COMPILE ERROR: Unexpected token 'lo.MtgPlanner_CRM__Borrower_Name__c'. LINE: 5 COLUMN: 77

It seems like it is not recognizing this part of the SOQL statement:

WHERE id = lo.MtgPlanner_CRM__Borrower_Name__c]

However, System.debug(lo.MtgPlanner_CRM__Borrower_Name__c) works fine.

I just want to get the Contacts that have id's equal to the MtgPlanner_CRM__Transaction_Property__c object's MtgPlanner_CRM__Borrower_Name__c field.

1 Answer 1

8

You need to add a preceding colon when using bind variables:

Correct

WHERE Field__c = :value

Incorrect

WHERE Field__c = value

See also: Using Apex Variables in SOQL and SOSL Queries (emphasis mine):

SOQL and SOSL statements in Apex can reference Apex code variables and expressions if they’re preceded by a colon (:). This use of a local code variable within a SOQL or SOSL statement is called a bind. The Apex parser first evaluates the local variable in code context before executing the SOQL or SOSL statement.

You must log in to answer this question.

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