You need to sign in to do that
Don't have an account?

Not able to Delete the record because of Apex Trigger
Hi ,
I have implemented the below code. But problem is when i am deleting the child record (Patient Record )then i am getting the error.
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateDoctor caused an unexpected exception, contact your administrator: UpdateDoctor: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Class.UpdatedoctorInfo.updatedoctor: line 10, column 1".
Apex Class:
-----------------
public Class UpdatedoctorInfo
{
public void updatedoctor (List<Patient__C> patients)
{
Map<String, Patient__C> patientcodeMap= new Map<String, Patient__C>();
for(Patient__c p: patients)
{
if (p.Doctor_Code__C != Null )
{
patientcodeMap.put(p.Doctor_Code__C ,p);
}
}
List<Doctor__C> doctorToUpdate= new List<Doctor__C>();
List<Doctor__C> doctorList = new List<Doctor__C>([SELECT id,Doctor_Code__C FROM Doctor__C WHERE Doctor_Code__C IN :patientcodeMap.KeySet()]);
System.debug('***********' +doctorList);
for(Doctor__C c: doctorList )
{
Patient__C obj = patientcodeMap.get(c.Doctor_Code__C );
if(obj.Doctor__C == NULL)
{
system.debug('doctor id' +obj.Id);
obj.Doctor__C = c.Id;
}
else
{
}
doctorToUpdate.add(c);
update doctorToUpdate;
}
Map<Id, Doctor__c> opportunityMap = new Map<Id, Doctor__c>();
for (Patient__C ps : patients)
{
Doctor__c opp = new Doctor__c(Id = ps.Doctor__C);
opp.Jan_Revenue_Value__c = 0;
opp.Feb_Revenue__c = 0;
opp.Mar_Revenue__c = 0;
opp.May_Revenue__c = 0;
opp.Apr_Revenue__c = 0;
opp.Jun_Revenue__c = 0;
opp.Jul_Revenue__c = 0;
opp.Aug_Revenue__c = 0;
opp.Sep_Revenue__c = 0;
opp.Oct_Revenue__c = 0;
opp.Nov_Revenue__c = 0;
opp.Dec_Revenue__c = 0;
opportunityMap.put(ps.Doctor__C, opp);
}
List<Patient__c> pat = [select id,Doctor__C ,Patient_Revenue__c,CreatedDate from Patient__C where Doctor__C in :opportunityMap.keySet()];
for (Patient__C ps : pat)
{
Doctor__c opp = opportunityMap.get(ps.Doctor__c);
if(ps.CreatedDate.month() == 1)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jan_Revenue_Value__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 2)
{
if (ps.Patient_Revenue__c != null )
{
opp.Feb_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 3)
{
if (ps.Patient_Revenue__c != null )
{
opp.Mar_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 4)
{
if (ps.Patient_Revenue__c != null )
{
opp.Apr_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 5)
{
if (ps.Patient_Revenue__c != null )
{
opp.May_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 6)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jun_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 7)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jul_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 8)
{
if (ps.Patient_Revenue__c != null )
{
opp.Aug_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 9)
{
if (ps.Patient_Revenue__c != null )
{
opp.Sep_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 10)
{
if (ps.Patient_Revenue__c != null )
{
opp.Oct_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 11)
{
if (ps.Patient_Revenue__c != null )
{
opp.Nov_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 12)
{
if (ps.Patient_Revenue__c != null )
{
opp.Dec_Revenue__c += ps.Patient_Revenue__c;
}
} }
update opportunityMap.values();
}}
Apex Trigger:
--------------------------
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, before delete,after update, after delete, after undelete ) {
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients); }
Thanks for your help!
I have implemented the below code. But problem is when i am deleting the child record (Patient Record )then i am getting the error.
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger UpdateDoctor caused an unexpected exception, contact your administrator: UpdateDoctor: execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Class.UpdatedoctorInfo.updatedoctor: line 10, column 1".
Apex Class:
-----------------
public Class UpdatedoctorInfo
{
public void updatedoctor (List<Patient__C> patients)
{
Map<String, Patient__C> patientcodeMap= new Map<String, Patient__C>();
for(Patient__c p: patients)
{
if (p.Doctor_Code__C != Null )
{
patientcodeMap.put(p.Doctor_Code__C ,p);
}
}
List<Doctor__C> doctorToUpdate= new List<Doctor__C>();
List<Doctor__C> doctorList = new List<Doctor__C>([SELECT id,Doctor_Code__C FROM Doctor__C WHERE Doctor_Code__C IN :patientcodeMap.KeySet()]);
System.debug('***********' +doctorList);
for(Doctor__C c: doctorList )
{
Patient__C obj = patientcodeMap.get(c.Doctor_Code__C );
if(obj.Doctor__C == NULL)
{
system.debug('doctor id' +obj.Id);
obj.Doctor__C = c.Id;
}
else
{
}
doctorToUpdate.add(c);
update doctorToUpdate;
}
Map<Id, Doctor__c> opportunityMap = new Map<Id, Doctor__c>();
for (Patient__C ps : patients)
{
Doctor__c opp = new Doctor__c(Id = ps.Doctor__C);
opp.Jan_Revenue_Value__c = 0;
opp.Feb_Revenue__c = 0;
opp.Mar_Revenue__c = 0;
opp.May_Revenue__c = 0;
opp.Apr_Revenue__c = 0;
opp.Jun_Revenue__c = 0;
opp.Jul_Revenue__c = 0;
opp.Aug_Revenue__c = 0;
opp.Sep_Revenue__c = 0;
opp.Oct_Revenue__c = 0;
opp.Nov_Revenue__c = 0;
opp.Dec_Revenue__c = 0;
opportunityMap.put(ps.Doctor__C, opp);
}
List<Patient__c> pat = [select id,Doctor__C ,Patient_Revenue__c,CreatedDate from Patient__C where Doctor__C in :opportunityMap.keySet()];
for (Patient__C ps : pat)
{
Doctor__c opp = opportunityMap.get(ps.Doctor__c);
if(ps.CreatedDate.month() == 1)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jan_Revenue_Value__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 2)
{
if (ps.Patient_Revenue__c != null )
{
opp.Feb_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 3)
{
if (ps.Patient_Revenue__c != null )
{
opp.Mar_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 4)
{
if (ps.Patient_Revenue__c != null )
{
opp.Apr_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 5)
{
if (ps.Patient_Revenue__c != null )
{
opp.May_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 6)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jun_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 7)
{
if (ps.Patient_Revenue__c != null )
{
opp.Jul_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 8)
{
if (ps.Patient_Revenue__c != null )
{
opp.Aug_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 9)
{
if (ps.Patient_Revenue__c != null )
{
opp.Sep_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 10)
{
if (ps.Patient_Revenue__c != null )
{
opp.Oct_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 11)
{
if (ps.Patient_Revenue__c != null )
{
opp.Nov_Revenue__c += ps.Patient_Revenue__c;
}
}
if(ps.CreatedDate.month() == 12)
{
if (ps.Patient_Revenue__c != null )
{
opp.Dec_Revenue__c += ps.Patient_Revenue__c;
}
} }
update opportunityMap.values();
}}
Apex Trigger:
--------------------------
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, before delete,after update, after delete, after undelete ) {
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients); }
Thanks for your help!
Below code works fine for me.
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, after update,after delete )
{
if( Trigger.isInsert || Trigger.isUpdate)
{
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
}
if(Trigger.isDelete)
{
Patient__c [] patients = Trigger.old;
for(Patient__c oldObject :patients) //assume deletion of more than 1 record
{
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
}
}
}
Thank you for your help.
All Answers
The error you getting because of trigger execution its execute each time if any action occurs.
You need to define context type Example isUpdate/isInsert
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, before delete,after update, after delete, after undelete ) {
if(trigger.isAfter && Trigger.IsUpdate)
{
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(trigger.new);
}
}
I think its solve your problem.
Thank you for your reply.
I have tried with below code. Still getting the same error.
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, before delete,after update, after delete, after undelete )
{
if (Trigger.isBefore)
{
if (Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete){
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
} }
if (Trigger.isAfter)
{
if(trigger.isInsert || Trigger.IsUpdate || Trigger.isDelete)
{
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
} } }
Thanks in Advance.
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, before delete,after update, after delete, after undelete )
{
if (Trigger.isBefore)
{
if (Trigger.isInsert || Trigger.isUpdate){
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
} }
if (Trigger.isAfter)
{
if(trigger.isInsert || Trigger.IsUpdate)
{
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
} } }
Master Code
trigger AccountTrigger on Account (before insert,after insert,before update,after update,after delete,before delete,after undelete) {
//Trigger.new//Trigger.NewMap
if(trigger.isInsert && trigger.isBefore){
set<string> accountName=new set<string>();
for(account acc:trigger.new)
{
accountName.add(acc.name);
}
system.debug('Account Name'+accountName);
}
//Trigger.new//Trigger.NewMap
else if(trigger.isInsert && trigger.isAfter)
{
system.debug('After Insert');
}//trigger.new,Trigger.Old,Trigger.newmap
else if(trigger.isUpdate && trigger.isBefore)
{
system.debug('before update');
}//trigger.new,trigger.old,trigger.newmap
else if(trigger.isUpdate && Trigger.isAfter)
{
system.debug('After Update');
}//trigger.old,trigger.oldmap
else if(trigger.isDelete && Trigger.isBefore)
{
system.debug('Before Delete');
}//trigger.old,trigger.oldmap
else if(trigger.isDelete && Trigger.isAfter)
{
system.debug('After Delete');
}//trigger.new
else if(trigger.isUndelete)
{
}
}
Actually my requirement is Doctor__C object contains the Jan_Revenue__c field. One Doctor__C record contains multiple Patient__c related records (Lookup Relationship). We have Revenue__c field on Patient__c record. Jan_Revenue__c field should populate with the sum of all Revenue__c field value of all child Patient__c records (Like Roll up Summary). It is not working in one scenario, that is when one child Patient__C record is deleted then sum is not populated with correct value.
list<Doctor__c> doctorList=new list<Doctor__c>();
Doctor__c doctorObj=new Doctor__c();
if(trigger.isinsert && trigger.isafter)
{
for(Patient__c Patientobj:[select id,name,Revenue__c,Doctor_Name__c,Doctor_Name__r.Jan_Revenue__c from Patient__c where id=:trigger.newMap.keySet()])
{
doctorObj.id=Patientobj.Doctor_Name__c;
//doctorObj.Jan_Revenue__c=Patientobj.Doctor_Name__r.Jan_Revenue__c+Patientobj.Revenue__c;
doctorList.add(doctorObj);
}
}else if(trigger.isupdate && trigger.isafter)
{
for(Patient__c Patientobj:[select id,name,Revenue__c,Doctor_Name__c,Doctor_Name__r.Jan_Revenue__c from Patient__c where id=:trigger.newMap.keySet()])
{
doctorObj.id=Patientobj.Doctor_Name__c;
//doctorObj.Jan_Revenue__c=Patientobj.Doctor_Name__r.Jan_Revenue__c+Patientobj.Revenue__c;
doctorList.add(doctorObj);
}
}
else if(trigger.isdelete && trigger.isafter)
{
for(Patient__c Patientobj:[select id,name,Revenue__c,Doctor_Name__c,Doctor_Name__r.Jan_Revenue__c from Patient__c where id=:trigger.oldmap.keySet()])
{
doctorObj.id=Patientobj.Doctor_Name__c;
//doctorObj.Jan_Revenue__c=Patientobj.Doctor_Name__r.Jan_Revenue__c-Patientobj.Revenue__c;
doctorList.add(doctorObj);
}
}
if(doctorList.size()>0)
update doctorList;
}
:trigger.oldmap.keySet() will Solve your error
Below code works fine for me.
trigger UpdateDoctor on Patient__c (before insert, before update, after insert, after update,after delete )
{
if( Trigger.isInsert || Trigger.isUpdate)
{
list<Patient__C> patients = trigger.new;
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
}
if(Trigger.isDelete)
{
Patient__c [] patients = Trigger.old;
for(Patient__c oldObject :patients) //assume deletion of more than 1 record
{
UpdatedoctorInfo my = new UpdatedoctorInfo();
my.updatedoctor(patients);
}
}
}
Thank you for your help.