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
Gabe Sanchez 7Gabe Sanchez 7 

Parse email and capture text from body

I am trying to capture a portion of the body of an email from a certain client in salesforce. Basically, the body will also have "ref:20392039". I want to capture that and drop it into our field in SF called Service Now Ref Code. Any ideas on how to do this?
ayu sharma devayu sharma dev
Hello Gabe

Please try the below code
 
Matcher m = Pattern.compile('ref:+.......[0-9]').matcher('This is a test string which contains ref and here is ref number ref:20392039 insert this in salesforce.');
m.find();
System.assert(false, m.group());

Hope it helps. If it solves your problem please mark this as the best solution.

Thanks and Regards
Ayush Sharma
Gabe Sanchez 7Gabe Sanchez 7
Thank you. Is this a formula that can be put into  process builder or do I have to create with Apex email service?
ayu sharma devayu sharma dev
Hello Gabe,
Use this Logic in you Apex code when you are updating the field in Apex Trigger/Batch. 
 
Matcher m = Pattern.compile('ref:+.......[0-9]').matcher('This is a test string which contains ref and here is ref number ref:20392039 insert this in salesforce.');

m.find();
Account.RefCode = m.group();

RefCode is the field where you put value from email Body.

Thanks
Ayush
Gabe Sanchez 7Gabe Sanchez 7
Thank you. I am super brand new to this. So then I create an APEX class or APEX trigger.  
User-added image

I assume I have to create that and then insert it here, correct?

User-added image

Thanks again for the help!!
ayu sharma devayu sharma dev
Hello Gabe,

Please provide me the following:

1. Is the Body field on the Object you are creating Process builder on. If yes then Provide me the API name of Object.
2. Provide me the API name of fields => 1. Which contains the ref: Id   2. Where you want to insert RFID.
3. If case 1 is false then tell me the relation between objects. 

Please let me know above, so I can create a sample code.

Regards
Ayush
Gabe Sanchez 7Gabe Sanchez 7

1. I am using 'Email Message in process builder, then matching criteria since it is only 1 client that we need this for. 

[EmailMessage].fromAddress
[EmailMessage].subject

User-added image


2. API for the field I need is : 

ServiceNow_Ref_Code__c

This is on the object of the case.

Please let me know if I've answered your questions. 

ayu sharma devayu sharma dev
Hello Gabe

Please try below solution

Create a class with this code:
 
public class InvocableMethods {
	
    @InvocableMethod( label='Ger Ref Id' description='' category='Case' )
    public static void getRefId( List<EmailMessage> emailMsgList ){
        List<Case> casesToUpdate = new List<Case>();
        for( EmailMessage em : emailMsgList ){
            if( em.TextBody != null ){
                Matcher m = Pattern.compile('ref:+.......[0-9]').matcher(em.TextBody);
                if( m.find() ){
                    casesToUpdate.add( new Case( Id = em.RelatedToId, ServiceNow_Ref_Code__c = m.group() ) );
                }
            }
        }
        update casesToUpdate;
    }
    
}


​​​​And your Process builder Action should look like https://ibb.co/sCmXR60.

Let me know if any error arises. 

Regards
Ayush
Gabe Sanchez 7Gabe Sanchez 7
Hello Ayush - 

I finally did get this into prod, but it did not update the ref code. I also realized that sample Ref codes were like these vs 1234567 : 

Ref:MSG38602367
Ref:MSG38689603

Would the process still work? Also in the process builder, I should be using EmailMessage as an object correct? I did get an error when I put the ref in SF in the description. 


Ref:38810719 <javascript:void(0);>

That is the sample that I used. 

Thanks again for the help!


Gabe



 
Gabe Sanchez 7Gabe Sanchez 7
Hello Ayush - 

I finally did get this into prod, but it did not update the ref code. I also realized that sample Ref codes were like these vs 1234567 : 

Ref:MSG38602367
Ref:MSG38689603

Would the process still work? Also in the process builder, I should be using EmailMessage as an object correct? I did get an error when I put the ref in SF in the description. 


Ref:38810719 <javascript:void(0);>

That is the sample that I used. 

Thanks again for the help!


Gabe