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
Santhosh Kon 5Santhosh 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;
}
}
SwethaSwetha (Salesforce Developers) 
HI Santosh,
What is the complete error message?
Santhosh Kon 5Santhosh Kon 5
Hi Swetha ,below the error have got while saving a parent record

System.QueryException: List has more than 1 row for assignment to SObject: External entry point
SwethaSwetha (Salesforce Developers) 
HI Santosh,
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