0

I have three custom objects named Parent__c, Child__c, and A__c. I've used A__c as the standardcontroller. It serves as the search page for Parent__c record, wherein on click of a lookup field, all Parent__c info, including Child__c records for that specific searched parent record, will be displayed automatically.

The current state of the controller queries Parent__c and Child__c in different methods within that class alone. One apex method is for getting all the Parent__c records, while the other method focuses on getting all the Child__c records related to that parent record.

How can I call method inside another method within just a single apex class? Meaning call the Child__c method inside the Parent__c method.

method1:for parent method

//some code....assume we already declared some variables
public void search() {
    parentqry = [SELECT Id, Name FROM Parent__c WHERE Id =: A__c.Parent__c];
} 

method2:for child method

//some code...this gets the `Child__c` records associated with the searched Parent__c record. let's assume we already have a wrapper class named `WrapperClass`....
public List<WrapperClass> getChild() {
    if(childList == null) {
        childList = new List<WrapperClass>();
        for(Child__c c :[SELECT Id, Name, Parent__c, Type__c FROM Child__c WHERE Parent__c =: A__c.Parent__c]) {
        }
    } 
} 
1

If you want to get all the children for this parent record, why don't you bring it together into one single SOQL query in search()?

parentqry = 
  [SELECT Id, Name, 
    (SELECT Id, Name, Parent__c, Type__c FROM Child__r) FROM Parent__c
  WHERE Id =: A__c.Parent__c];
0

You can call the other method in the One Method by following:

public void search() {
    parentqry = [SELECT Id, Name FROM Parent__c WHERE Id =: A__c.Parent__c];
    List<WrapperClass> childListYoWant = getChild();
} 

public List<WrapperClass> getChild() {
    if(childList == null) {
        childList = new List<WrapperClass>();
        for(Child__c c :[SELECT Id, Name, Parent__c, Type__c FROM Child__c WHERE Parent__c =: A__c.Parent__c]) {
        }
    }
    return childList;
} 

Hope this will help.

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.