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
MystwalkerMystwalker 

assign account id for SOQL

I've created a controller for a custom object to retrieved every contact related to the account that is related to my custom object.

 

                 Account

              ^            |

             /              |

            /               v

Custom_Object     Contact

 

But, I can't fix my the account Id to search for in the SOQL. I get a Illegal assignement for my code below but, I don't seem to find the good way to do it. Does someone has a good hint to help me start?

 

 

public class addon {
  private final Meeting_Preparation__c Meeting_PreparationObj;
  public addon(ApexPages.StandardController controller) {
    this.Meeting_PreparationObj = (Meeting_Preparation__c)controller.getSubject();
  }

  public Meeting_Preparation__c[] getRelatedContact() {
       ID Custom_Account_ID = Meeting_Preparation__c.Account__r.id;
    Contact[] contactList =
      [ SELECT Name, Title, MobilePhone, Relationship__c, Owner.Name, CreatedBy.Name

        FROM Contact

        WHERE Account.Id = :Custom_Account_ID];
 

   return contactList;
 }
}

Best Answer chosen by Admin (Salesforce Developers) 
SivarajanSivarajan

Hi ,

 

You can solve this problem. I just created the following code with my Custom object called Sub_Contract__c instead of your custom object Meeting_Preparation__c.  I just placed a button on detailed page of  the custom object.  It works fine. Just go ahead on this code.

 

 

 Controller

 

 

public class addon {
  public Sub_Contract__c subObj,temp_subobj;
  public ID Custom_Account_ID,Cus_obj_Record_ID;
  public contact[] contactList{get;set;}

  public addon(ApexPages.StandardController controller) {
    this.subObj = (Sub_Contract__c)controller.getRecord();
    Cus_obj_Record_ID = subObj.Id;
  getRelatedContact();
  }

  public void getRelatedContact() {
 
      
       temp_subobj = [select Account__r.id from Sub_Contract__c where Id =:Cus_obj_Record_ID];
       Custom_Account_ID = temp_subobj.Account__r.id; 
       contactList =[ SELECT Name, Title, MobilePhone  from Contact  WHERE Account.Id = :Custom_Account_ID];
 
 }

 

 

Visual force Page

 

<apex:page standardController="Sub_Contract__c" extensions="addon">

    <!-- Begin Default Content REMOVE THIS -->



    <apex:pageBlock>
        <apex:pageBlockTable value="{!contactList}" var="ct">

            <apex:column value="{!ct.Name}" headerValue="Name"/>
            <apex:column value="{!ct.Title}" headerValue="Title"/>
            <apex:column value="{!ct.MobilePhone}" headerValue="Mobile"/>
        </apex:pageBlockTable>
       
  </apex:pageBlock>     
</apex:page> 

 

 

Regards

 

Sivarajan

 

 

 

 

 

All Answers

SivarajanSivarajan

Hi ,

 

You can solve this problem. I just created the following code with my Custom object called Sub_Contract__c instead of your custom object Meeting_Preparation__c.  I just placed a button on detailed page of  the custom object.  It works fine. Just go ahead on this code.

 

 

 Controller

 

 

public class addon {
  public Sub_Contract__c subObj,temp_subobj;
  public ID Custom_Account_ID,Cus_obj_Record_ID;
  public contact[] contactList{get;set;}

  public addon(ApexPages.StandardController controller) {
    this.subObj = (Sub_Contract__c)controller.getRecord();
    Cus_obj_Record_ID = subObj.Id;
  getRelatedContact();
  }

  public void getRelatedContact() {
 
      
       temp_subobj = [select Account__r.id from Sub_Contract__c where Id =:Cus_obj_Record_ID];
       Custom_Account_ID = temp_subobj.Account__r.id; 
       contactList =[ SELECT Name, Title, MobilePhone  from Contact  WHERE Account.Id = :Custom_Account_ID];
 
 }

 

 

Visual force Page

 

<apex:page standardController="Sub_Contract__c" extensions="addon">

    <!-- Begin Default Content REMOVE THIS -->



    <apex:pageBlock>
        <apex:pageBlockTable value="{!contactList}" var="ct">

            <apex:column value="{!ct.Name}" headerValue="Name"/>
            <apex:column value="{!ct.Title}" headerValue="Title"/>
            <apex:column value="{!ct.MobilePhone}" headerValue="Mobile"/>
        </apex:pageBlockTable>
       
  </apex:pageBlock>     
</apex:page> 

 

 

Regards

 

Sivarajan

 

 

 

 

 

This was selected as the best answer
MystwalkerMystwalker

Thanks a lot Siva_org,

 

I've learned a few things from your advice, It gives me a great start for the other things I need to do ,

 

thanks again!