Extracting values from a pipe delimited text file

A Trigger to extract key value pairs from a pipe delimited text file

This Trigger is incomplete. I just wanted to keep it for nostalgic purposes as it's the most complex/annoying one I've worked on so far.

I had a requirement to break down a field called Key Event String into a list of values. Then, to loop through the values searching for specific terms and adding up the totals for each.

It all sounded so simple but this particular trigger kept bringing up totals from one value and listing them against another. So Custom State 1 would actually be the total of Custom State 2.

I spent hours debugging but eventually decided to rewrite it. I'm pretty sure the answer lay where I was setting the value variable but rewriting gave me an opportunity to tidy it up and move the handling into a class.




So, here's the original trigger for posterity, but I'll be back with a (hopefully) fully functional one.

 

trigger TaggedCustomState on NVMStatsSF__NVM_Agent_Summary__c (before update) {
    /* 
In this trigger we need to look for a list of Custom States in a string and add up the totals for each.
Example Key Event field
53344Custom State 320:20|
53344Custom State 321:21|
53344Custom State 322:22|
53344Custom State 323:23|
53344Custom State 324:24|
53344Custom State 325:25|
53344Custom State 326:26|
*/
    
    //define a string constant for the regex-escaped | character
    final String CHKSTR_END_CHAR_REGEX = '\\|';
    //Then define the custom states and the duration counter for each
    
    //Custom State 1
    String CUSTOM_STATE1 = 'None';
    String CUSTOM_STATE1Value = 'None';
    Integer CUSTOM_STATE1TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS1 = NVMCustomStates__c.getInstance('1');
    if (CUSTOM_STATE_SETTINGS1 !=Null ) {
        if (CUSTOM_STATE_SETTINGS1.Label__c != Null) {
            CUSTOM_STATE1 = CUSTOM_STATE_SETTINGS1.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS1.Value__c != Null) {    
            CUSTOM_STATE1Value = CUSTOM_STATE_SETTINGS1.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    //Custom State 2
    String CUSTOM_STATE2 = 'None';
    String CUSTOM_STATE2Value = 'None';
    Integer CUSTOM_STATE2TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS2 = NVMCustomStates__c.getInstance('2');
    if (CUSTOM_STATE_SETTINGS2 !=Null ) {
        if (CUSTOM_STATE_SETTINGS2.Label__c != Null) {
            CUSTOM_STATE2 = CUSTOM_STATE_SETTINGS2.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS2.Value__c != Null) {    
            CUSTOM_STATE2Value = CUSTOM_STATE_SETTINGS2.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    
    //Custom State 3
    String CUSTOM_STATE3 = 'None';
    String CUSTOM_STATE3Value = 'None';
    Integer CUSTOM_STATE3TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS3 = NVMCustomStates__c.getInstance('3');
    if (CUSTOM_STATE_SETTINGS3 !=Null ) {
        if (CUSTOM_STATE_SETTINGS3.Label__c != Null) {
            CUSTOM_STATE3 = CUSTOM_STATE_SETTINGS3.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS3.Value__c != Null) {    
            CUSTOM_STATE3Value = CUSTOM_STATE_SETTINGS3.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    //Custom State 4
    String CUSTOM_STATE4 = 'None';
    String CUSTOM_STATE4Value = 'None';
    Integer CUSTOM_STATE4TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS4 = NVMCustomStates__c.getInstance('4');
    if (CUSTOM_STATE_SETTINGS4 !=Null ) {
        if (CUSTOM_STATE_SETTINGS4.Label__c != Null) {
            CUSTOM_STATE4 = CUSTOM_STATE_SETTINGS4.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS4.Value__c != Null) {    
            CUSTOM_STATE4Value = CUSTOM_STATE_SETTINGS4.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    //Custom State 5
    String CUSTOM_STATE5 = 'None';
    String CUSTOM_STATE5Value = 'None';
    Integer CUSTOM_STATE5TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS5 = NVMCustomStates__c.getInstance('5');
    if (CUSTOM_STATE_SETTINGS5 !=Null ) {
        if (CUSTOM_STATE_SETTINGS5.Label__c != Null) {
            CUSTOM_STATE5 = CUSTOM_STATE_SETTINGS5.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS5.Value__c != Null) {    
            CUSTOM_STATE5Value = CUSTOM_STATE_SETTINGS5.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    //Custom State 6
    String CUSTOM_STATE6 = 'None';
    String CUSTOM_STATE6Value = 'None';
    Integer CUSTOM_STATE6TOTAL = 0;
    NVMCustomStates__c CUSTOM_STATE_SETTINGS6 = NVMCustomStates__c.getInstance('6');
    if (CUSTOM_STATE_SETTINGS6 !=Null ) {
        if (CUSTOM_STATE_SETTINGS6.Label__c != Null) {
            CUSTOM_STATE6 = CUSTOM_STATE_SETTINGS6.Label__c;
        }
        if (CUSTOM_STATE_SETTINGS6.Value__c != Null) {    
            CUSTOM_STATE6Value = CUSTOM_STATE_SETTINGS6.Value__c;
        }
    } //end of overall if - did we find a Custom Setting
    
    
    Integer breakValue = 0; 
    Integer comfortBreakValue = 0;
    Integer lunchValue = 0;
    Integer trainingValue = 0;
    Integer teamMeetingValue = 0;
    
    List customStates = new List();
    //The list we're storing everything in
    
    for (NVMStatsSF__NVM_Agent_Summary__c SS : Trigger.new) {
        
        if (SS.NVMStatsSF__Key_Event_String__c != Null) {
            //Check that there is something in the Key Event String
            
            //If there is something there - lets swap out the Custom State values for human readable versions
            if (CUSTOM_STATE1 != Null && CUSTOM_STATE1Value != Null) {
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE1, CUSTOM_STATE1Value);
            }
            if (CUSTOM_STATE2 != Null && CUSTOM_STATE2Value != Null) {            
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE2, CUSTOM_STATE2Value);
            }
            if (CUSTOM_STATE3 != Null && CUSTOM_STATE3Value != Null) {            
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE3, CUSTOM_STATE3Value);
            }
            if (CUSTOM_STATE4 != Null && CUSTOM_STATE4Value != Null) {            
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE4, CUSTOM_STATE4Value);
            }
            if (CUSTOM_STATE5 != Null && CUSTOM_STATE5Value != Null) {            
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE5, CUSTOM_STATE5Value);
            }
            if (CUSTOM_STATE6 != Null && CUSTOM_STATE6Value != Null) {            
                SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE6, CUSTOM_STATE6Value);
            }
            
            Boolean csCheck = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE1Value);
            if (!csCheck) {
                CUSTOM_STATE1Value = '0';
            }
            Boolean csCheck2 = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE2Value);
            if (!csCheck2) {
                CUSTOM_STATE2Value = '0';
            }
            
            Boolean csCheck3 = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE3Value);
            if (!csCheck3) {
                CUSTOM_STATE3Value = '0';
            }
            Boolean csCheck4 = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE4Value);
            if (!csCheck4) {
                CUSTOM_STATE4Value = '0';
            }
            Boolean csCheck5 = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE5Value);
            if (!csCheck5) {
                CUSTOM_STATE5Value = '0';
            }
            
            Boolean csCheck6 = SS.NVMStatsSF__Key_Event_String__c.contains(CUSTOM_STATE6Value);
            if (!csCheck6) {
                CUSTOM_STATE6Value = '0';
            }
            Boolean csCheck7 = SS.NVMStatsSF__Key_Event_String__c.contains('Comfort Break');
            if (!csCheck7) {
                comfortBreakValue = 0;
            }
            Boolean csCheck8 = SS.NVMStatsSF__Key_Event_String__c.contains('Break');
            if (!csCheck8) {
                breakValue = 0;
            }
            Boolean csCheck9 = SS.NVMStatsSF__Key_Event_String__c.contains('Lunch');
            if (!csCheck9) {
                lunchValue = 0;
            }
            Boolean csCheck10 = SS.NVMStatsSF__Key_Event_String__c.contains('Training');
            if (!csCheck10) {
                trainingValue = 0;
            }
            Boolean csCheck11 = SS.NVMStatsSF__Key_Event_String__c.contains('Team Meeting');
            if (!csCheck11) {
                teamMeetingValue = 0;
            }
            
            
            
            customStates = (''+SS.NVMStatsSF__Key_Event_String__c).split(CHKSTR_END_CHAR_REGEX);
            //Break initial string down using the variable CHKSTR_END_CHAR_REGEX
            System.debug('Size of new list ' + customStates.size());
            //All details now in list - in human readable form
            
            String label; //Custom State
            String value; //Number of seconds        
            
            if (customStates.size() > 1 ) {
                //Check that we created a list greater than 1
                System.debug('Looping through customStates list');
                for (Integer i = 0; i < customStates.size(); i++) {
                    //Loop through new list
                    
                    String temp = SS.NVMStatsSF__Key_Event_String__c;
                    System.debug('customState is ' + customStates[i] + 'at position ' + i);
                    label = customStates[i].substring(5);
                    System.debug('label variable is ' + Label);
                    value = customStates[i].right(5);
                    value = value.replaceAll('[^0-9.]', '');
                    System.debug('value variable is ' + value);
                    
                    //Search against CUSTOM_STATE variables - if we find a match, extra duration and increment for overall total
                    //Repeat for as many custom states as required 
                    Boolean i1;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i1 = customStates[i].contains(CUSTOM_STATE1Value);
                    if (i1) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE1Value);
                        CUSTOM_STATE1TOTAL = CUSTOM_STATE1TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE1TOTAL);
                    }
                    if (!i1) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE1Value);
                    }
                    Boolean i2;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i2 = customStates[i].contains(CUSTOM_STATE2Value);
                    if (i2) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE2Value);
                        CUSTOM_STATE2TOTAL = CUSTOM_STATE2TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE2TOTAL);
                    }
                    if (!i2) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE2Value);
                    }
                    Boolean i3;
                    
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i3 = customStates[i].contains(CUSTOM_STATE3Value);
                    if (i3) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE3Value);
                        CUSTOM_STATE3TOTAL = CUSTOM_STATE3TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE3TOTAL);
                    }
                    if (!i3) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE3Value);
                        CUSTOM_STATE3TOTAL = 0;
                    }
                    Boolean i4;
                    
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i4 = customStates[i].contains(CUSTOM_STATE4Value);
                    if (i4) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE4Value);
                        CUSTOM_STATE4TOTAL = CUSTOM_STATE4TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE4TOTAL);
                    }
                    if (!i4) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE4Value);
                    }
                    Boolean i5;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i5 = customStates[i].contains(CUSTOM_STATE5Value);
                    if (i5) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE5Value);
                        CUSTOM_STATE5TOTAL = CUSTOM_STATE5TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE5TOTAL);
                    }
                    if (!i5) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE5Value);
                    }
                    Boolean i6;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i6 = customStates[i].contains(CUSTOM_STATE6Value);
                    if (i6) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE6Value);
                        CUSTOM_STATE6TOTAL = CUSTOM_STATE6TOTAL + integer.valueof(value);
                        System.debug('Total seconds : ' + CUSTOM_STATE6TOTAL);
                    }
                    if (!i6) {
                        System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE6Value);
                    }
                    
                    //Bringing in Minor States from the ContactPad including Break, Comfort Break etc
                    
                    Boolean i7;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i7 = customStates[i].contains('Break');
                    if (i7) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain Break');
                        breakValue = breakValue + integer.valueof(value);
                        System.debug('Total seconds : ' + breakValue);
                    }
                    if (!i7) {
                        System.debug(customStates[i] + ' does not contain Break');
                    }
                    
                    Boolean i8;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i8 = customStates[i].contains('Comfort Break');
                    if (i8) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain Comfort Break');
                        comfortBreakValue = comfortBreakValue + integer.valueof(value);
                        System.debug('Total seconds : ' + comfortBreakValue);
                    }
                    if (!i8) {
                        System.debug(customStates[i] + ' does not contain Comfort Break');
                    }
                    
                    Boolean i9;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i9 = customStates[i].contains('Lunch');
                    if (i9) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain Lunch');
                        lunchValue = lunchValue + integer.valueof(value);
                        System.debug('Total seconds : ' + lunchValue);
                    }
                    if (!i9) {
                        System.debug(customStates[i] + ' does not contain Lunch');
                    }
                    
                    Boolean i10;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i10 = customStates[i].contains('Training');
                    if (i10) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain Training');
                        trainingValue = trainingValue + integer.valueof(value);
                        System.debug('Total seconds : ' + trainingValue);
                    }
                    if (!i9) {
                        System.debug(customStates[i] + ' does not contain Lunch');
                    }
                    
                    Boolean i11;
                    System.debug('Current variable being evaluated ' + customStates[i]);
                    i11 = customStates[i].contains('Team Meeting');
                    if (i11) {
                        value = customStates[i].right(5);
                        value = value.replaceAll('[^0-9.]', '');
                        System.debug(customStates[i] + ' does contain Team Meeting');
                        teamMeetingValue = teamMeetingValue + integer.valueof(value);
                        System.debug('Total seconds : ' + teamMeetingValue);
                    }
                    if (!i11) {
                        System.debug(customStates[i] + ' does not contain Team Meeting');
                    }
                    
                    
                } //end inner list 
                
                //Finally populate totals on the field;
                if (CUSTOM_STATE1TOTAL !=Null) {
                    SS.Custom_State_1__c = CUSTOM_STATE1TOTAL;  
                }
                else if (CUSTOM_STATE1TOTAL ==Null) {
                    SS.Custom_State_1__c = 0;      
                }    
                if (CUSTOM_STATE2TOTAL !=Null) {
                    SS.Custom_State_2__c = CUSTOM_STATE2TOTAL;  
                }
                else if (CUSTOM_STATE2TOTAL ==Null) {
                    SS.Custom_State_2__c = 0;      
                }  
                if (CUSTOM_STATE3TOTAL !=Null) {
                    SS.Custom_State_3__c = CUSTOM_STATE3TOTAL;
                }
                else if (CUSTOM_STATE3TOTAL ==Null) {
                    SS.Custom_State_3__c = 0;      
                }              
                if (CUSTOM_STATE4TOTAL !=Null) {
                    SS.Custom_State_4__c = CUSTOM_STATE4TOTAL;
                }
                else if (CUSTOM_STATE4TOTAL ==Null) {
                    SS.Custom_State_4__c = 0;      
                }              
                if (CUSTOM_STATE5TOTAL !=Null) {
                    SS.Custom_State_5__c = CUSTOM_STATE5TOTAL; 
                }
                else if (CUSTOM_STATE5TOTAL ==Null) {
                    SS.Custom_State_5__c = 0;      
                }              
                if (CUSTOM_STATE6TOTAL !=Null) {
                    SS.Custom_State_6__c = CUSTOM_STATE6TOTAL;  
                }
                else if (CUSTOM_STATE6TOTAL ==Null) {
                    SS.Custom_State_6__c = 0;      
                } 
                if (breakValue > 1 ) {
                    SS.Break__c = breakValue - comfortBreakValue;
                }
                if (comfortBreakValue > 1 ) {
                    SS.Comfort_Break__c = comfortBreakValue;
                }
                
                if (lunchValue > 1 ) {
                    SS.Lunch__c = lunchValue;
                }
                
                if (trainingValue > 1 ) {
                    SS.Training__c= trainingValue;
                }
                
                if (teamMeetingValue > 1 ) {
                    SS.Team_Meeting__c= teamMeetingValue;
                }
            } //end if to check whether there is anything in Key Events
            
        } // end if to check whether there were any customStates    
    } // end outer for loop
    System.debug('Checking variable memory ' + CUSTOM_STATE1TOTAL + ' ' + CUSTOM_STATE2TOTAL + ' ' +  CUSTOM_STATE3TOTAL + ' ' + CUSTOM_STATE4TOTAL + ' ' + CUSTOM_STATE5TOTAL + ' ' + CUSTOM_STATE6TOTAL);
    
    
} //end trigger

Comments