You need to sign in to do that
Don't have an account?
Muhammad 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
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;
}
}
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;
}
}
Try below Code:
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
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
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
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:
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.
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
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 ?
Try below Code:
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
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
Jazak Allah