You need to sign in to do that
Don't have an account?
null pointer Exception In Trigger
employee==>API: junebatch2021__Employee__c
employee payment==>API: junebatch2021__EMP_PAYMENT__c
Both in Lookup r/s where employee is parent.
Now,
employee payment there is a amount field soas many time i enter amount corresponding to there employee it will rollup and Relect
the SUM IN
Total field :API==>junebatch2021__Total__c
WHICH IS CURRENCY FIELD ON PARENT OBJECT WITH DEFAULT VALUE 0.
I AM POSTING ERROR AND BELOW THAT MY HANDLER AND TRIGGER,,,
IF ANY ONE CAN GIVE SOME VALUABLE FEEDBACK ,,THAT WOULD BE VERY HELPFULL.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger junebatch2021.deleteAction caused an unexpected exception, contact your administrator: junebatch2021.deleteAction: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.junebatch2021.deleteAction.del: line 3, column 1
Handler:
public class EmpLaymentHelper {
public static void beforeInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
for(junebatch2021__EMP_PAYMENT__c p:payments){
if(p.junebatch2021__Employee__c==null){
p.junebatch2021__Employee__c.addError('SELECT THE EMPLOYEE');
}
}
}
public static void afterInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
Map<Id,List<junebatch2021__EMP_PAYMENT__c>> payMap=new Map<Id,List<junebatch2021__EMP_PAYMENT__c>>();
try{
for(junebatch2021__EMP_PAYMENT__c p:payments){
ID empId=p.junebatch2021__Employee__c;
Boolean flag=payMap.containsKey(empId);
List<junebatch2021__EMP_PAYMENT__c> payList=new List<junebatch2021__EMP_PAYMENT__c>();
if(flag==True){
payList=payMap.get(empId);
payList.add(p);
payMap.put(empId,payList);
}else{
payList.add(p);
payMap.put(empId,payList);
}
}
//LOGIC OF SUM IN CHILD
Map<Id,Decimal> empMap=new Map<Id,Decimal>();
for(Id key:payMap.keySet()){
List<junebatch2021__EMP_PAYMENT__c> pays=payMap.get(key);
Decimal sum=0;
for(junebatch2021__EMP_PAYMENT__c pa:pays){
sum=sum+pa.junebatch2021__EMP_AMOUNT__c;
}
empMap.put(key,sum);
}
//NOW REFLECT THAT UPDATE IN PARENT
List<junebatch2021__Employee__c> employees=[Select id,junebatch2021__Total__c from junebatch2021__Employee__c where id in:empMap.keySet()];
for(junebatch2021__Employee__c e:employees){
e.junebatch2021__Total__c=e.junebatch2021__Total__c+empMap.get(e.Id);
}
update employees;
}
catch(Exception e){
e.getMessage();
}
}
}
Trigger:
trigger EmpLaymentHelper on junebatch2021__EMP_PAYMENT__c (before insert,after insert,after update,after delete) {
if(Trigger.isBefore && Trigger.isInsert){
EmpLaymentHelper.beforeInsert(Trigger.new);
}
else{
if(Trigger.isAfter && Trigger.isInsert){
EmpLaymentHelper.afterInsert(Trigger.new);
}
}
}
employee payment==>API: junebatch2021__EMP_PAYMENT__c
Both in Lookup r/s where employee is parent.
Now,
employee payment there is a amount field soas many time i enter amount corresponding to there employee it will rollup and Relect
the SUM IN
Total field :API==>junebatch2021__Total__c
WHICH IS CURRENCY FIELD ON PARENT OBJECT WITH DEFAULT VALUE 0.
I AM POSTING ERROR AND BELOW THAT MY HANDLER AND TRIGGER,,,
IF ANY ONE CAN GIVE SOME VALUABLE FEEDBACK ,,THAT WOULD BE VERY HELPFULL.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger junebatch2021.deleteAction caused an unexpected exception, contact your administrator: junebatch2021.deleteAction: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.junebatch2021.deleteAction.del: line 3, column 1
Handler:
public class EmpLaymentHelper {
public static void beforeInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
for(junebatch2021__EMP_PAYMENT__c p:payments){
if(p.junebatch2021__Employee__c==null){
p.junebatch2021__Employee__c.addError('SELECT THE EMPLOYEE');
}
}
}
public static void afterInsert(List<junebatch2021__EMP_PAYMENT__c> payments){
Map<Id,List<junebatch2021__EMP_PAYMENT__c>> payMap=new Map<Id,List<junebatch2021__EMP_PAYMENT__c>>();
try{
for(junebatch2021__EMP_PAYMENT__c p:payments){
ID empId=p.junebatch2021__Employee__c;
Boolean flag=payMap.containsKey(empId);
List<junebatch2021__EMP_PAYMENT__c> payList=new List<junebatch2021__EMP_PAYMENT__c>();
if(flag==True){
payList=payMap.get(empId);
payList.add(p);
payMap.put(empId,payList);
}else{
payList.add(p);
payMap.put(empId,payList);
}
}
//LOGIC OF SUM IN CHILD
Map<Id,Decimal> empMap=new Map<Id,Decimal>();
for(Id key:payMap.keySet()){
List<junebatch2021__EMP_PAYMENT__c> pays=payMap.get(key);
Decimal sum=0;
for(junebatch2021__EMP_PAYMENT__c pa:pays){
sum=sum+pa.junebatch2021__EMP_AMOUNT__c;
}
empMap.put(key,sum);
}
//NOW REFLECT THAT UPDATE IN PARENT
List<junebatch2021__Employee__c> employees=[Select id,junebatch2021__Total__c from junebatch2021__Employee__c where id in:empMap.keySet()];
for(junebatch2021__Employee__c e:employees){
e.junebatch2021__Total__c=e.junebatch2021__Total__c+empMap.get(e.Id);
}
update employees;
}
catch(Exception e){
e.getMessage();
}
}
}
Trigger:
trigger EmpLaymentHelper on junebatch2021__EMP_PAYMENT__c (before insert,after insert,after update,after delete) {
if(Trigger.isBefore && Trigger.isInsert){
EmpLaymentHelper.beforeInsert(Trigger.new);
}
else{
if(Trigger.isAfter && Trigger.isInsert){
EmpLaymentHelper.afterInsert(Trigger.new);
}
}
}
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger junebatch2021.deleteAction caused an unexpected exception, contact your administrator: junebatch2021.deleteAction: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.junebatch2021.deleteAction.del: line 3, column 1
Can you share the code of junebatch2021.deleteAction.del: line 3
Handler:
public class deleteAction {
public static void del(Map<Id,junebatch2021__Employee__c> empMap){
Set<Id> empId=empMap.keyset();//STORE ID's
List<junebatch2021__EMP_PAYMENT__c> payments=[select id from junebatch2021__EMP_PAYMENT__c where id in: empId];
delete payments;
}
}
Trigger:
trigger deleteAction on junebatch2021__Employee__c (Before insert) {
deleteAction.del(Trigger.oldMap);
}
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Bt the issues is sum of AMOUNT on Child Object Is Not Reflecting On the Total field in Parent Object
are you getting this error again System.NullPointerException: Attempt to de-reference a null object
But i put my efforts on your error and it's solved, so you should mark as the best answer.
and if you have different issue the please create a new question