+ Start a Discussion
ThomasmThomasm 

Code to edit a string

I have a customer field called Subject that i would like to have the informtion change from  TT-ABS EE38877 Jessica Martin is Absent. 0 Present. JB1356 Evonik Labs PO146 T&m

to only show the name Jessica Martin

the name changes but the format is always the same.  Any idea's where to begin
Best Answer chosen by Thomasm
pconpcon
The code below will work, but it's not the best way.  Also, this will only work if the format of the string does not change
 
global class Paging_Test implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();     
        Pages__C sPages = new Pages__C();        

        sPages.Job__C = 'a013000000UHggN';
        List<String> subjectBits = email.plainTextBody.split(' ');
        sPages.Employee_Name__C = subjectBits.get(2) + ' ' + subjectBits.get(3);

        insert sPages;                                         

        return Result;         
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.

I'd also recommend that you do more control testing to make sure you do not get an array out of bounds exception or an NPE.

All Answers

Balaji BondarBalaji Bondar
Hi Thomasm,

Query all the records with Subject__c=' TT-ABS EE38877 Jessica Martin is Absent. 0 Present. JB1356 Evonik Labs PO146 T&m' .Update this fields with 'Jessica Martin' in the retrieved CSV and update the  records.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
ThomasmThomasm
i am looking to make the changes when the record is created
pconpcon
Will this string always be this way, or will it be in the format of:

<Identifier> <Identifier> <Name> is Absent. <Number> Present. ...

If it will always be the third and fourth value in the string, you could do something like:
 
List<String> subjectBits = myObject.Subject.split(' ');
String name = '';

if (subjectBits.length() > 4) {
    name = subjectBits.get(2) + ' ' + subjectBits.get(3);
}

//Do something with name

 
ThomasmThomasm
here is my current code

global class Paging_Test implements Messaging.InboundEmailHandler
 {         
 global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) 
 {                     
 Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();            
 Pages__C sPages = new Pages__C();               
         
                   
 sPages.Job__C = 'a013000000UHggN';             
 sPages.Employee_Name__C = email.plainTextBody;     
               
 insert sPages;                                                
 
 
 Return Result;                
     
 }   
 }

An email is sent with the body of the email that looks like 
"TT-ABS EE38877 Jessica Martin is Absent. 0 Present. JB1356 Evonik Labs PO146 T&m"

this create the Page record but the field employee name (formaly subject) is 
TT-ABS EE38877 Jessica Martin is Absent. 0 Present. JB1356 Evonik Labs PO146 T&m
and i would like to pull out all of the date except the name which for this one is Jessica Martin
 
pconpcon
The code below will work, but it's not the best way.  Also, this will only work if the format of the string does not change
 
global class Paging_Test implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();     
        Pages__C sPages = new Pages__C();        

        sPages.Job__C = 'a013000000UHggN';
        List<String> subjectBits = email.plainTextBody.split(' ');
        sPages.Employee_Name__C = subjectBits.get(2) + ' ' + subjectBits.get(3);

        insert sPages;                                         

        return Result;         
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.

I'd also recommend that you do more control testing to make sure you do not get an array out of bounds exception or an NPE.
This was selected as the best answer