You need to sign in to do that
Don't have an account?
Santhosh Kon 5
Hi Community below code throwing an error while updating child from parent, Error:Apex trigger studentupdate123 caused an unexpected exception, contac
trigger studentupdate123 on Teacher__c (after update) {
list<Student__c> childquery=new list<student__c>();
list<Student__c> childupdatelist=new list<student__c>();
for(Teacher__c acc:[select id,name,City__c,(select id ,name,City__c from Students__r) from Teacher__c])
{
if(acc.Students__r.size()>0 &&acc.Students__r.size()!=null){
childquery.add(acc.Students__r);
}
}
for(Teacher__c ad:Trigger.new)
{
for(Student__c c:childquery)
{
c.City__c=ad.City__c;
childupdatelist.add(c);
}
}
if(childupdatelist.size()>0 && childupdatelist.size()!=null){
update childupdatelist;
}
}
list<Student__c> childquery=new list<student__c>();
list<Student__c> childupdatelist=new list<student__c>();
for(Teacher__c acc:[select id,name,City__c,(select id ,name,City__c from Students__r) from Teacher__c])
{
if(acc.Students__r.size()>0 &&acc.Students__r.size()!=null){
childquery.add(acc.Students__r);
}
}
for(Teacher__c ad:Trigger.new)
{
for(Student__c c:childquery)
{
c.City__c=ad.City__c;
childupdatelist.add(c);
}
}
if(childupdatelist.size()>0 && childupdatelist.size()!=null){
update childupdatelist;
}
}
What is the complete error message?
System.QueryException: List has more than 1 row for assignment to SObject: External entry point
As suggested in https://salesforce.stackexchange.com/questions/80637/list-has-more-than-1-row-for-assignment-to-sobject-external-entry-point does it fix the issue when you use childquery.addAll(acc.Students__r); instead of childquery.add(acc.Students__r);
Related posts: https://salesforce.stackexchange.com/questions/45810/list-has-more-than-1-row-for-assignment-to-sobject
https://salesforce.stackexchange.com/questions/64812/system-queryexception-list-has-more-than-1-row-for-assignment-to-sobject
If this information helps, please mark the answer as best.Thank you