I am new to Salesforce. My Account have 'Keys'(custom object) as child object. So in the Account page I could see list of Keys (name, value). Now I need to get the list of 'Keys' for the particular Account and then do some manipulation. I tried to write some apex code but couldn't find a way to

  1. locate the current account object
  2. Get the list of all 'Keys' associated with that Account object

Any help is much appreciated. - Thank you.

  • This is possible with SOQL relationship queris developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/… Share your code so that people can help you on that – highfive May 22 '16 at 13:33
  • Building a Controller Extension and similar illustrate a common way to handle your first point. – Keith C May 22 '16 at 13:47
  • Could you share what you have tried so far? You could write a simple query to pick up the data such as List<Key__c> keysForAccount = [Select Id, Name, Value__c, Account__c From Key__c Where Account__c = 'xxxxxxxxxxxxxxx'] but replacing the x's with your account Id, this will give you the related keys for the specified account, it might be a good start point to help you. Once you have built this out and worked on the code to manipulate the keys you can switch the Account Id to be a variable rather than a static Id and tie this into your process. I hope this helps. – Art of Cloud May 22 '16 at 13:48

Accessing related object fields is possible in both child to parent and parent to child ways. To address your problem, you can access the child Keys like in below Apex snippet.

// place a valid account id at the "accntId"
List<Account> accntsWithKeys = [SELECT Id, Name, 
                            (SELECT Id, Name, Custom_Field1__c FROM Keys__r)
                        FROM Account WHERE Id = 'accntId'];

for(Account a : accntsWithKeys){
    System.debug('account : ' + a.Name);
    // to access child objects iterate through children list
    for(Key__c k : a.Keys__r){
        System.debug('key : ' + k.Name);

Note : In the given code snippet it's assumed that the API Name for child object is Keys__r. If the name differ, you can refer to the WSDL and get the correct API name for that.

To get an idea read on Relationship Queries

| improve this answer | |

To add to highfive's answer, if you do not need the Account fields in your processing (or just use the standard controller to access them), you can query the Key__c objects on their own:

public with sharing class AccountKeysExtension {
    public AccountKeysExtension(ApexPages.StandardController sc) {
        for (Key__c k : [
                select ...
                from Key__c
                where Account__c = :sc.getId()
                order by ...
                ) {
            // Key processing logic

<apex:page standardController="Account" extensions="AccountKeysExtension">
| improve this answer | |

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.