1

I have below trigger which passes string values to below class to do some changes for update. Please find the trigger. Trigger:

trigger AccountFieldAutoUpdates on Account (before insert, before update) 
{

    String CalcCombinedABS = '';
    for(Account acc:trigger.new)
    {
        // Auto capitalization and translation
        if(acc.Ispersonaccount==true)
        {  
           String TempStr='';
           if(acc.FirstName!=null)
           {
            TempStr=AbbvieUtilitiesclass.translate(acc.FirstName);
            acc.FirstName=AbbvieUtilitiesclass.initCaps(TempStr);            
           }

            // Middle name
            if(acc.Middle_vod__c!=null)
            {
            TempStr=AbbvieUtilitiesclass.translate(acc.Middle_vod__c);
            acc.Middle_vod__c=AbbvieUtilitiesclass.initCaps(TempStr); 
            }
            //  Last Name            
            TempStr=AbbvieUtilitiesclass.translate(acc.LastName);
            acc.LastName=AbbvieUtilitiesclass.initCaps(TempStr); //// Error line
        }            
        else 
        {// Business Accounts            
            String sName = AbbvieUtilitiesclass.translate(acc.Name);
            sName= sName.substring(0,1).toUppercase() + sName.substring(1,sName.length());            
            acc.Name = sName;
        }


        //Auto Update HCI/HCO
        if(acc.IsPersonAccount)
            acc.HCP_HCI_abv__c = 'HCP';
        else
            acc.HCP_HCI_abv__c = 'HCI/HCO';

        // Auto Update ABS Concat
        CalcCombinedABS  = '';
        if(acc.Abbott_Best_Specialty_ABT__c != null && acc.Abbott_Best_Specialty_ABT__c != '')
        {
            CalcCombinedABS = acc.Abbott_Best_Specialty_ABT__c;
        } 
        if(acc.Abbott_Best_Specialty_Description_ABT__c != null && acc.Abbott_Best_Specialty_Description_ABT__c!= '')
        {
            if(CalcCombinedABS  == '')
            {
                CalcCombinedABS =acc.Abbott_Best_Specialty_Description_ABT__c;
            } 
            else
            {
                CalcCombinedABS  = CalcCombinedABS + '-' + acc.Abbott_Best_Specialty_Description_ABT__c;
            }         
        }
        if(acc.Combined_ABS_abv__c != CalcCombinedABS)
            acc.Combined_ABS_abv__c = CalcCombinedABS;

        // Added for 71 for Approved Email Consent Update upon Record Type Change

        String newAccRecordType = acc.Account_Record_Type_Name_ABT__c;         

        if(trigger.IsInsert && newAccRecordType != 'Prescriber' && newAccRecordType != 'Non-Prescriber with ABS')
            acc.Approved_Email_Opt_Type_vod__c = 'Never_vod';

        if(trigger.IsUpdate)
        {
            Account oldAcc = trigger.oldMap.get(acc.Id); 
            String oldAccRecordType = oldAcc.Account_Record_Type_Name_ABT__c;

            Boolean isPersonAcc = acc.IsPersonAccount;

            if(oldAccRecordType  != newAccRecordType && isPersonAcc) 
            {
                if(newAccRecordType == 'Prescriber' || newAccRecordType == 'Non-Prescriber with ABS')
                    acc.Approved_Email_Opt_Type_vod__c = '';
                else
                    acc.Approved_Email_Opt_Type_vod__c = 'Never_vod';
            }
        }
    }

}

Class:

public class AbbvieUtilitiesclass
{
    public static Map<String,String> M;
    public static String initCaps(String inputString)
    {
        if(inputString!= null)
        {
            system.debug('inputstring@'+inputstring); 

            if(!inputString.contains(' '))
            {
                String CapsName= inputString;
                      CapsName=(Capsname.substring(0,1)).toUppercase()+(CapsName.substring(1,CapsName.length()));
                      system.debug('CapsName@'+CapsName);
                return CapsName;
            }
            else 
            {
                 List<String> CapsName= inputString.split(' ');
                 String RetName='';
                    for(Integer i=0;i<CapsName.size();i++)
                    {
                        CapsName[i] = (CapsName[i].substring(0,1)).toUppercase() +(CapsName[i].substring(1,CapsName[i].length()));   ////  Error line
                        if(i>0)
                        {
                            RetName=RetName+' '+CapsName[i];
                        }
                        else RetName=CapsName[i];    
                    }
                    system.debug('Retname@'+Retname);
                    return Retname;
             }

         }
           //code added
         else
         {
             return null;
         }    
    }

    public static String translate(String str)
    {
        if(str==null)
        return null;    

        String TransMap = Abbvie_Custom_Settings__c.getValues('Main').Translation_Map_abv__c;

        if(TransMap != null && TransMap != '')
        {
            List<String> maps = TransMap.split(',');

            for (String key: maps)
            {
                List<String> keyval = key.split(':');
                str = str.replace(keyval[0],keyval[1]);//Get CORR Value
            }
        }
        return str;//english       
    }         
}

Please find the error I am facing now.

Apex script unhandled trigger exception by user/organization: 005F0000002gRMP/00DM0000001eGhW Source organization: 00DA0000000cASY (null) AccountFieldAutoUpdates: execution of BeforeUpdate

caused by: System.StringException: Ending position out of bounds: 1

Class.AbbvieUtilitiesclass.initCaps: line 23, column 1 Trigger.AccountFieldAutoUpdates: line 32, column 1

1

To add to theGreatDanton's answer, you could simplify your loop immensely by using some of Salesforce's built-in string methods.

Your initcaps method could look something like this (missing the extra else):

if(inputString!= null){
   if(!inputString.contains(' ')){
       return inputString.capitalize();
   }
   else {
       List<String> splitInputs= inputString.split(' ');
       for (String str : splitInputs){
           if (str != '' && str != null){
               str.capitalize();
           }
       }
       return String.join(splitInputs,' ');
   }
}
| improve this answer | |
0

This is happening because when the inputString is split there is a possibility that values in the list may have blank values and then you do a substring on each of the values then the ones with the blanks will endup throwing the error as there's nothing to a substring of.

As a test you can set the input string to ' abc abcd abcde xyz'; and run that piece in the dev console to reproduce the issue.

And for your case just adding a simple if condition should do the trick

if(CapsName[i] != ''){
       CapsName[i] = (CapsName[i].substring(0,1)).toUppercase() +(CapsName[i].substring(1,CapsName[i].length()));   ////  Error line
       if(i>0)
        {
           RetName=RetName+' '+CapsName[i];
        }
        else RetName=CapsName[i];    
        }
 }|
| improve this answer | |

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.