Search code examples
salesforcesalesforce-lightningsalesforce-communities

Error: Compile Error: Expression must be a list type: String at line 446 column 44


i am trying validate the insurance number in salesforce , but i am getting below error

insurance = insurance.replace('A','01');

                Integer sum = 0;
                Integer numDigits = insurance.length() - 1;
                Integer cle = parseInt(insurance[numDigits],10);  =====>Error

                for (Integer i = 0; i < numDigits; i++) {
                    Integer digit = parseInt(insurance[i], 10); =====> Error
                    if (math.mod(i,2) != 0) {

                        digit *= 2;
                    }
                    sum += digit > 9 ? digit - 9 : digit;
                }

                if (math.mod(sum ,10) != cle) {
                    return false;
                } else {
                    return true;
                }
            }

Solution

  • Use substring method of String to get the character at index.

    Also, there is no parseInt in Apex. Use Integer.valueOf instead.

    So, the first part of your code should be

    Integer numDigits = insurance.length() - 1;
    Integer cle = Integer.valueOf(insurance.substring(numDigits));
    

    References: