Student
Student

Reputation: 103

How to retrieve account object through SOQL Query

I have an account field(lookup field to account) and a user lookup field called ALO on contact object.What I want to do is to find out the account name field value on contact object, traverse that account, fetch the owner id and then assign that to the ALO field on the contact object.

This is what I have written in my apex controller but I am getting some syntax error maybe because of the API names that I am using. Can anybody help, please?

public Account acc {get; set;}

     acc = [ SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.Account
                        ];

     contact.ALO= acc.OwnerId;

where 'contact' is the current contact instance.

Upvotes: 0

Views: 1214

Answers (1)

user1067017
user1067017

Reputation: 173

Since ALO is a custom field you will have to append __c to the API name to access it. It would be contact.ALO__c = acc.OwnerId.

Also, you should be querying the AccountId field on Contact. Otherwise you will be getting an "No such column 'Account' on entity 'Contact'." error.

Upvotes: 1

Related Questions