You need to sign in to do that
Don't have an account?
After Update trigger Problem?Pls help me
Hi All,
This Apex trigger to automatically create the child record when a new parent record is created.But If i am going to update on Studentname then Error is thrown.pls help on this.
Error:Apex trigger createchildobj caused an unexpected exception, contact your administrator: createchildobj: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.createchildobj: line 13, column 1
Parent:Student__c
Child:Class__c
trigger createchildobj on Student__c(after insert,after update)
{
List<Class__c> lstclass=new List<Class__c>();
for(Student__c s:Trigger.new)
{
if(s.name!=null){
lstclass.add(new Class__c(Name=s.name,Age__c=s.Age__c,classstu__c=s.id));
}
}
if(lstclass.size()>0){
update lstclass; }
}
trigger createchildobj on Student__c(after insert,after update){
List<Class__c> lstclass=new List<Class__c>();
Map<id,Student__c> stdMap=new Map<Id,Student__c>();
for(Student__c s:Trigger.new){
stdMap.put(s.Id,s);
}
if(Trigger.isInsert){
for(Student__c s:Trigger.new){
if(s.name!=null){
lstclass.add(new Class__c(Name=s.name,Age__c=s.Age__c,classstu__c=s.id));
}
}
if(lstclass.size()>0){
try{
insert lstclass;
}catch(DmlException de){
System.debug(de);
}
}
}
if(Trigger.isUpdate){
for(Student__c s:Trigger.new){
stdMap.put(s.Id,s);
}
List<Class__c> clasList=[SELECT id,Name,classstu__c FROM Class__c WHERE Id IN:stdMap.keySet()];
List<Class__c> clasListToUpdate=new List<Class__c>();
for(Class__c cls:clasList){
cls.Name=stdMap.get(cls.Id).Name;
clasListToUpdate.add(cls);
}
if(clasListToUpdate.size()>0){
try{
update lstclass;
}catch(DmlException de){
System.debug(de);
}
}
}
}
Check this code it will work
All Answers
Hey you are updating the classlist in case of insert. Try to insert th elist.
Hi,
Manoj is right.You need to insert the list rather than Update.
In update you need to add Id as well.And in your case u don't have Id for the records.
if(lstclass.size()>0){
insert lstclass; // instead od update
}
Please let me know if u have any problem on same and if this post helps u please throw KUDOS by click on star at left.
Hi All,
Thanks for quick reply..
I have changed the code but No error is coming But record is not updated then New record is inserted.
trigger createchildobj on Student__c(after insert,after update)
{
List<Class__c> lstclass=new List<Class__c>();
for(Student__c s:Trigger.new){
if(s.name!=null){
lstclass.add(new Class__c(Name=s.name,Age__c=s.Age__c,classstu__c=s.id));
}
}
if(lstclass.size()>0){
insert lstclass;
}
}
If i Change
stuname= Ramkumar
now i updating this name stuname=Ramkumar S
Now two records is showing in the Classlist .
Thanks,
Vicky
trigger createchildobj on Student__c(after insert,after update){
List<Class__c> lstclass=new List<Class__c>();
Map<id,Student__c> stdMap=new Map<Id,Student__c>();
for(Student__c s:Trigger.new){
stdMap.put(s.Id,s);
}
if(Trigger.isInsert){
for(Student__c s:Trigger.new){
if(s.name!=null){
lstclass.add(new Class__c(Name=s.name,Age__c=s.Age__c,classstu__c=s.id));
}
}
if(lstclass.size()>0){
try{
insert lstclass;
}catch(DmlException de){
System.debug(de);
}
}
}
if(Trigger.isUpdate){
for(Student__c s:Trigger.new){
stdMap.put(s.Id,s);
}
List<Class__c> clasList=[SELECT id,Name,classstu__c FROM Class__c WHERE Id IN:stdMap.keySet()];
List<Class__c> clasListToUpdate=new List<Class__c>();
for(Class__c cls:clasList){
cls.Name=stdMap.get(cls.Id).Name;
clasListToUpdate.add(cls);
}
if(clasListToUpdate.size()>0){
try{
update lstclass;
}catch(DmlException de){
System.debug(de);
}
}
}
}
Check this code it will work