+ Start a Discussion
Tina Chang 6Tina Chang 6 

Apex "Else If" statement won't hit. Please help! Thank you!

Hello, there! The highlighted "if" and "else if" statement for the "Percent Complete" field below never execute. Only the "else" statement would work as expected. Does anyone know how to re-write to make it work? Any help will be greatly appreciated!!
if(fldsPopulated == numberFlds){
                PrecentComplete = 100;
            }else if(fldsPopulated == 0){
                PrecentComplete = 0;
            }else{
                precentComplete = ((numberFlds - fldsPopulated)/numberFlds)*100;
            }

Here's the complete Apex Class. Everything else works except for the if statement for the Percent Complete field mentioned above. Thanks in advance for your suggestions!
public with sharing class AccountGradingMethod {

    public static void AccountGrading(Set<String> setAcctGradingIds) {
        List<Account> lstAGS = new List<Account>();
        List<Account> lstUpdateAcct = new List<Account>();
        Map<String, Field_Value__mdt> mapFlds = new Map<String, Field_Value__mdt>();
        String strSOQLAcct;
        String strSOQLAcctGrade;
        Set<String> setFlds = new Set<String>();
        Set<String> setFldName = new Set<String>();
        String strScore;
        Decimal maxPossScore = 0;
        Decimal numberFlds = 0;

        strSOQLAcctGrade = 'SELECT Id';
        for(Field_Value__mdt fn:[SELECT Id, Field_Score_or_Multiplier__c, Picklist_API_Value__c, Use_Greater_then_or_Less_then__c, 
                                        Field_Name__r.Field_API_Name__c, Field_Name__r.Object__c, Field_Name__r.Type_of_Field__c,
                                        Greater_then__c, Less_then__c
                                    FROM Field_Value__mdt]){
            String mapKey;
            setFldName.add(fn.Field_Name__r.Field_API_Name__c);
            if(fn.Field_Name__r.Object__c == 'Account'){
                setFlds.add(fn.Field_Name__r.Field_API_Name__c);
            }
            if(fn.Use_Greater_then_or_Less_then__c){
                mapKey = fn.Field_Name__r.Field_API_Name__c + '-' + fn.Greater_then__c + '-' + fn.Less_then__c;
            }
            else{
                mapKey = fn.Field_Name__r.Field_API_Name__c + '-' + fn.Picklist_API_Value__c.deleteWhitespace();
            }
            mapFlds.put(mapKey, fn);
        }
        for(String f:setFlds){
            strSOQLAcctGrade += ', ' + f;
        }
        numberFlds = setFlds.size();
        strSOQLAcctGrade += ' FROM Account WHERE Id in :setAcctGradingIds';
        System.debug('strSOQLAcctGrade: ' + strSOQLAcctGrade);
        lstAGS = Database.query(strSOQLAcctGrade);
        strScore = System.Label.Max_Possible_Score;
        maxPossScore = Decimal.valueOf(strScore);

        for(Account ags:lstAGS){
            Decimal subTotal = 0;
            Decimal multiplier = 1;
            Decimal decTotal = 0;
            Decimal ecommTotal = 0;
            Decimal ecommMin = 0;
            Decimal ecommMax = 0;
            Decimal fldsPopulated = 0;
            Decimal precentComplete = 0;
            for(Field_Value__mdt fldVal:mapFlds.values()){
                String value;
                Decimal score = 0;
                Boolean skip = false;
                String fld = fldVal.Field_Name__r.Field_API_Name__c;
                String strKey;
                Decimal decValue = 0;
                if(setFldName.contains(fld)){
                    if(fldVal.Field_Name__r.Object__c == 'Account'){
                        value = String.valueOf(ags.get(fld));
                        if(value != '' && value != 'null' && value != null){
                            value = value.deleteWhitespace();
                            if(fldVal.Use_Greater_then_or_Less_then__c){
                                decValue = Decimal.valueOf(value);
                                System.debug('decValue: ' + decValue);
                                if(fldVal.Greater_then__c != null && fldVal.Less_then__c != null){
                                    if(fldVal.Greater_then__c > decValue && fldVal.Less_then__c < decValue){
                                     System.debug('if 1 fldVal.Less_then__c: ' + fldVal.Less_then__c);
                                        strKey = fld + '-' + fldVal.Greater_then__c + '-' + fldVal.Less_then__c;
                                    }
                                    else{
                                        skip = true;
                                    }
                                }
                                if(fldVal.Greater_then__c == null && fldVal.Less_then__c != null){
                                    if(fldVal.Less_then__c > decValue){
                                     System.debug('if 2 fldVal.Less_then__c: ' + fldVal.Less_then__c);
                                        strKey = fld + '-' + fldVal.Greater_then__c + '-' + fldVal.Less_then__c;
                                    }
                                    else{
                                        skip = true;
                                    }
                                }
                                if(fldVal.Greater_then__c != null && fldVal.Less_then__c == null){
                                    System.debug('if 3 fldVal.Greater_then__c : ' + fldVal.Greater_then__c );
                                    if(fldVal.Greater_then__c < decValue){
                                        strKey = fld + '-' + fldVal.Greater_then__c + '-' + fldVal.Less_then__c;
                                    }
                                    else{
                                        skip = true;
                                    }
                                }
                            }
                            else if (!fldVal.Use_Greater_then_or_Less_then__c){
                                strKey = fldVal.Field_Name__r.Field_API_Name__c + '-' + value;
                            }
                            if(!String.isBlank(strKey) && mapFlds.get(strKey) != null){
                                setFldName.remove(fld);
                                score = mapFlds.get(strKey).Field_Score_or_Multiplier__c;
                            }
                            if(score != null){
                                if(fld.startsWithIgnoreCase('e-commerce solutions')){
                                    skip = true;
                                    ecommTotal = ecommTotal + score;
                                    if(ecommMax<score){
                                        ecommMax = score;
                                    }
                                    if(ecommMin>score){
                                        ecommMin = score;
                                    }
                                }
                                if(fldVal.Field_Name__r.Type_of_Field__c == 'Score' && !skip){
                                    System.debug('fld: ' + fld);
                                    System.debug('Score: ' + score);
                                    subTotal = subTotal + score;
                                }
                                if(fldVal.Field_Name__r.Type_of_Field__c == 'Multiplier' && !skip){
                                    System.debug('fld: ' + fld);
                                    System.debug('multiplier: ' + score);
                                    multiplier = multiplier * score;
                                }
                            }
                        }
                        else{
                            fldsPopulated = fldsPopulated + 1;
                            setFldName.remove(fld);
                        }
                    }
                }
            }
            if(ecommTotal>=0){
                subTotal = subTotal + ecommMax;
            }
            else{
                subTotal = subTotal + ecommMin;
            }
            
            if(fldsPopulated == numberFlds){
                PrecentComplete = 100;
            }else if(fldsPopulated == 0){
                PrecentComplete = 0;
            }else{
                precentComplete = ((numberFlds - fldsPopulated)/numberFlds)*100;
            }
            
            System.debug('subtotal: ' +  subTotal);
            subTotal = (subTotal/maxPossScore)*100;
            System.debug('subtotal after: ' +  subTotal);
            System.debug('multiplier: ' + multiplier);
            decTotal = subTotal*multiplier;
            System.debug('decTotal: ' + decTotal );
            Account a = new Account();
                a.Id = ags.Id;
                a.Account_Overall_Grade__c = decTotal;
                a.Percent_Complete__c = precentComplete;
                a.LastScoredDate__c = System.today();
            lstUpdateAcct.add(a);

        }
        if(lstUpdateAcct.size()>0){
            update lstUpdateAcct;
        }
    }
}

 
Lalit Mistry 21Lalit Mistry 21
Hi Tina,

