Extracting Values from a Delimited File Part Deux

Extracting Values from a Delimited File Part Deux

Since working on this Trigger last I've been reading a lot about Trigger best practice.

I particularly like the requirement of having a single Trigger per Object in Production environments and will move to this model over time.

With that set, here is v2 of this trigger.

trigger TaggedState on NVMStatsSF__NVM_Agent_Summary__c (before insert, before update) {
    NVMStatsSF__NVM_Agent_Summary__c[] agentSummaries = Trigger.new;
    TaggedState.applyCustomStates(agentSummaries);
}

and the class that now contains all of the logic:



 
public class TaggedState {
    
    public static void applyCustomStates(NVMStatsSF__NVM_Agent_Summary__c[] agentSummaries) {
        
        final String CHKSTR_END_CHAR_REGEX = '\\|';
        List<String> customStates = new List<String>();
        
        //Set up the CustomState variables
        String customState1 = 'Administration';
        String customState2 = 'Desk Work';
        String customState3 = 'Assigned Task';
        String customState4 = 'Feedback';
        String customState5 = 'Working Email';
        String customState6 = 'Escalation';
        //Set up MinorState variables
        String minorState1 = 'Break';
        String minorState2 = 'Comfort Break';
        String minorState3 = 'Lunch';       
        String minorState4 = 'Team Meeting';
        String minorState5 = 'Training';
        
        for (NVMStatsSF__NVM_Agent_Summary__c b :agentSummaries){
            
            String keyString = b.NVMStatsSF__Key_Event_String__c;
            //No processing if the Key Events String is empty
            if (keyString != null ) {
            //We need to replace Custom State 320 etc with acual values
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 320', customState1);
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 321', customState2);
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 322', customState3);
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 324', customState4);
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 325', customState5);
            b.NVMStatsSF__Key_Event_String__c = b.NVMStatsSF__Key_Event_String__c.replaceAll('Custom State 326', customState6);
 
                
                //First lets check for each of the Custom States in the Key Event String:
                Boolean cState1 = keyString.contains(customState1);
                Boolean cState2 = keyString.contains(customState2);
                Boolean cState3 = keyString.contains(customState3);
                Boolean cState4 = keyString.contains(customState4);
                Boolean cState5 = keyString.contains(customState5);
                Boolean cState6 = keyString.contains(customState6);            
                //Then search for the standard values:
                Boolean mState1 = keyString.contains(minorState1); 
                Boolean mState2 = keyString.contains(minorState2);
                Boolean mState3 = keyString.contains(minorState3);
                Boolean mState4 = keyString.contains(minorState4);
                Boolean mState5 = keyString.contains(minorState5);
                
                //Blank the values on the Custom States if no matches
                if (!cState1) { b.Custom_State_1__c = 0; }
                if (!cState2) { b.Custom_State_2__c = 0; }
                if (!cState3) { b.Custom_State_3__c = 0; }
                if (!cState4) { b.Custom_State_4__c = 0; }
                if (!cState5) { b.Custom_State_5__c = 0; }
                if (!cState6) { b.Custom_State_6__c = 0; }
                //Blank values on Minor State fields    
                if (!mState1) { b.Break__c = 0; }
                if (!mState2) { b.Comfort_Break__c = 0; }
                if (!mState3) { b.Lunch__c = 0; }
                if (!mState4) { b.Training__c = 0; }
                if (!mState5) { b.Team_Meeting__c = 0; }
                
                //After initial checks split the string by the pipe character          
                customStates = (''+b.NVMStatsSF__Key_Event_String__c).split(CHKSTR_END_CHAR_REGEX);
                String cs1tempValue;
                Integer cs1value;
                Integer cs1FinalValue;
                Integer cState1Value;
                
                String cs2tempValue;
                Integer cs2value;
                Integer cs2FinalValue;
                Integer cState2Value;
                
                String cs3tempValue;
                Integer cs3value;
                Integer cs3FinalValue;
                Integer cState3Value;
                
                String cs4tempValue;
                Integer cs4value;
                Integer cs4FinalValue;
                Integer cState4Value;
                
                String cs5tempValue;
                Integer cs5value;
                Integer cs5FinalValue;
                Integer cState5Value;
                
                String cs6tempValue;
                Integer cs6value;
                Integer cs6FinalValue;
                Integer cState6Value;
                
                String ms1tempValue;
                Integer ms1value;
                Integer ms1FinalValue;
                Integer mState1Value;
                
                String ms2tempValue;
                Integer ms2value;
                Integer ms2FinalValue;
                Integer mState2Value;
                
                String ms3tempValue;
                Integer ms3value;
                Integer ms3FinalValue;
                Integer mState3Value;
                
                String ms4tempValue;
                Integer ms4value;
                Integer ms4FinalValue;
                Integer mState4Value;
                
                String ms5tempValue;
                Integer ms5value;
                Integer ms5FinalValue;
                Integer mState5Value;
                
                for (Integer i = 0; i < customStates.size(); i++) {
                    //Inner loop addding up values
                    /* Start of each CustomState */ 
                    Boolean i1 = customStates[i].contains(customState1);
                    if (i1) {
                        cs1tempValue = customStates[i].right(5);
                        cs1tempValue = cs1tempValue.replaceAll('[^0-9.]', '');
                        cs1value = integer.valueOf(cs1tempValue);
                        system.debug('value is ' + cs1value + ' second value is ' + cs1FinalValue);
                        if (cs1FinalValue == null){
                            cs1FinalValue = cs1value;
                        } 
                        else if (cs1FinalValue != null) { 
                            cs1FinalValue = cs1FinalValue + cs1value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    /* Start of each CustomState */ 
                    Boolean i2 = customStates[i].contains(customState2);
                    if (i2) {
                        cs2tempValue = customStates[i].right(5);
                        cs2tempValue = cs2tempValue.replaceAll('[^0-9.]', '');
                        cs2value = integer.valueOf(cs2tempValue);
                        system.debug('value is ' + cs2value + ' second value is ' + cs2FinalValue);
                        if (cs2FinalValue == null){
                            cs2FinalValue = cs2value;
                        } 
                        else if (cs2FinalValue != null) { 
                            cs2FinalValue = cs2FinalValue + cs2value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    /* Start of each CustomState */ 
                    Boolean i3 = customStates[i].contains(customState3);
                    if (i3) {
                        cs3tempValue = customStates[i].right(5);
                        cs3tempValue = cs3tempValue.replaceAll('[^0-9.]', '');
                        cs3value = integer.valueOf(cs3tempValue);
                        system.debug('value is ' + cs3value + ' second value is ' + cs3FinalValue);
                        if (cs3FinalValue == null){
                            cs3FinalValue = cs3value;
                        } 
                        else if (cs3FinalValue != null) { 
                            cs3FinalValue = cs3FinalValue + cs3value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    /* Start of each CustomState */ 
                    Boolean i4 = customStates[i].contains(customState4);
                    if (i4) {
                        cs4tempValue = customStates[i].right(5);
                        cs4tempValue = cs4tempValue.replaceAll('[^0-9.]', '');
                        cs4value = integer.valueOf(cs4tempValue);
                        system.debug('value is ' + cs4value + ' second value is ' + cs4FinalValue);
                        if (cs4FinalValue == null){
                            cs4FinalValue = cs4value;
                        } 
                        else if (cs4FinalValue != null) { 
                            cs4FinalValue = cs4FinalValue + cs4value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    
                    /* Start of each CustomState */ 
                    Boolean i5 = customStates[i].contains(customState5);
                    if (i5) {
                        cs5tempValue = customStates[i].right(5);
                        cs5tempValue = cs5tempValue.replaceAll('[^0-9.]', '');
                        cs5value = integer.valueOf(cs5tempValue);
                        system.debug('value is ' + cs5value + ' second value is ' + cs5FinalValue);
                        if (cs5FinalValue == null){
                            cs5FinalValue = cs5value;
                        } 
                        else if (cs5FinalValue != null) { 
                            cs5FinalValue = cs5FinalValue + cs5value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    
                    /* Start of each CustomState */ 
                    Boolean i6 = customStates[i].contains(customState6);
                    if (i6) {
                        cs6tempValue = customStates[i].right(5);
                        cs6tempValue = cs6tempValue.replaceAll('[^0-9.]', '');
                        cs6value = integer.valueOf(cs6tempValue);
                        system.debug('value is ' + cs6value + ' second value is ' + cs6FinalValue);
                        if (cs6FinalValue == null){
                            cs6FinalValue = cs6value;
                        } 
                        else if (cs6FinalValue != null) { 
                            cs6FinalValue = cs6FinalValue + cs6value;
                        }  
                    }
                    /* End of each CustomState */
                    
                    /* Start of each MinorState */ 
                    Boolean m1 = customStates[i].contains(minorState1);
                    if (m1) {
                        ms1tempValue = customStates[i].right(5);
                        ms1tempValue = ms1tempValue.replaceAll('[^0-9.]', '');
                        ms1value = integer.valueOf(ms1tempValue);
                        system.debug('value is ' + ms1value + ' second value is ' + ms1FinalValue);
                        if (ms1FinalValue == null){
                            ms1FinalValue = ms1value;
                        } 
                        else if (ms1FinalValue != null) { 
                            ms1FinalValue = ms1FinalValue + ms1value;
                        }  
                    }
                    /* End of each MinorState */
                    
                                        /* Start of each MinorState */ 
                    Boolean m2 = customStates[i].contains(minorState2);
                    if (m2) {
                        ms2tempValue = customStates[i].right(5);
                        ms2tempValue = ms2tempValue.replaceAll('[^0-9.]', '');
                        ms2value = integer.valueOf(ms2tempValue);
                        system.debug('value is ' + ms2value + ' second value is ' + ms2FinalValue);
                        if (ms2FinalValue == null){
                            ms2FinalValue = ms2value;
                        } 
                        else if (ms2FinalValue != null) { 
                            ms2FinalValue = ms2FinalValue + ms2value;
                        }  
                    }
                    /* End of each MinorState */
                    
                                        /* Start of each MinorState */ 
                    Boolean m3 = customStates[i].contains(minorState3);
                    if (m3) {
                        ms3tempValue = customStates[i].right(5);
                        ms3tempValue = ms3tempValue.replaceAll('[^0-9.]', '');
                        ms3value = integer.valueOf(ms3tempValue);
                        system.debug('value is ' + ms3value + ' second value is ' + ms3FinalValue);
                        if (ms3FinalValue == null){
                            ms3FinalValue = ms3value;
                        } 
                        else if (ms3FinalValue != null) { 
                            ms3FinalValue = ms3FinalValue + ms3value;
                        }  
                    }
                    /* End of each MinorState */
                    
                                        /* Start of each MinorState */ 
                    Boolean m4 = customStates[i].contains(minorState4);
                    if (m4) {
                        ms4tempValue = customStates[i].right(5);
                        ms4tempValue = ms4tempValue.replaceAll('[^0-9.]', '');
                        ms4value = integer.valueOf(ms4tempValue);
                        system.debug('value is ' + ms4value + ' second value is ' + ms4FinalValue);
                        if (ms4FinalValue == null){
                            ms4FinalValue = ms4value;
                        } 
                        else if (ms4FinalValue != null) { 
                            ms4FinalValue = ms4FinalValue + ms4value;
                        }  
                    }
                    /* End of each MinorState */
                    
                                        /* Start of each MinorState */ 
                    Boolean m5 = customStates[i].contains(minorState5);
                    if (m5) {
                        ms5tempValue = customStates[i].right(5);
                        ms5tempValue = ms5tempValue.replaceAll('[^0-9.]', '');
                        ms5value = integer.valueOf(ms5tempValue);
                        system.debug('value is ' + ms5value + ' second value is ' + ms5FinalValue);
                        if (ms5FinalValue == null){
                            ms5FinalValue = ms5value;
                        } 
                        else if (ms5FinalValue != null) { 
                            ms5FinalValue = ms5FinalValue + ms5value;
                        }  
                    }
                    /* End of each MinorState */
                    
                    
                    
                } //end for loop
                
                //Time to set Custom State fields
                b.Custom_State_1__c = cs1FinalValue;
                b.Custom_State_2__c = cs2FinalValue;  
                b.Custom_State_3__c = cs3FinalValue;  
                b.Custom_State_4__c = cs4FinalValue;  
                b.Custom_State_5__c = cs5FinalValue;  
                b.Custom_State_6__c = cs6FinalValue;  
                //Minor State fields
                b.Break__c = ms1FinalValue;
                b.Comfort_Break__c = ms2FinalValue;
                b.Lunch__c = ms3FinalValue;
                b.Training__c = ms4FinalValue;
                b.Team_Meeting__c = ms5FinalValue;
            }
            
            
            
        } // end of loop
        
        
        
    }  
}
and finally the test class:
 

@IsTest
public class TestTaggedCustomState {
    static testMethod void insertNewAgentSummary() {
        
        List<NVMStatsSF__NVM_Agent_Summary__c> summaryToCreate = new List<NVMStatsSF__NVM_Agent_Summary__c>();   
        //Populate fields
        
        //Insert 5 with Custom States
        for (Integer i = 0; i < 5; i++) {
            NVMStatsSF__NVM_Agent_Summary__c s = new NVMStatsSF__NVM_Agent_Summary__c();
            String TEST_STRING = '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| 53344TComfort Break 320:20| 53344Lunch 320:20';
            s.NVMStatsSF__Key_Event_String__c = TEST_STRING;
            s.NVMStatsSF__dateAgentId__c = 'a9918237aaaa' + i;
            s.NVMStatsSF__AgentID__c = '123a' + i;
            summaryToCreate.add(s);
        }
        try {
            //Insert the record
            System.debug('summaryToCreate is ' + summaryToCreate.size());
            insert summaryToCreate;
            
        } catch (DmlException e) { 
            //    
            for (Integer i = 0; i < e.getNumDml(); i++) {
                // Process exception here 
                
                System.debug(e.getDmlMessage(i)); 
            }
        }
        
        //Check all records were inserted
        Integer i = 0;
        for (NVMStatsSF__NVM_Agent_Summary__c tmp : [SELECT Comfort_Break__c, Lunch__c, Training__c, Team_Meeting__c, Custom_State_1__c, Custom_State_2__c, Custom_State_3__c, Custom_State_4__c, Custom_State_5__c, Custom_State_6__c, CreatedDate FROM NVMStatsSF__NVM_Agent_Summary__c ORDER By CreatedDate DESC]) {
            i++;
            System.debug(i);
        }
        System.debug('Records returned = ' + i);
        System.AssertEquals(5, i);
        
        List<NVMStatsSF__NVM_Agent_Summary__c> summaryToCreate2 = new List<NVMStatsSF__NVM_Agent_Summary__c>();   
        
        //Insert 5 without any Custom or Minor
        for (Integer j = 0; j < 5; j++) {
            NVMStatsSF__NVM_Agent_Summary__c m = new NVMStatsSF__NVM_Agent_Summary__c();
            String TEST_STRING = '123123123 | 2342 34234 234 | 329472347234 | 53344Training 320:20| 53344Break 320:20| 53344Escalation 320:20';
            m.NVMStatsSF__Key_Event_String__c = TEST_STRING;
            m.NVMStatsSF__dateAgentId__c = 'a9918237aaaa' + j;
            m.NVMStatsSF__AgentID__c = '123b' + j;
            summaryToCreate2.add(m);
        }
        //5 with Custom States
        for (Integer h = 0; h < 5; h++) {
            NVMStatsSF__NVM_Agent_Summary__c m = new NVMStatsSF__NVM_Agent_Summary__c();
            String TEST_STRING = '123123123 | 2342 34234 234 | 329472347234 | 53344Custom State 320:20 | 53344Team Meeting 320:20';
            m.NVMStatsSF__Key_Event_String__c = TEST_STRING;
            m.NVMStatsSF__dateAgentId__c = 'a9918237aaaa' + h;
            m.NVMStatsSF__AgentID__c = '123c' + h;
            summaryToCreate2.add(m);
        }  
        
        try {
            //Insert the record
            System.debug('summaryToCreate2 is ' + summaryToCreate2.size());
            insert summaryToCreate2;
            
        } catch (DmlException e) { 
            //    
            for (Integer j = 0; j < e.getNumDml(); j++) {
                // Process exception here 
                
                System.debug(e.getDmlMessage(j)); 
            }
        }
        
        //Check all records were inserted
        Integer j = 0;
        for (NVMStatsSF__NVM_Agent_Summary__c tmp2 : [SELECT Comfort_Break__c, Lunch__c, Training__c, Team_Meeting__c, Custom_State_1__c, Custom_State_2__c, Custom_State_3__c, Custom_State_4__c, Custom_State_5__c, Custom_State_6__c, CreatedDate FROM NVMStatsSF__NVM_Agent_Summary__c WHERE Custom_State_1__c = 20 ORDER By CreatedDate DESC]) {
            j++;
            System.debug(i);
        }
        System.debug('Records returned = ' + j);
        System.AssertEquals(5, j);       
        
        
    } //end of method
    
    
} //end of test class

Comments