Trigger with Custom States
I've written about this text manipulation requirement before in previous blogs, but most recently I've had to replicate the Trigger for multiple clients.
To help with this, I've revisited the code and have started using Custom Settings to store some of the detail and make the Trigger more easily customisable.
Utilising Custom Settings had great promise, it was a good learning curve for me as well to work out how to create the Custom Settings within the Test Class to ensure coverage.
The Custom Settings
Here is the custom setting file I created for this project.
Custom settings definition |
The Trigger
trigger Tagged_Custom_State on NVMStatsSF__NVM_Agent_Summary__c (before update, before insert) { /* 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 304:04| 53344Custom State 305:05| 53344Custom State 320:20| 53344Custom State 321:21| 53344Custom State 322:22| 53344Custom State 323:23| 53344Custom State 324:24| */ //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 //Custom State 7 String CUSTOM_STATE7 = 'None'; String CUSTOM_STATE7Value = 'None'; Integer CUSTOM_STATE7TOTAL = 0; NVMCustomStates__c CUSTOM_STATE_SETTINGS7 = NVMCustomStates__c.getInstance('7'); if (CUSTOM_STATE_SETTINGS7 !=Null ) { if (CUSTOM_STATE_SETTINGS7.Label__c != Null) { CUSTOM_STATE7 = CUSTOM_STATE_SETTINGS7.Label__c; } if (CUSTOM_STATE_SETTINGS7.Value__c != Null) { CUSTOM_STATE7Value = CUSTOM_STATE_SETTINGS7.Value__c; } } //end of overall if - did we find a Custom Setting ListcustomStates = 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 (SS.Copy_Key_Event_String__c == Null) { SS.Copy_Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c ; //copy Key Event string in case Trigger eats it } //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); } if (CUSTOM_STATE7 != Null && CUSTOM_STATE7Value != Null) { SS.NVMStatsSF__Key_Event_String__c = SS.NVMStatsSF__Key_Event_String__c.replaceAll(CUSTOM_STATE7, CUSTOM_STATE7Value); } 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(CUSTOM_STATE7Value); if (!csCheck7) { CUSTOM_STATE7Value = '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); } 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); } Boolean i7; System.debug('Current variable being evaluated ' + customStates[i]); i7 = customStates[i].contains(CUSTOM_STATE7Value); if (i7) { value = customStates[i].right(5); value = value.replaceAll('[^0-9.]', ''); System.debug(customStates[i] + ' does contain ' + CUSTOM_STATE7Value); CUSTOM_STATE7TOTAL = CUSTOM_STATE7TOTAL + integer.valueof(value); System.debug('Total seconds : ' + CUSTOM_STATE7TOTAL); } if (!i7) { System.debug(customStates[i] + ' does NOT contain ' + CUSTOM_STATE7Value); } } //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 (CUSTOM_STATE7TOTAL !=Null) { SS.Custom_State_7__c = CUSTOM_STATE7TOTAL; } else if (CUSTOM_STATE7TOTAL ==Null) { SS.Custom_State_7__c = 0; } } //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 + ' ' + CUSTOM_STATE7TOTAL); } //end trigger
Test Class
@IsTest public class Test_Tagged_Custom_State { static testMethod void insertNewAgentSummary() { //Populate CustomSettings NVMCustomStates__c newSetting1 = new NVMCustomStates__c(); newSetting1.Name = '1'; newSetting1.Label__c = 'Custom State 320'; newSetting1.Value__c = 'Resting'; insert newSetting1; NVMCustomStates__c newSetting2 = new NVMCustomStates__c(); newSetting2.Name = '2'; newSetting2.Label__c = 'Custom State 320'; newSetting2.Value__c = 'Resting'; insert newSetting2; NVMCustomStates__c newSetting3 = new NVMCustomStates__c(); newSetting3.Name = '3'; newSetting3.Label__c = 'Custom State 320'; newSetting3.Value__c = 'Resting'; insert newSetting3; NVMCustomStates__c newSetting4 = new NVMCustomStates__c(); newSetting4.Name = '4'; newSetting4.Label__c = 'Custom State 320'; newSetting4.Value__c = 'Resting'; insert newSetting4; NVMCustomStates__c newSetting5 = new NVMCustomStates__c(); newSetting5.Name = '5'; newSetting5.Label__c = 'Custom State 320'; newSetting5.Value__c = 'Resting'; insert newSetting5; NVMCustomStates__c newSetting6 = new NVMCustomStates__c(); newSetting6.Name = '6'; newSetting6.Label__c = 'Custom State 320'; newSetting6.Value__c = 'Resting'; insert newSetting6; NVMCustomStates__c newSetting7 = new NVMCustomStates__c(); newSetting7.Name = '7'; newSetting7.Label__c = 'Custom State 320'; newSetting7.Value__c = 'Resting'; insert newSetting7; ListsummaryToCreate = new List (); //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 304:04|53344Custom State 305:05|53344Custom State 320:20|53344Custom State 321:21|53344Custom State 322:22|53344Custom State 323:23|53344Custom State 324:24|'; s.NVMStatsSF__Key_Event_String__c = TEST_STRING; s.NVMStatsSF__dateAgentId__c = 'a991823711' + i; s.NVMStatsSF__AgentID__c = '123a1' + i; summaryToCreate.add(s); } try { //Insert the record System.debug('summaryToCreate is ' + summaryToCreate.size()); insert summaryToCreate; } 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 k = 0; for (NVMStatsSF__NVM_Agent_Summary__c tmp : [SELECT 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]) { k++; System.debug(k); } System.debug('Records returned = ' + k); System.AssertEquals(5, k); List summaryToCreate2 = new List (); //Insert 5 without any Custom or Minor for (Integer l = 0; l < 5; l++) { 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 = 'a991823722' + l; m.NVMStatsSF__AgentID__c = '12322a' + l; summaryToCreate2.add(m); } //5 with Custom States for (Integer m = 0; m < 5; m++) { NVMStatsSF__NVM_Agent_Summary__c n = new NVMStatsSF__NVM_Agent_Summary__c(); String TEST_STRING = '123123123 | 2342 34234 234 | 329472347234 | 53344Custom State 320:20 | 53344Team Meeting 320:20'; n.NVMStatsSF__Key_Event_String__c = TEST_STRING; n.NVMStatsSF__dateAgentId__c = 'a9918237aa33' + m; n.NVMStatsSF__AgentID__c = '123cd' + m; summaryToCreate2.add(n); } 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)); } } List summaryToCreate3 = new List (); NVMStatsSF__NVM_Agent_Summary__c o = new NVMStatsSF__NVM_Agent_Summary__c(); String TEST_STRING = 'Emoty Key Event String'; o.NVMStatsSF__Key_Event_String__c = TEST_STRING; o.NVMStatsSF__dateAgentId__c = 'a991823722'; o.NVMStatsSF__AgentID__c = '12322a'; summaryToCreate3.add(o); try { //Insert the record System.debug('summaryToCreate3 is ' + summaryToCreate3.size()); insert summaryToCreate3; } catch (DmlException e) { // System.debug(e); } //Check all records were inserted Integer j = 0; for (NVMStatsSF__NVM_Agent_Summary__c tmp2 : [SELECT 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 NVMStatsSF__AgentID__c = '123cd' ORDER By CreatedDate DESC]) { j++; System.debug(j); } System.debug('Records returned = ' + j); System.AssertEquals(0, j); } //end of method } //end of test class
Comments
Post a Comment