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

Apex Trigger Exception
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ItemizationAfterTrigger caused an unexpected exception, contact your administrator: ItemizationAfterTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0vS0000002vAnqIAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have sufficient Amount for reimbursement.: []: Class.ItemizationClass.UpdateApprovedandPending: line 41, column 1
I am getting the above the error.I have trigger on the object to update the value of the status to approved but if the amount is negative i have a negative so any suggestions on how to handle this would appreciated.
Ya..the validation rule is something that says amount>0.It does not allow negative values.
So that validation rule may effect the field updation on trigger..
Please share the trigger code & validation formula
Here is my code:
TriggerCode:
trigger ItemizationAfterTrigger on Itemization__c (after insert, after update) {
ItemizationClass itemclass = new ItemizationClass();
Set<String> ApproveandPendingMarketingDollarIds = new Set<String>();
Set<String> AssigntoCommissionsQueueCaseIds = new Set<String>();
Set<String> RejectedCaseIds = new Set<String>();
Map<Id, Set<string>> CaseStatusMap = new Map<Id, Set<string>>();
set<string> CaseStatusSet;
for(Itemization__c item : Trigger.New){
if((Trigger.isInsert || (Trigger.isUpdate && (item.Approval_Status__c != Trigger.oldmap.get(item.ID).Approval_Status__c && item.Approval_Status__c != 'Pending'))) && item.MarketingDollarID__c != null){
ApproveandPendingMarketingDollarIds.add(String.valueof(item.MarketingDollarID__c).substring(0,15));
CaseStatusSet = CaseStatusMap.get(item.Case__c);
if(CaseStatusSet == null) {
CaseStatusSet = new set<string>();
CaseStatusMap.put(item.Case__c, CaseStatusSet);
}
CaseStatusSet.add(item.Approval_Status__c.ToUppercase());
}
List<Itemization__c> ItemizationList = [SELECT Id, Case__c, Approval_Status__c
FROM Itemization__c
WHERE Case__c IN : CaseStatusMap.keyset()
];
for(Itemization__c Item : ItemizationList) {
if(item.Approval_Status__c != null) {
CaseStatusSet = CaseStatusMap.get(item.Case__c);
if(CaseStatusSet == null) {
CaseStatusSet = new set<string>();
CaseStatusMap.put(item.Case__c, CaseStatusSet);
}
CaseStatusSet.add(item.Approval_Status__c.ToUppercase());
}
}
system.debug('CaseStatusMap: ' + CaseStatusMap);
for(Id caseId : CaseStatusMap.keyset()) {
set<string> NewcasestatusSet = CaseStatusMap.get(caseId);
system.debug('NewcasestatusSet:' + NewcasestatusSet);
if(NewcasestatusSet != null && NewcasestatusSet.size() > 0) {
if(!NewcasestatusSet.contains('PENDING')) {
if(NewcasestatusSet.contains('APPROVED')) {
AssigntoCommissionsQueueCaseIds.add(caseId);
} else {
RejectedCaseIds.add(caseId);
}
}
}
}
if(ApproveandPendingMarketingDollarIds.size() > 0)
itemclass.UpdateApprovedandPending(ApproveandPendingMarketingDollarIds);
if(AssigntoCommissionsQueueCaseIds.size() > 0)
itemClass.AssignCasetoCommissionsQueue(AssigntoCommissionsQueueCaseIds);
if(RejectedCaseIds.size() > 0)
itemClass.ChangeCaseStatus(RejectedCaseIds);
}
Itemization class:
public class ItemizationClass {
public void UpdateApprovedandPending(Set<String> MktDolIds){
Marketing_Dollar_Allocation__c mktDollar;
Map<Id,Marketing_Dollar_Allocation__c> mktDollarMap = new Map<Id,Marketing_Dollar_Allocation__c>();
for(Itemization__c item : [select ID,MarketingDollarID__c,Amount__c,Approval_Status__c from Itemization__c where MarketingDollarID__c IN: MktDolIds AND Approval_Status__c != 'Rejected']){
if(mktDollarMap.containsKey(item.MarketingDollarID__c)){
mktDollar = mktDollarMap.get(item.MarketingDollarID__c);
if(item.Approval_Status__c == 'Pending')
mktDollar.Pending__c = mktDollar.Pending__c + item.Amount__c;
else if(item.Approval_Status__c == 'Approved' || item.Approval_Status__c == 'Credited')
mktDollar.Total_Reimbursed__c = mktDollar.Total_Reimbursed__c + item.Amount__c;
mktDollarMap.put(item.MarketingDollarID__c,mktDollar);
}else{
mktDollar = new Marketing_Dollar_Allocation__c(ID = item.MarketingDollarID__c);
mktDollar.Pending__c = item.Approval_Status__c == 'Pending'? item.Amount__c : 0;
mktDollar.Total_Reimbursed__c = item.Approval_Status__c == 'Approved' || item.Approval_Status__c == 'Credited' ? item.Amount__c : 0;
mktDollarMap.put(item.MarketingDollarID__c,mktDollar);
}
}
if(mktDollar.Total_Remaining__c>=0)
Update mktDollarMap.values();
}
}
Validation Rule:
Total_Remaining__c <0
Totol Remaining is formula field which Total_Awarded__c - ( Total_Reimbursed__c )
errors:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ItemizationAfterTrigger caused an unexpected exception, contact your administrator: ItemizationAfterTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id a0vS0000002vAnqIAE; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have sufficient Amount for reimbursement.: []: Class.ItemizationClass.UpdateApprovedandPending: line 41, column 1
Hi Kumar,
Check for the Amount__c>0 in the first if condition.