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

Help please NullPointerException: Attempt to de-reference a null object
The error is on line 40, column 1
I've marked the line BOLD.
Trigger SummingUpMonthlyValue On OTC_Contract_Product_Line_Items__c (After Insert, After Update, After Delete, After UnDelete){
List<Id> accountManagerIds = New List<Id>();
DateTime d = datetime.now();
String monthName = d.format('MMMMM');
System.debug('Current Month Name ****** ' + monthName);
If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
For(OTC_Contract_Product_Line_Items__c contr : Trigger.New){
If(contr.Account_Manager__c != null){
accountManagerIds.add(contr.Account_Manager__c);
}
}
}
If(Trigger.IsDelete){
For(OTC_Contract_Product_Line_Items__c contr : Trigger.Old){
If(contr.Account_Manager__c != null){
accountManagerIds.add(contr.Account_Manager__c);
}
}
}
Map<ID, Double > UserWithTotalInvoicevalue = New Map<ID, Double>();
System.debug('-------------'+accountManagerIds);
AggregateResult[] aggRes = [Select Account_Manager__c,
Sum(OTC_Unit_Price__c) sumac
FROM OTC_Contract_Product_Line_Items__c where Account_Manager__c = :accountManagerIds and OTC_Status__c = 'Active'
Group by Account_Manager__c];//Invoice_Value_Current_Month__c
System.debug('-------------------'+ aggRes);
List<OTC_Contract_Product_Line_Items__c> cpliList = [Select Account_Manager__c,OTC_Unit_Price__c,OTC_Month__c,CreatedDate,OTC_Status__c,Month_Finance_Rev_Rec__c FROM OTC_Contract_Product_Line_Items__c where Account_Manager__c =:accountManagerIds and OTC_Status__c = 'Active'];
Decimal monthlyAchieved = 0;
Decimal DoubleUp = 0;
Set<Id> idToSkip = new Set<Id>();
Integer Month,Year;
for(OTC_Contract_Product_Line_Items__c cpli: Trigger.New){
Month = cpli.CreatedDate.month();
Year = cpli.CreatedDate.Year();
if( (cpli.Account_Manager__c != Trigger.oldMap.get(cpli.id).get('Account_Manager__c')) && (cpli.OTC_Status__c == 'Active') ){
monthlyAchieved = monthlyAchieved + cpli.OTC_Unit_Price__c;
idToSkip.add(cpli.id);
}
}
for(OTC_Contract_Product_Line_Items__c cpli: cpliList){
if(!idToSkip.contains(cpli.id)){
if((cpli.CreatedDate.month() == Month) && (cpli.CreatedDate.year() == Year) && (cpli.OTC_Status__c == 'Active') ){
monthlyAchieved = monthlyAchieved + cpli.OTC_Unit_Price__c;
}
}
if((cpli.OTC_Month__c.month() == Month-1) && (cpli.OTC_Status__c == 'Active') && (cpli.Month_Finance_Rev_Rec__c.month() == Month) ){
DoubleUp = DoubleUp + cpli.OTC_Unit_Price__c;
}
}
for(AggregateResult EveryAR : aggRes )
{
Id accountManagerId = (Id)EveryAR.get('Account_Manager__c');
Decimal TotalInvoiceValue = (decimal)EveryAR.get('sumac');
UserWithTotalInvoicevalue.put(accountManagerId ,TotalInvoiceValue);
}
system.debug('-------'+ UserWithTotalInvoicevalue);
List<Monthly_Target__c> targetFinalListToUpdate = New List<Monthly_Target__c>();
For(Monthly_Target__c targ : [Select Id,No_of_Renewals__c, Portfolio_Total_Revenue__c, Active_Amount_Acheived__c, Monthly_Target_User__c, Start_Date__c
FROM Monthly_Target__c WHERE Monthly_Target_User__c =: accountManagerIds LIMIT 1 ])
{
if( targ.Start_Date__c.month() == Month){
targ.Portfolio_Total_Revenue__c = UserWithTotalInvoicevalue.get(targ.Monthly_Target_User__c);
//targ.No_of_Renewals__c= UserWithTotalInvoicevalue.get(targ.Monthly_Target_User__c );
targ.Active_Amount_Acheived__c = monthlyAchieved;
targ.Double_Up_s_Current_Month__c = DoubleUp;
targetFinalListToUpdate.add(targ);
}
}
try{
If(!targetFinalListToUpdate.IsEmpty()){
update targetFinalListToUpdate;
}
}
catch(Exception e){
System.Debug('Thrown Exception for SummingUpInvoiceValue Trigger Is:: ' + e.getMessage());
}
}
Thanks in Advance.
I've marked the line BOLD.
Trigger SummingUpMonthlyValue On OTC_Contract_Product_Line_Items__c (After Insert, After Update, After Delete, After UnDelete){
List<Id> accountManagerIds = New List<Id>();
DateTime d = datetime.now();
String monthName = d.format('MMMMM');
System.debug('Current Month Name ****** ' + monthName);
If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
For(OTC_Contract_Product_Line_Items__c contr : Trigger.New){
If(contr.Account_Manager__c != null){
accountManagerIds.add(contr.Account_Manager__c);
}
}
}
If(Trigger.IsDelete){
For(OTC_Contract_Product_Line_Items__c contr : Trigger.Old){
If(contr.Account_Manager__c != null){
accountManagerIds.add(contr.Account_Manager__c);
}
}
}
Map<ID, Double > UserWithTotalInvoicevalue = New Map<ID, Double>();
System.debug('-------------'+accountManagerIds);
AggregateResult[] aggRes = [Select Account_Manager__c,
Sum(OTC_Unit_Price__c) sumac
FROM OTC_Contract_Product_Line_Items__c where Account_Manager__c = :accountManagerIds and OTC_Status__c = 'Active'
Group by Account_Manager__c];//Invoice_Value_Current_Month__c
System.debug('-------------------'+ aggRes);
List<OTC_Contract_Product_Line_Items__c> cpliList = [Select Account_Manager__c,OTC_Unit_Price__c,OTC_Month__c,CreatedDate,OTC_Status__c,Month_Finance_Rev_Rec__c FROM OTC_Contract_Product_Line_Items__c where Account_Manager__c =:accountManagerIds and OTC_Status__c = 'Active'];
Decimal monthlyAchieved = 0;
Decimal DoubleUp = 0;
Set<Id> idToSkip = new Set<Id>();
Integer Month,Year;
for(OTC_Contract_Product_Line_Items__c cpli: Trigger.New){
Month = cpli.CreatedDate.month();
Year = cpli.CreatedDate.Year();
if( (cpli.Account_Manager__c != Trigger.oldMap.get(cpli.id).get('Account_Manager__c')) && (cpli.OTC_Status__c == 'Active') ){
monthlyAchieved = monthlyAchieved + cpli.OTC_Unit_Price__c;
idToSkip.add(cpli.id);
}
}
for(OTC_Contract_Product_Line_Items__c cpli: cpliList){
if(!idToSkip.contains(cpli.id)){
if((cpli.CreatedDate.month() == Month) && (cpli.CreatedDate.year() == Year) && (cpli.OTC_Status__c == 'Active') ){
monthlyAchieved = monthlyAchieved + cpli.OTC_Unit_Price__c;
}
}
if((cpli.OTC_Month__c.month() == Month-1) && (cpli.OTC_Status__c == 'Active') && (cpli.Month_Finance_Rev_Rec__c.month() == Month) ){
DoubleUp = DoubleUp + cpli.OTC_Unit_Price__c;
}
}
for(AggregateResult EveryAR : aggRes )
{
Id accountManagerId = (Id)EveryAR.get('Account_Manager__c');
Decimal TotalInvoiceValue = (decimal)EveryAR.get('sumac');
UserWithTotalInvoicevalue.put(accountManagerId ,TotalInvoiceValue);
}
system.debug('-------'+ UserWithTotalInvoicevalue);
List<Monthly_Target__c> targetFinalListToUpdate = New List<Monthly_Target__c>();
For(Monthly_Target__c targ : [Select Id,No_of_Renewals__c, Portfolio_Total_Revenue__c, Active_Amount_Acheived__c, Monthly_Target_User__c, Start_Date__c
FROM Monthly_Target__c WHERE Monthly_Target_User__c =: accountManagerIds LIMIT 1 ])
{
if( targ.Start_Date__c.month() == Month){
targ.Portfolio_Total_Revenue__c = UserWithTotalInvoicevalue.get(targ.Monthly_Target_User__c);
//targ.No_of_Renewals__c= UserWithTotalInvoicevalue.get(targ.Monthly_Target_User__c );
targ.Active_Amount_Acheived__c = monthlyAchieved;
targ.Double_Up_s_Current_Month__c = DoubleUp;
targetFinalListToUpdate.add(targ);
}
}
try{
If(!targetFinalListToUpdate.IsEmpty()){
update targetFinalListToUpdate;
}
}
catch(Exception e){
System.Debug('Thrown Exception for SummingUpInvoiceValue Trigger Is:: ' + e.getMessage());
}
}
Thanks in Advance.
Or you can add check like below
Let us know if this will help you
All Answers
Trigger.oldMap: A map of IDs to the old versions of the sObject records.
This map is only available in update and delete triggers.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm
You have: (After Insert, After Update, After Delete, After UnDelete)
if(Trigger.IsUpdate || Trigger.IsDelete){
// use of the old map
}
Regards
Or you can add check like below
Let us know if this will help you
thanks and regards
priya