Looking at the if-else logic, it seems you are trying to calculate the percentage of fields populated.
If that is the case then replacing your if-else logic with below code snippet should work for you.
 
precentComplete = (fldsPopulated/numberFlds) * 100;

Mark this as answer if it solves your problem.
 
Tina Chang 6Tina Chang 6
Hi, Lalit! Thanks so much for your prompt assistance! Yes, you understood my intent correctly. I just tried and it did not work, unfortunately.

When an account has no information at all filled out in the Account Intelligence section (fields used for the Account Grading Method), the Percent Complete defaults to 100%, when it should be 0%.  And when the Account Intelligence fields are all filled in, the Percent Complete calculates to 0%, when it should be 100%. Do you know what could be causing this?

The person who wrote the codes has left the company, and I was wondering if something in there is missing/preventing the Percent Complete from working? Thank you!
 
Lalit Mistry 21Lalit Mistry 21
Hi Tina,

You are incrementing the count for fldsPopulated if the value is not equal to null/blank and hence you see the numbers reversed.
If you put fldsPopulated under the if block instead of else then it should work.
Also, a small suggestion, instead of checking for null or blank, you can use String.isBlank(value) which internally checks for null and blank. If this method returns true that means value is blank or null so you'll need to user if(!String.isBlank(value)).

Let me know if that helps.
Tina Chang 6Tina Chang 6
Hi, there!

I'm sorry if I misinterpreted your messsage. I re-wrote the codes as follows: 

The if statement for the precentComplete = 100 won't hit. Everything else works. Have I missed anything? Thank you.
if(fldsPopulated == numberFlds){
   precentComplete = 100;
}if(fldsPopulated == 0){
   PrecentComplete = 0;
}else{
   precentComplete = ((numberFlds - fldsPopulated)/numberFlds)*100;
}
Waqar Hussain SFWaqar Hussain SF
Hi Tina, 

I think the issue is the below line. Please test using the below code line.
 
if(value != '' || value != 'null' || value != null){

 
Tina Chang 6Tina Chang 6
Hi, Waqar! Thank you so much for your help! I did and unfortunately got the below error:

Error element myRule_1_A1 (FlowActionCall).
An Apex error occurred: System.NullPointerException: Attempt to de-reference a null object

I guess I ran out of luck. :(
Waqar Hussain SFWaqar Hussain SF
The error is due to the issue with your process builder, please check by deactivating you process builder. 
Tina Chang 6Tina Chang 6
Hi, Waqar, I cannot deactivate that process builder. Sorry that I should have attached the full error message. That process builder is the one that triggers this Apex class to work.

Sandbox: Error Occurred During Flow "Account_Grading_Process": An Apex error occurred: System.NullPointerException:...
Waqar Hussain SFWaqar Hussain SF
Hi Tina, 
You will be receiving the full detail of the error on the user's email. Can you send me that detail which is received in email while failing the process to run. 
Tina Chang 6Tina Chang 6
Hi, Waqar, my apologies for the late reply. I was able to solve this issue with a workaround. Thank you so much for your kind help!

@Lalit Mistry 21, your help is much appreciated as well! Happy new year!