function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ppiyushppiyush 

SOQL help - how to access attribute of object in a lookup field

Hi,

 

I have a lookup field called Related_Contact__c on an Opportunity which is a lookup to a Contact record. How can I access the Account for this Contact through the lookup field. I am trying the following, but doesnt seem to work - the error report tells me that there is an "unexpected token: Account"

 

SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c FROM Opportunity WHERE (Related_Contact__c.Account:=Account)

 

The comparison I am trying to make here is between the Account field of the Related_Contact__c lookup and the Account field on the Opportunity itself.

 

Any recommendations?

Best Answer chosen by Admin (Salesforce Developers) 
rmehrmeh

Hi,

 

Yes, i also did check that.

The reason it is showing an unexpected token is that it expects to be an id written in the String Format.

So if you run the query as:

 

SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c FROM Opportunity where Related_Contact__c.Account =: 'mention some account Id here '

 

You can do one thing as a solution to it:

Simply query as:

List<Opportunity> lstOpp = [SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c,  Related_Contact__c.Account, Account FROM Opportunity];

if(!lstOpp.isEmpty())

{

    for(Opportunity opp: lstOpp)

    {

            if(opp.Related_Contact__r.Account == opp.Account)

            {

                // put your code here

            }

     }

}

All Answers

rmehrmeh

Hi,

 

First of all, can you just confirm in your schema that the Account Lookup in the Opportunity Object is AccountId and not Account.

 

If it AccountId, change your query as follows:

 

SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c FROM Opportunity WHERE (Related_Contact__c.Account =: AccountId)

 

I hope this works for you.

ppiyushppiyush

I changed to AccountId, but get a message that the Variable does not exist: AccountId. I then checked the Opportunity fields, and the Field Name is actually Account, and not AccountId...

 

i have now changed it back to Account, but am unsure if its going to work...

rmehrmeh

Hi,

 

Yes, i also did check that.

The reason it is showing an unexpected token is that it expects to be an id written in the String Format.

So if you run the query as:

 

SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c FROM Opportunity where Related_Contact__c.Account =: 'mention some account Id here '

 

You can do one thing as a solution to it:

Simply query as:

List<Opportunity> lstOpp = [SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c,  Related_Contact__c.Account, Account FROM Opportunity];

if(!lstOpp.isEmpty())

{

    for(Opportunity opp: lstOpp)

    {

            if(opp.Related_Contact__r.Account == opp.Account)

            {

                // put your code here

            }

     }

}

This was selected as the best answer
Imran MohammedImran Mohammed

Use this query and see if it works,

 

ID yourval = assign the account id val here;

Opportunity[] oppList = [SELECT id, StageName, Sub_Stage__c, Start_Date__c, End_Date__c FROM Opportunity WHERE Related_Contact__r.Account.id = :yourval ];

 

Let me know if it worked.