You need to sign in to do that
Don't have an account?
Pavan Kumar 1072
How to add error in apex class with particular criteria
I have a requirement in cases relationship with child cases.
In parent case i have a main field called orginal order Quanity and total units order.
And for every case we have total units order field.
Like Suppose i have parent P1 case with following values
P1 orginal order quantity = 50,total units ordered=30;
Difference between is 20.
So, i need to restrict my support reps to create any child cases for above parent would be equals or less than difference means 20
So i use aggregate query and with parent case values.
But the problem is it is not allowing me to save child case if i give less value equals 20 or less than too.
Please Let me know your suggestions we are went wrong.
In parent case i have a main field called orginal order Quanity and total units order.
And for every case we have total units order field.
Like Suppose i have parent P1 case with following values
P1 orginal order quantity = 50,total units ordered=30;
Difference between is 20.
So, i need to restrict my support reps to create any child cases for above parent would be equals or less than difference means 20
So i use aggregate query and with parent case values.
But the problem is it is not allowing me to save child case if i give less value equals 20 or less than too.
public static void ChildOrder(List<Case> CaseTriggers) { Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id; List<Case> CaseIDs = new List<Case>(); Decimal add=0,sum=0; system.debug('Old Values'+CaseTriggers); List<Id> ParentIds =new List<ID>(); Map<Id,case> parentdetails= new Map<Id,case>(); for (Case s : CaseTriggers) { if(s.RecordTypeId == recordTypeId && s.ParentID != null) { // Loop through and add caseId's to the list. CaseIDs.add(s); ParentIDs.add(s.ParentId); } } Map<Id, AggregateResult> results = new Map<Id, AggregateResult>( [SELECT ParentID Id, SUM(Total_units_ordered__c) total FROM Case WHERE ParentID= :ParentIDs GROUP BY ParentID]); if(ParentIDs !=null && !ParentIDs.isEmpty()) { for(case cd:[Select id,FSS_Rep__c,FSS_TL__c,Total_units_ordered__c,Original_Ordered_Qty__c from case where id =:ParentIds]) { parentdetails.put(cd.id, cd); } } for(case cass: CaseTriggers) { if(results.get(cass.ParentID) != null) { if((Decimal)cass.Total_units_ordered__c <= (Decimal)parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - parentdetails.get(cass.ParentId).Total_units_ordered__c-(Decimal)results.get(cass.ParentId).get('total')) { cass.FSS_Rep__c=parentdetails.get(cass.ParentId).FSS_Rep__c; cass.FSS_TL__c=parentdetails.get(cass.ParentId).FSS_TL__c; } else if((Decimal)cass.Total_units_ordered__c > (Decimal)(parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - parentdetails.get(cass.ParentId).Total_units_ordered__c)-(Decimal)results.get(cass.ParentId).get('total')) { add=(Decimal)(parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - parentdetails.get(cass.ParentId).Total_units_ordered__c)-(Decimal)results.get(cass.ParentId).get('total'); cass.addError('You can ship only'+' '+add+' '+'unit for this order'); } } } }
Please Let me know your suggestions we are went wrong.
If i try to create a child case with 20 total units ordered.
In (Decimal)results.get(cass.ParentId).get('total') instead of 0 it is coming like 20 also taking in consideration because it is already a child case.
Like this (20 > 50 - 30 -20)
How to handle above scenario
I had updated like:
For Insertion: For Updation: i need to take difference between old value & current value: