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
cactusdavecactusdave 

Parsing/Substring Value in Apex Trigger - Looking for Specific value "@"

I am looking to parse the email address from the user table to get the first part of the address. For example, if an email address is bgates@microsoft.com, I want to just get the bgates.  I can use substring to get the first part but don't know what value to put in where this would end. 

 

If this was in Excel, I would do the following =mid(email, 0, find(@)-1)

 

Is there a similar way in which I could utilize the find() functionality in excel and apply this in the trigger? Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Use the indexOf function to find the @ symbol, and then use that as the second integer.

 

 

All Answers

BritishBoyinDCBritishBoyinDC

Use the indexOf function to find the @ symbol, and then use that as the second integer.

 

 

This was selected as the best answer
cactusdavecactusdave

This worked. I added the following and was able to get the value I wanted from the user object:


// Parse Email to get all of the values prior to the "@" sign

User caseowneruserinfor = [select  email from User where id = :caseowner];


string CreatorName;
integer Whereitsat;

CreatorName = caseowneruserinfor.email;
Whereitsat = CreatorName.indexOf(

a.CaseCreatorName__c = CreatorName.substring(0,Whereitsat) ;

Reppin__505Reppin__505

I wrote a piece of apex code to parse through the body of an email and look for an email address. Here's how i pulled the email address from the body of the message.

 

        	//Pull the substring starting at the @ and then end at the end of the string.
        	String email1 = email.plainTextBody.substring(email.plainTextBody.indexOf('@'));
        	//Now from that substring, parse through it and keep everything before the first space.
       	String email2 = email1.substring(0, email1.indexOf(' '));
        	//Now take the entire message and remove the first substring.
        	String subtract = email.plainTextBody.replaceAll(email.plainTextBody.substring(email.plainTextBody.indexOf('@')), '');
        	//From that reduced string, pull from that string. Start at the last occurance of the space and run until the
        	//first occurance of the ampersand.
        	String email4 = email.plainTextBody.substring(subtract.LastIndexOf(' '), email.plainTextBody.indexOf('@'));
        	//Email 2 is the second part of the email and email 4 is the first part of the email address.