+ Start a Discussion
KasriftKasrift 

Multiple Field updates in one Trigger

I was wondering how to properly consolidate multiple triggers (specifically for field updates) into one trigger.  I had separate triggers for multiple field updates, the first and most important one being multiple field updates from the Case Description field (the one that the default Email to Case On Demand writes to).  The Apex code to get the information from the email into fields looks like this:

 

trigger trgr_CaseDescriptionPreFormat on Case (before Insert, before Update) {

    Map<String, String> fieldListMap = new Map<String, String>{
        'State'         => 'State__c',
        'City'          => 'City__c',
        'Explanation 1' => 'Explanation_1__c',
        'Explanation 2' => 'Explanation_2__c',
        'Explanation 3' => 'Explanation_3__c',
        'Explanation 4' => 'Explanation_4__c',
        'Explanation 5' => 'Explanation_5__c',
        'Name'          => 'Name__c'
    };

    List<String> descpSplitList;
    List<String> labelValuePairSplitList;
    for(Case cas : trigger.new){
        if(!String.isBlank(cas.Description)){
            descpSplitList = cas.Description.split('\n');
       
            for(String labelValuePair : descpSplitList){
                labelValuePairSplitList = labelValuePair.split(':');
                if (
                    labelValuePairSplitList.size() > 1 &&
                    fieldListMap.containsKey(labelValuePairSplitList[0])
                ) {
                    cas.put(fieldListMap.get(labelValuePairSplitList[0]), labelValuePairSplitList[1]);
                }
            }
        }
    }
}

 

I had other Apex triggers to update fields based on the fields that were written to by the Description field Apex.  They are basic field updates that update another field Tier 1-5 based on the values of the Explaination 1-5 that look like this:

 

trigger FieldUpdateTier1 on Case (before insert, before update){
Map<String, String> myPickListMap = new Map<String, String> {'Explaination 1 value1'=>'Tier 1',
'Explaination 1 Value 2'=>'Tier 2',
'Explaination 1 Value 3'=>'Tier 3',
'Explaination 1 Value 4'=>'Tier 4',
'Explaination 1 Value 5'=>'Tier 5'};
for (Case c : Trigger.new){
if(c.AEM__c != null) {
c.Explaination_1__c = myPickListMap.get(c.Tier1__c);
}
}

 

 

I originally had 5 separate triggers for each filed update, but then I realized they use the same map, so I updated the Apex  code to look like this :

 

trigger FieldUpdateTier1 on Case (before insert, before update){
Map<String, String> myPickListMap = new Map<String, String> {'Explaination 1 value1'=>'Tier 1',
'Explaination 1 Value 2'=>'Tier 2',
'Explaination 1 Value 3'=>'Tier 3',
'Explaination 1 Value 4'=>'Tier 4',
'Explaination 1 Value 5'=>'Tier 5'};
for (Case c : Trigger.new){
c.Explaination_1__c = myPickListMap.get(c.Tier1__c);

c.Explaination_2__c = myPickListMap.get(c.Tier2__c);

c.Explaination_3__c = myPickListMap.get(c.Tier3__c);

c.Explaination_4__c = myPickListMap.get(c.Tier4__c);

c.Explaination_5__c = myPickListMap.get(c.Tier5__c);
}
}

 

 

The issue that I have now is the order of execution for the triggers.  An email creates a case, which writes the email body into the Case field Description.  I want the Apex Trigger to string the values from the Description field into specific fields on the Case, which it does.  But I can't get the field updates to fire properly to update the other fields after the Description updates the Explaination fields.  I tried consolidating the Apex trigger even further from two Apex triggers into one, which looks like this:

 

trigger trgr_CaseDescriptionPreFormat on Case (before Insert, before Update) {

Map<String, String> fieldListMap = new Map<String, String>{
'State' => 'State__c',
'City' => 'City__c',
'Explanation 1' => 'Explanation_1__c',
'Explanation 2' => 'Explanation_2__c',
'Explanation 3' => 'Explanation_3__c',
'Explanation 4' => 'Explanation_4__c',
'Explanation 5' => 'Explanation_5__c',
'Name' => 'Name__c'
};

List<String> descpSplitList;
List<String> labelValuePairSplitList;


Map<String, String> myPickListMap = new Map<String, String> {'Explaination 1 value1'=>'Tier 1',
'Explaination 1 Value 2'=>'Tier 2',
'Explaination 1 Value 3'=>'Tier 3',
'Explaination 1 Value 4'=>'Tier 4',
'Explaination 1 Value 5'=>'Tier 5'};


for(Case c : trigger.new){
if(!String.isBlank(c.Description)){
descpSplitList = c.Description.split('\n');

for(String labelValuePair : descpSplitList){
labelValuePairSplitList = labelValuePair.split(':');
if (
labelValuePairSplitList.size() > 1 &&
fieldListMap.containsKey(labelValuePairSplitList[0])
) {
cas.put(fieldListMap.get(labelValuePairSplitList[0]), labelValuePairSplitList[1]);
}
}
}
c.Explaination_1__c = myPickListMap.get(c.Tier1__c);
c.Explaination_2__c = myPickListMap.get(c.Tier2__c);
c.Explaination_3__c = myPickListMap.get(c.Tier3__c);
c.Explaination_4__c = myPickListMap.get(c.Tier4__c);
c.Explaination_5__c = myPickListMap.get(c.Tier5__c);
}

}

}

 

 

However it isn't updating the fields after the first part of the trigger.  Can anyone assist with this (I'm really new to coding and still trying to figure this out).   Hopefully I explained the situation well enough, long story short, I'm trying to multiple field updates on the same object the Case in a specific order which requires fields to be updated first in order for other fields to be updated via the inlcuded apex trigger.

 

-Stephen