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
Muhammad Saad JavedMuhammad Saad Javed 

SOQL fetching child Record

I am not being able to configure the trigger whose fetching childs records . This is the Screen shot , Please help me in it as your earliest possible 

User-added image

This is the Code 

trigger insert12Records on University__c (after insert) {
List<Class__c> classList = new List<Class__c>();
Set<Id> idSet = new Set<Id>();
Map<id,Integer> mapIDcount = new Map<id,Integer>();
for(University__c uni:Trigger.new)
{
idSet.add(uni.Name);
for(University__c u:[Select Id,(Select id from Class__c ) from University__c where Id =:idSet ])
{


for(Integer i = 0; i< 12; i++){
Class__c cls = new Class__c();
cls.Name= 'Test Class'+i;
cls.University__c = uni.Id;
classList.add(cls); 


}
}
insert classList;

}

}
 
Best Answer chosen by Muhammad Saad Javed
Khan AnasKhan Anas (Salesforce Developers) 
You need to change idSet.add(uni.Name); to idSet.add(uni.Id); 

Try below Code:
trigger insert12Records on University__c (after insert) {
    
    List<Class__c> classList = new List<Class__c>();
    Set<Id> idSet = new Set<Id>();
    Map<id,Integer> mapIDcount = new Map<id,Integer>();
    
    for(University__c uni : Trigger.new) {
        idSet.add(uni.Id);
    }
    
    for(University__c u : [Select Id,(Select id from Classes__r ) from University__c where Id =:idSet ]) {
        for(Integer i = 0; i< 12; i++){
            Class__c cls = new Class__c();
            cls.Name= 'Test Class'+i;
            cls.University__c = u.Id;
            classList.add(cls);         
        }
    }
    if(classList.size()>0){
        insert classList;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

S_RathS_Rath

Hello,

When we are doing parent-child query and using a relationship name in a query, you must use the relationship names without the __c. Instead, append a __r (underscore r).

Please try the below code. This may help you

for(University__c u:[Select Id,(Select id from Class__r ) from University__c where Id =:idSet ])
{}

Please choose the best answer if your problem has been resolved.

Thanks
 
S_RathS_Rath

Ali Raza Qasim

Please append __r on line no 8 where you are doing soql query. Add the below line 
for(University__c u:[Select Id,(Select id from Class__r ) from University__c where Id =:idSet ])
{}

Hope, this will help.....!!!!

Thanks
Muhammad Saad JavedMuhammad Saad Javed
can you come on team viewer and have a look on this on my screen please?
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ali,

Greetings to you!

Custom object child relationships end in __r. So the likely relationship name would be Class__r. Also, you need to use correct Child Relationship Name. So, your child relationship name should be Classes.

You can get Child Relationship Name by following below steps:
- Go to Class__c object -> Fields and Relationships -> University Field -> You can see Child Relationship Name

Change your query to:
Select Id, (Select id from Classes__r ) from University__c where Id =:idSet

If the problem still persists, please share the screenshot of relationship field.

Also, you need to modify the trigger code, you have to follow best practices.
 
trigger insert12Records on University__c (after insert) {
    
    List<Class__c> classList = new List<Class__c>();
    Set<Id> idSet = new Set<Id>();
    Map<id,Integer> mapIDcount = new Map<id,Integer>();
    
    for(University__c uni : Trigger.new) {
        idSet.add(uni.Name);
    }
    
    for(University__c u : [Select Id,(Select id from Classes__r ) from University__c where Id =:idSet ]) {
        for(Integer i = 0; i< 12; i++){
            Class__c cls = new Class__c();
            cls.Name= 'Test Class'+i;
            cls.University__c = u.Id;
            classList.add(cls);         
        }
    }
    if(classList.size()>0){
        insert classList;
    }
}


I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas


 
Muhammad Saad JavedMuhammad Saad Javed
Thanks Anas, Now trigger get saved , but didnt give the required output, 
I want this trigger to add 12 records of University Objects child object Class , They have lookup Relationship among them , This is the snippet of error
I really appriciate your answer , can you help me in this too ?
User-added image
Khan AnasKhan Anas (Salesforce Developers) 
You need to change idSet.add(uni.Name); to idSet.add(uni.Id); 

Try below Code:
trigger insert12Records on University__c (after insert) {
    
    List<Class__c> classList = new List<Class__c>();
    Set<Id> idSet = new Set<Id>();
    Map<id,Integer> mapIDcount = new Map<id,Integer>();
    
    for(University__c uni : Trigger.new) {
        idSet.add(uni.Id);
    }
    
    for(University__c u : [Select Id,(Select id from Classes__r ) from University__c where Id =:idSet ]) {
        for(Integer i = 0; i< 12; i++){
            Class__c cls = new Class__c();
            cls.Name= 'Test Class'+i;
            cls.University__c = u.Id;
            classList.add(cls);         
        }
    }
    if(classList.size()>0){
        insert classList;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
S_RathS_Rath
Hi Ali,

You cannot get child records on after insert as the parent record is not created yet so you cannot query the child of that record. 

On after insert, you cannot query child records. So your trigger will not work on After insert. Please flow the best practice.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_bestpract.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Please check the above link.

Hope, this will help you.

Thanks


 
Muhammad Saad JavedMuhammad Saad Javed
thanks a lot , worked , 
Jazak Allah