• Iain Clements
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Professional Services Consultant
  • NewVoiceMedia

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi everyone

Hope someone can help with this odd Trigger behaviour I'm experiencing. The Trigger takes a block of text that comes in on a new record, splits it into a list and then if it finds certain values in the list populates a few fields on a custom object with totals of the values found.

Everything's working great if the Trigger finds some values, but if I insert a record straight after the Trigger is populating the custom fields with the same totals as the previous record - as if the values of each variable are being stored in the trigger's memory.

Can anyone see why this is happening please? I'll post a sample of the trigger below but can share the whole thing if it would be useful.

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
    
    Integer breakValue = 0; 
    Integer comfortBreakValue = 0;
    Integer lunchValue = 0;
    Integer trainingValue = 0;
    Integer teamMeetingValue = 0;
    
    List<String> customStates = new List<String>();
    //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);
            }
           
            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 : ' + breakValue);
                    }
                    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;
                }
                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

#Apex #Trigger

So I'm trying to have the CallDisposition on the CTI Softphone to update the Lead status.

 

I think I'm pretty close, but I'm getting an 'Exec Anon Error' when Executing the Trigger is the Console: line 1, column 0: required (...)+ loop did not match anything at input 'trigger'

 

I'm very new to Apex - or any code for that matter, although I have tweaked different kinds of code, which is essentially what I'm trying to do here. I copied the code from here: http://boards.developerforce.com/t5/Apex-Code-Development/Task-Trigger-to-Update-Date-Time-field-on-Lead/td-p/279615

 

trigger ActivityFirstcontact on Task (after insert) {

List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q') && newTask.Subject != 'Lead Source Details')
            {
            Lead changedLead = [select id, First_Contact_Date_Time__c from lead where id=:NewTask.WhoId];
               if(changedLead.First_Contact_Date_Time__c == NULL){
                   changedLead.First_Contact_Date_Time__c=newTask.CreatedDate;
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;
              
   }
}

 

And came up with this:

 

trigger ActivityCall on Task (after insert) {

    List<Lead> updatedLeads= new List<Lead>();
 
    for(Task newTask:Trigger.new){
            if(string.valueOf(NewTask.WhoId).startsWith('00Q') && newTask.CallType != 'Incoming')
            {
            Lead changedLead = [select id, Status from lead where id=:NewTask.WhoId];
                   changedLead.Status=newTask.CallDisposition;
                   updatedLeads.add(changedLead);
           }}
   
              update updatedLeads;
}

 

It's definitely missing a few other things, because I want it to trigger from all incoming and outgoing calls. I may also just do one trigger for each disposition, as the dispositions don't always match up, but certain CallDispositions will move the lead to a different status/stage. - As long as I can get it to work for one scenario, I can build from there.

 

Thank you in advance for your help and potentially making me look like a hero!!!

 

 

 

  • September 30, 2012
  • Like
  • 0

I'm trying to use a field update to bring out a phone number from a "Fax" field, and convert it to just a numerical sequence.  Could find a formula that would do this in the formula editor.   For example I want to convert fax # (818) 555-1212 into 8185551212, and add it onto an email.  Is there a formula that will take this string and remove the () and spaces?  So far I've done this:

 

"1"&FAX&"@concordsend.com"

 

Which gives me 1(818) 555-1212@concordsend.com.

I want: 18185551212@concordsend.com

 

Thanks,

 

Sean

  • August 17, 2010
  • Like
  • 0