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
Allen ManamelAllen Manamel 

How to retrieve account object through SOQL Query

Hi,

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.
Best Answer chosen by Allen Manamel
Sagar PatilSagar Patil
Hi Anjli,

Can you please modify the SOQL as below and try again.

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

Also, if ALO is custom lookup field then check the API name of ALO field and use that in code.

Regards,
Sagar 
 

All Answers

Sagar PatilSagar Patil
Hi Anjli,

Can you please modify the SOQL as below and try again.

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

Also, if ALO is custom lookup field then check the API name of ALO field and use that in code.

Regards,
Sagar 
 
This was selected as the best answer
Allen ManamelAllen Manamel
I tried this

acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.accountid
                        LIMIT 1];
                        
                
                        
              contact.ALO__c = acc.OwnerId;
            


Error: Error occurred while loading a Visualforce page. 

Also would it be public Account acc {get; set;} or 

Account acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.accountid
                        LIMIT 1];
                        
                
                        
              contact.ALO__c = acc.OwnerId;
Sagar PatilSagar Patil
Can you post the complete code to analyze the issue.
Allen ManamelAllen Manamel
Alright so that's my visualforce page:

<apex:page controller="Page4Controller" docType="html-5.0" showHeader="false" sidebar="false" standardStylesheets="false" html-lang="en-US">

<apex:commandbutton value="Submit Application" id="theButton" action="{!submit}"/>

Here is my Page4Controller:

  public Contact contact {get; set;}
  public Account acc {get; set;}

  public Page4Controller() {

        User currentUser = [SELECT contactId  FROM User WHERE Id = :UserInfo.getUserId()];
        System.debug('Current user' + currentUser);

          if(String.isNotBlank(currentUser.ContactId)) {
            contact  = [
                SELECT  firstName, lastName, MiddleName,  Nickname__c, Suffix, MailingStreet, MailingCity,
                MailingStateCode, MailingPostalCode
                
                FROM Contact
                WHERE Id = :currentUser.ContactId
                LIMIT 1
            ];

            System.debug('contact information: ' + contact );


            application = [
                        
                        SELECT Id, Contact__c, Attend_College_after_High_School__c
                        FROM Application__c 
                        WHERE Contact__c =: currentUser.contactId
                        LIMIT 1];
            
                        
              
            
         
              }
     
     }
     



public pagereference submit() {

if(application.Attend_College_after_High_School__c == 'No')
         
            {
            
                   
                acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.accountid
                        LIMIT 1];
                        
                
                        
              contact.ALO__c = acc.OwnerId;
            
                        
              }
              
       update contact;
        Pagereference Page = new Pagereference('Page_4');
        Page.setRedirect(true);
        return Page;
}





Note: I am accurately getting contact and application object data. 
Allen ManamelAllen Manamel
ok so I tried this and it worked
 if(application.Attend_College_after_High_School__c == 'No')
         
            {
            
                   
                acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: '001r0000008ZxQp'
                        ];
                        
                
                        
               contact.ALO__c = acc.OwnerId;
            
                        
              }
            
so something is wrong here
   WHERE Id =: contact.accountid  
Allen ManamelAllen Manamel
So I figured out the issue and it works fine now.

I was not fetching accountid while retrieving the contact object from database. 
Following is my final working code

 if(String.isNotBlank(currentUser.ContactId)) {
            contact  = [
                SELECT  firstName, lastName, MiddleName,  Nickname__c, Suffix, MailingStreet, MailingCity,
                MailingStateCode, MailingPostalCode, AccountId
                
                FROM Contact
                WHERE Id = :currentUser.ContactId
                LIMIT 1
            ];



 if(application.Attend_College_after_High_School__c == 'No')
         
            {
            
                   
                acc = [
                        
                        SELECT Id, OwnerId
                        FROM Account
                        WHERE Id =: contact.AccountId
                        ];
                        
                
                contact.ALO__c = acc.OwnerId;
            
                        
              }
 
Allen ManamelAllen Manamel
Also I would be very grateful if you could help me with this question.
https://developer.salesforce.com/forums#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=OPENQUESTIONS&id=9060G0000005XFoQAM