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
Maf_007Maf_007 

Error in Test Class for SOQL Query String Child relationship

Hi All,

I have a batch call out class which has a nested SOQL query string in start method. My test class fails with following:

System.SObjectException: Invalid aggregate relationship Child Relationship Name for Account

my code is below:

global class batchclassname implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts
{

     global database.querylocator start(Database.BatchableContext BC)
    {
	 String Query = 'SELECT id, Industry__c, (Select id, Account_Name__c from Children__r where active__c = true) FROM Account Limit 100';
        
     return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> scope)
    {   
        if(scope.size()>0)
        {
			for(sobject s: scope)
        	{
            	//Get the Account sobject
            	Account a = (Account)s;
            	
            	List<Child__c> RelatedChildren = a.getsobjects('Children__r'); // This line causing issue in test class I believe
               
                       for(Child__c Ch:RelatedChildren)
                      {
                           do some call out here....
                       }
                  }
            }
     }

    global void finish(Database.BatchableContext BC)
    {
        	
    }
    

}
Best Answer chosen by Maf_007
Maf_007Maf_007
Hi Thanks for your reply I have solved the problem by casting soject to the child type. Please see the following:

List<SObject> RelatedRecords = a.getsobjects('Customer_Accounts__r');
List<Customer_Account__c> RelatedAcc = (List<Customer_Account__c>)RelatedRecords;

and then going over the child list...

All Answers

Vinit_KumarVinit_Kumar
Can you verify that the relationship name between Account and child is Children__r ??
Maf_007Maf_007
Hi Vinit,

Thanks for your reply. 

My child relationship name is Customer_Accounts__r. I have written children__r to make it simpler. Also my child object name is customer__c.

thanks
Vinit_KumarVinit_Kumar
Replace this line 

List<Child__c> RelatedChildren = a.getsobjects('Children__r');

with 

List<Child__c> RelatedChildren = a.get(0).getsobjects('Children__r');

The reason I am doing this as it would check for the first element in the List (as Account is also a list )else you can loop around the collection to get the whole list.
Maf_007Maf_007
Account is a single sobject in this case as you can see I am looping over scope sobect and casting each one into Account. Let me try your solution and I will see if that works.

Thanks again
Maf_007Maf_007
This gives me a compille time error as expected because Account is a single sObject and not a list...
Maf_007Maf_007
Hi Thanks for your reply I have solved the problem by casting soject to the child type. Please see the following:

List<SObject> RelatedRecords = a.getsobjects('Customer_Accounts__r');
List<Customer_Account__c> RelatedAcc = (List<Customer_Account__c>)RelatedRecords;

and then going over the child list...
This was selected as the best answer