0

I have an email to case where a person sends a template containing specific values to a particular address that creates a case in salesforce. I want to pick up the values from the case description field and insert it into different custom fields.

Mapping:
Media Request Type: Choose an item.
MCM Account #: ##########
Original Account #: ##########
Consumer Name: First Name Last Name
Company: Company Name
Document Type: Choose an item.
Document Date: Click or tap to enter a date.

Apex Trigger:

trigger FieldUpdate on Case (before insert){

    Map<String, String> fieldListMap = new Map<String, String>{
'Media Request Type: '          => 'Media_Request_Type__c',
'MCM Account #: '                       => 'MCM_Account_Number__c',
'Original Account #: '          => 'Original_Account_Number_c',
'Consumer Name: '           => 'Customer_Name__c',
'Company: '                 => 'Company_c',
'Document Type: '           =>'Media_Document_Type__c'



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

This trigger isn't running when I send the email to case,can anyone provide knowledge on how to achieve this or how this trigger can be fixed?

1 Answer 1

3

You're splitting on ':', so your key won't match. Change your values to exclude the ':':

'Media Request Type'          => 'Media_Request_Type__c',

Alternatively, you might consider using a Pattern:

for(Case cas : trigger.new){
    if(cas.Description == null) {
        continue;
    }
    Pattern p = Pattern.compile('^(.+?: )(.+)$');
    Matcher m = p.matcher(cas.Description);
    while(m.find()) {
        String key = m.group(1), value = m.group(2);
        if(fieldListMap.get(key) != null) {
            cas.put(fieldListMap.get(key), value);
        }
    }
}

Finally, I suggest you use a Map<String, SObjectField> to avoid typos:

Map<String, SObjectField> fieldListMap = new Map<String, SObjectField> {
    'Media Request Type: ' => Case.Media_Request_Type__c,

This forces the compiler to verify your fields, which is more reliable than using string values.

1
  • I am trying it the way you told me with sObjectField but it is still not working! I tried to send it with Media Request Type: Media but it did not map the value! Jul 31, 2017 at 14:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .