0

I have a revenue loss field in case wth specific record type and condition and I need to update based on account and other object with lookup to account based on criteria.

Everything works fine. But for a specific user it's throwing exception sometimes. I am unable to handle exception with try/catch block.

And I didn't get any email when script failed.

My Apex code

public class ARPUCases 
{
    /** @var recordTypeID - is to filter cases with particular record type */
    /** @var Accountstoupdate - is to update the particular accounts */
    /** @var CaseIDs - is to retrive all related case id's */
    public static Set<Account> accSet = new Set<Account>();

    public static void RevenueLoss(List<Case> CaseTriggers) 
    {
        Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Customer_Order'].Id;
        List<Case> CaseIDs = new List<Case>();
        system.debug('Old Values'+CaseTriggers);
        Set<Id> CaseAccountIds = new set<Id>();
        for (Case s : CaseTriggers)
        {
            if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='Cancell' && s.Revenue_effective__c <> null) 
            {
                // Loop through and add caseId's to the list.
                CaseIDs.add(s);
                // Loop through and add AccountId to the accountId Set
                CaseAccountIds.add(s.AccountId);
            }
            if(s.RecordTypeId == recordTypeId && s.Case_Details__c =='Cancell' && s.Revenue_effective__c == null && s.Revenue_Loss__c <> null) 
            {
                s.Revenue_Loss__c=null;
            }
        }

        Map<Id,Account> accountInfoMap=new Map<Id,Account>([SELECT Id,Revenue_Since__c from account where Id=:CaseAccountIds]);
        Integer DaysDifference=0,RevenueContract=0;
        Map<Id,Zuora__Subscription__c> subscriptionByAccountMap= new Map<Id,Zuora__Subscription__c>();

        for(Zuora__Subscription__c subscription:[SELECT Id,Zuora__NextRenewalDate__c,Zuora__Account__c FROM Zuora__Subscription__c WHERE Zuora__Account__c = :CaseAccountIds ORDER BY CreatedDate]){
            subscriptionByAccountMap.put(subscription.Zuora__Account__c,subscription);
        }

        for(Case cs:CaseIDs) 
        {
            System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
            Account a = accountInfoMap.get(cs.AccountId);
            Zuora__Subscription__c sub = subscriptionByAccountMap.get(cs.AccountId);

            if(a!=null && sub!=null){
                //update case only if it is associated to an Account and a Subscription
                if(sub.Zuora__NextRenewalDate__c <> null && cs.Revenue_effective__c <> null ) 
                {
                    DaysDifference=cs.Revenue_effective__c.daysBetween(sub.Zuora__NextRenewalDate__c);
                    System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
                    System.debug('DATA in Zuora__NextRenewalDate__c Before '+ sub.Zuora__NextRenewalDate__c);
                    System.debug('DATA in DaysDifference Calculation '+ DaysDifference);
                }
            }
                    System.debug('DATA in RevenueContract Calculation '+ RevenueContract);
                    System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
                    System.debug('DATA in the account '+ a.Revenue_Since__c);
            try{
                if(a.Revenue_Since__c <> null && cs.Contract_Term__c <> null && cs.Revenue_effective__c <> null) 
                {
                    RevenueContract=cs.Revenue_effective__c.daysBetween(a.Revenue_Since__c.addMonths((Integer)cs.Contract_Term__c));
                    System.debug('DATA in RevenueContract Calculation '+ RevenueContract);
                    System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
                    System.debug('DATA in the account '+ a.Revenue_Since__c);
                }
                if(DaysDifference>=RevenueContract) 
                {
                    cs.Revenue_Loss__c = (cs.Total_units_ordered__c)*DaysDifference*(cs.ARPU__c/31);
                    System.debug('DATA in Revenue_Loss__c Days Difference Calculation '+ cs.Revenue_Loss__c);
                }
                else if(RevenueContract>DaysDifference) 
                {
                    cs.Revenue_Loss__c = (cs.Total_units_ordered__c)*RevenueContract*(cs.ARPU__c/31);
                }
            }
            catch (System.NullPointerException e) { 
            ApexPages.addMessages(e);
                System.debug('Error is: '+e);
            }


        }

    }
}

It's throwing error in line

if(a.Revenue_Since__c <> null && cs.Contract_Term__c <> null && cs.Revenue_effective__c <> null) { }

Debug logs for the user. Debug Logs

User have all the access.

2 Answers 2

1

You are incorrect about the line throwing the error. Note that your debug log shows:

[180] DEBUG | DATA in Revenue_effective Before ... EXCEPTION_THROWN | [181] | System.NullPointerException

The line throwing is:

System.debug('DATA in the account '+ a.Revenue_Since__c);

So...just remove the debug statement.

For the rest of your logic, you can just start off with a null check. Since Boolean logic performs short-circuit evaluation, no attempt will be made to de-reference the null variable:

if (a != null && a.Revenue_Since__c ....)
{
    // do stuff
}

No exception handling required.

2
  • You are right it's throwing error on line 181.But i didn't include whole apex class in this question.@Adrian Larson
    – Pavan tej
    Feb 10, 2017 at 10:46
  • I'm not sure what your point is. It seems clear a is null.
    – Adrian Larson
    Feb 10, 2017 at 10:49
0
if(a!=null && sub!=null){
    //update case only if it is associated to an Account and a Subscription
    if(sub.Zuora__NextRenewalDate__c <> null && cs.Revenue_effective__c <> null ){
            DaysDifference=cs.Revenue_effective__c.daysBetween(sub.Zuora__NextRenewalDate__c);
            System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
            System.debug('DATA in Zuora__NextRenewalDate__c Before '+ sub.Zuora__NextRenewalDate__c);
            System.debug('DATA in DaysDifference Calculation '+ DaysDifference);
    }
}
System.debug('DATA in RevenueContract Calculation '+ RevenueContract);
System.debug('DATA in Revenue_effective Before '+ cs.Revenue_effective__c);
System.debug('DATA in the account '+ a.Revenue_Since__c); // last statement
        //your logic

In this code snipet, in last statement, a is null. Its not scoped under the if condition

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .