I need to update a field value on an AccountContactRelation record in an Apex class, so I need to get a reference to the record that gets created based on the Account and Contact I just created.

So I'm trying this out first in the ExecuteAnonymous window. But this code isn't working. It works until the attempt to use the Select statement and assign the result to the variable. Help, what am I doing wrong here?

Account pa = new Account();
pa.ParentId = '001M000000zr8YvIAI';
pa.Name = 'Test Parent Account';

Contact c = new Contact();
c.AccountId = pa.Id;
c.FirstName = 'John';
c.LastName = 'Tester';
c.email = 'jtester@test.com';

AccountContactRelation acr = [Select Id, status__c from 
AccountContactRelation where 
AccountId = pa.Id and Contactid = c.Id];
acr.status__c = 'Active';

The error I get is "Unexpected token 'acr'."

Can anyone help me, please?

| improve this question | | | | |

As written in Using Apex Variables in SOQL and SOSL Queries:

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. ...

In other words, you need to prefix the variables in the query with colons:

AccountContactRelation acr = 
   [Select Id, status__c 
    from AccountContactRelation 
    where AccountId = :pa.Id and Contactid = :c.Id];
| improve this answer | | | | |
  • Missed that too, thanks. I fixed, and I fixed the update/insert error, but still no luck. Still getting the token error. Now if I change the query to "where account.name = 'Test Bank', which is the account name of the parent account, it works, so you can query and assign on that AccountContactRelation object. I'm stumped here. – puffsplus Oct 19 '18 at 11:40

You are doing a few more thing wrong.

  1. You are trying to insert an existing record. Since you are trying to update a record, Use database.update(acr);.

It Modifies an existing sObject record, such as an individual account or contact, in your organization's data.


Account a = new Account(Name='SFDC');

Account myAcct =
  [SELECT Id, Name, BillingCity
   FROM Account WHERE Id = :a.Id];
myAcct.BillingCity = 'San Francisco';

Database.SaveResult SR =

Reference:- Database Class

| improve this answer | | | | |
  • Thank you. I updated my code, but there's still an issue there that is causing an error. I'm getting the same "Unexpected token 'acr' error. But great catch, thanks. – puffsplus Oct 19 '18 at 11:39
  • Update your latest code which you are using by updating your question. Then we can tell more. – sanket kumar Oct 19 '18 at 12:04
  • OK, good news, I got it working! Would not have gotten it without your help, though. Thank you. Turned out to be parentheses that were the key. AccountContactRelation acr = [SELECT Id FROM AccountContactRelation where (contactid = :c.Id and AccountId = :pa.Id)]; – puffsplus Oct 19 '18 at 12:36
  • I don't think so. Try to query without it and with it. Match the result. Both will be the same. – sanket kumar Oct 19 '18 at 12:49

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.