0

If condition in a trigger has weird results. My condition is, if there are no values from my Set collection do not proceed. Even my debug statement shows NULL value. why this is happening??

if(triggeredRecords.size()>0){
        for (Funding_Cycles_TA__c item: triggeredRecords)
            if(item.Achieved_Credits_to_Rpting_Yr_End__c>=0 && 
               (trigger.oldMap.get(item.Id).Achieved_Credits_to_Rpting_Yr_End__c !=item.Achieved_Credits_to_Rpting_Yr_End__c)){
                   affectedRecords.add(item);
                   affectedFCTAIds.add(item.id);
                   affectedAccounts.add(item.Company_ID__c);
                   affectedPareantAccount.add(item.Parent_Company_ID__c);
                   affectedeportingYr.add(item.Reporting_Year__c);
                   AchievedCreditYrEndOld = trigger.oldMap.get(item.Id).Achieved_Credits_to_Rpting_Yr_End__c;
                   AchievedCreditYrEndNew =  item.Achieved_Credits_to_Rpting_Yr_End__c;  
               }
    }
    System.debug('affected TAFAID '+ affectedFCTAIds);
    System.debug('affected account '+ affectedAccounts);
    System.debug('affected parent '+ affectedPareantAccount);
    System.debug('affected Year '+ affectedeportingYr);
    System.debug('affected Records '+ affectedRecords);

if(affectedAccount != NULL){
        System.debug('affected Account '+ affectedAccount);
        List<Funding_Cycle_Companies__c> fcaParentList = [SELECT Reporting_Year__c,Achieved_Credits_to_Year_End__c 
                                                          FROM Funding_Cycle_Companies__c WHERE Parent_Company_ID__c IN: affectedPareantAccount];

Map<Date, Funding_Cycle_Companies__c> FundingCycleParentCompanyMap = new Map<Date, Funding_Cycle_Companies__c>();  
        //Adding Funding Cycle TA records to Map   
        if(affectedRecords != NULL && affectedRecords.size()>0){
            for (Funding_Cycle_Companies__c fca:fcalist){
                FundingCycleParentCompanyMap.put(fca.Reporting_Year__c, fca);
            }
        } 
        System.debug('new credit '+ AchievedCreditYrEndNew);
        System.debug('old credit '+ AchievedCreditYrEndOld);

The statements inside for loops goes through irrespective of the condition

  • I have add more code. Yes, it should get NULL pointer exception. But the debug statement gives NULL value and it is correct. My question is why statements inside if condition executes if the condition return no value – Jay Weera Oct 11 '18 at 1:13
1

Presumably, you allowed your indentation to trick you. This code right here doesn't work the way you expect it does:

if(triggeredRecords.size()>0){
        for (Funding_Cycles_TA__c item: triggeredRecords)
            if(item.Achieved_Credits_to_Rpting_Yr_End__c>=0 && 
               (trigger.oldMap.get(item.Id).Achieved_Credits_to_Rpting_Yr_End__c !=item.Achieved_Credits_to_Rpting_Yr_End__c)){
                   affectedRecords.add(item);
                   affectedFCTAIds.add(item.id);
                   affectedAccounts.add(item.Company_ID__c);
                   affectedPareantAccount.add(item.Parent_Company_ID__c);
                   affectedeportingYr.add(item.Reporting_Year__c);
                   AchievedCreditYrEndOld = trigger.oldMap.get(item.Id).Achieved_Credits_to_Rpting_Yr_End__c;
                   AchievedCreditYrEndNew =  item.Achieved_Credits_to_Rpting_Yr_End__c;  
               }
    }

If you count the number of { and }, you'll notice that there's a missing {:

    for (Funding_Cycles_TA__c item: triggeredRecords) { // Forgotten {

As such, you have an extra } somewhere else in your code that's also misplaced. The last quoted } in the first block ends the if statement, so it results in everything afterwards executing.

So, two things are important here:

Always Check Code Blocks

Always make sure that every if, do, while, and for have a matching open and close curly brace. Always.

Always Indent Appropriately

If you use a modern IDE, each curly brace will be highlighted with the matching pair. Always make sure that they match up. If you try saving to the server, and it complains about a missing } or {, always take the time to match them up correctly. Also, use the auto-indent feature of your IDE (e.g. in Developer Console, select all and then Shift-Tab to auto-indent). If it indents weird, double-check your curly braces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.