function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pavan Kumar 1072Pavan 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.
 
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. 
 
Paul S.Paul S.
If we use your example of a parent case with an original order quantity of 50 and a total units ordered of 30, and assume (in this example) that your new child case has a total units ordered of 10, then this line: 
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'))
resolves to:
else if (10 > 50 - 30 - 30)
Since 10 > -10, the statement resolves to true and you enter that block of code.  I believe the usage of "- (Decimal)results.get(cass.ParentId).get('total')" is what's causing your issue as this would seem to be a duplication of "parentdetails.get(cass.ParentId).Total_units_ordered__c." 
 
Pavan Kumar 1072Pavan Kumar 1072
May be you are right in one way.As per logs i understood like kin above example of a parent case with an original order quantity of 50 and a total units ordered of 30,

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

 
Paul S.Paul S.
I think in that case you'd want to check to see if the total equation resolved to a number less than 0.  So: 
else if((Decimal)parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - parentdetails.get(cass.ParentId).Total_units_ordered__c - (Decimal)cass.Total_units_ordered__c < 0)
So, if you tried to order 35: 50 - 30 - 35 = -15, which is less than 0, throw the error 
Pavan Kumar 1072Pavan Kumar 1072
Actually i found my problem through logs.My logic is working fine for insertion but updation in have to change logic.
I had updated like:

For Insertion:
if((Decimal)parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - 
                    (Decimal)parentdetails.get(cass.ParentId).Total_units_ordered__c - (Decimal)results.get(cass.ParentId).get('total')
                    < 0)
For Updation: i need to take difference between old value & current value:
 
if((Decimal)parentdetails.get(cass.ParentId).Original_Ordered_Qty__c - 
                    (Decimal)parentdetails.get(cass.ParentId).Total_units_ordered__c - (Decimal)results.get(cass.ParentId).get('total')
               -(Decimal)cass.Total_units_ordered__c -(Decimal)CaseTriggersOld.get(cass.Id).Total_units_ordered__c
                    < 0)