function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Wendy Tanner 11Wendy Tanner 11 

I have Email to Case set up, Detail coming in to body of email. Need to Parse that Info in to fields in Case. Apex Trigger? Any Examples?

Email to case is set up. Detail comes in to the description: 

First Name: xxxxx
Last Name: Test
Email: test@gmail.com
Phone: xxxxxxxx
Country: US
Comment: Last Test!
TextAlertsOptIn: Yes

I ahve Fields for each option and I need to pass the information in to those fields. the format is the same every time. 

Any suggestions or examples of how this can be done, Trigger? New to apex and any detail is appreciated. 

 
Best Answer chosen by Wendy Tanner 11
SarvaniSarvani
Hi Wendy,

As you already have trigger Populate_Case on Case(before insert) {    in line 1 its showing the error. Please clear all the code and paste the below code in your trigger it will work.
trigger Populate_Case on Case (before insert) {
 for(Case C:trigger.new){
        if(C.Subject!=Null && C.Subject.Contains('A New Contact Us Form Submitted')==True){
        if(C.Description!='' && C.Description.length()>0){
            String data=C.Description;
            C. First_Name__c=data.substringBetween('First Name:', 'Last Name:'); // value between the two substrings specified is considered as first name.
            C.Last_Name__c=data.substringBetween('Last Name:', 'Email:');
            C.Email__c=data.substringBetween('Email:', 'Phone:');             
            // Add your other fields
            }
        }
     }
 }

Thanks,
Sarvani

All Answers

SarvaniSarvani
Hi Wendy,

You can refer to the sample code below. The trigger executes in before insert scenario. It actually reads the the value of case description into a variable called 'data' of type string. As your variable now has the decsription of case it searches for substrings bteween First Name: , Last Name:, Email: and Phone: and assigns them into the fields ( First_Name__c, Last_Name__c,Email__c) on the case record. Implement the same for other fields using substringBetween method.
 
trigger WorkOnStrings on Case (before insert) {
    for(Case C:trigger.new){
        if(C.Description!='' && C.Description.length()>0){
            String data=C.Description;
            C. First_Name__c=data.substringBetween('First Name:', 'Last Name:'); // value between the two substrings specified is considered as first name.
            C.Last_Name__c=data.substringBetween('Last Name:', 'Email:');
            C.Email__c=data.substringBetween('Email:', 'Phone:'); 
            // Add your other fields
            }
    }
 }

Your description field on case record should always have the strings as you specified or it will fail.

Hope it helps! Please mark as best if it does.

Thanks
Wendy Tanner 11Wendy Tanner 11
@sarvani This i fantastic, and how would i add an element that it only runs if it is certain case record type of subject, that way i know that the fields will be consistent and it won't fail? 

Thank You. 
SarvaniSarvani
Hi Wendy,

Try the below code (If loop added in line 3 for subject check). It runs only if the incoming emails subject is not null and if the subject has string "Biodata Email:" Likewise you can replace the string with your subject string.
trigger WorkOnStrings on Case (before insert) {
    for(Case C:trigger.new){
        if(C.Subject!=Null && C.Subject.Contains('Biodata Email:')==True){
        if(C.Description!='' && C.Description.length()>0){
            String data=C.Description;
            C. First_Name__c=data.substringBetween('First Name:', 'Last Name:'); // value between the two substrings specified is considered as first name.
            C.Last_Name__c=data.substringBetween('Last Name:', 'Email:');
            C.Email__c=data.substringBetween('Email:', 'Phone:');             
            // Add your other fields
            }
        }
     }
 }

Hope this helps! Please mark as best answer if it does

Thanks
Wendy Tanner 11Wendy Tanner 11
Thank You and One last question as I put this in and I am getting a Missing <EOF> at trigger at lign 3 column 2. 

trigger Populate_Case on Case (before insert) {

}trigger WorkOnStrings on Case (before insert) {
    for(Case C:trigger.new){
        if(C.Subject!=Null && C.Subject.Contains('A New Contact Us Form Submitted')==True){
        if(C.Description!='' && C.Description.length()>0){
            String data=C.Description;
            C. First_Name__c=data.substringBetween('First Name:', 'Last Name:'); // value between the two substrings specified is considered as first name.
            C.Last_Name__c=data.substringBetween('Last Name:', 'Email:');
            C.Email__c=data.substringBetween('Email:', 'Phone:');             
            // Add your other fields
            }
        }
     }
 }
SarvaniSarvani
Hi Wendy,

As you already have trigger Populate_Case on Case(before insert) {    in line 1 its showing the error. Please clear all the code and paste the below code in your trigger it will work.
trigger Populate_Case on Case (before insert) {
 for(Case C:trigger.new){
        if(C.Subject!=Null && C.Subject.Contains('A New Contact Us Form Submitted')==True){
        if(C.Description!='' && C.Description.length()>0){
            String data=C.Description;
            C. First_Name__c=data.substringBetween('First Name:', 'Last Name:'); // value between the two substrings specified is considered as first name.
            C.Last_Name__c=data.substringBetween('Last Name:', 'Email:');
            C.Email__c=data.substringBetween('Email:', 'Phone:');             
            // Add your other fields
            }
        }
     }
 }

Thanks,
Sarvani
This was selected as the best answer