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
AkiraDioAkiraDio 

Convert text field object

Good day!
I write a trigger and ran into a problem ((

In the info_c comes this value:
Scott; Kavanagh; The Art Of; Vice President; 416-555-5555; scott@theartof.com | Steve; Comrie; Simplicate Interactive; Owner; 416-555-5555; steve@simplicate.ca | John; Smith; Rogers; Marketing Assistant; 416-555-5555; john.smith @ rogers.com

 

I need to convert this value and add to object Сontacts:

1st contact:
First Name - Scott
Last Name - Kavanagh
Company - The Art Of
Job Title - Vice President
Phone - 416-555-5555
Email - scott@theartof.com

2nd contact:
First Name - Steve
Last Name - Comrie
Company - Owner
Job Title - Vice President
Phone - 416-555-5555
Email - steve@simplicate.ca

 

N nd contact:

.......

 

How do I make it?

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

If the order stays the same, I would first explode it using your PIPE symbol so you can seperate your Sobjects as such.

 

String originalString = 'Scott; Kavanagh; The Art Of; Vice President; 416-555-5555; scott@theartof.com | Steve; Comrie; Simplicate Interactive; Owner; 416-555-5555; steve@simplicate.ca | John; Smith; Rogers; Marketing Assistant; 416-555-5555; john.smith @ rogers.com';

String[] contacts = originalString.split('\\|', 0);
// at this point it should be something like
// contacts[0] = 'Scott; Kavanagh; The Art Of; Vice President; 416-555-5555; scott@theartof.com'
// contacts[1] = 'Steve; Comrie; Simplicate Interactive; Owner; 416-555-5555; steve@simplicate.ca'
// contacts[2] = 'John; Smith; Rogers; Marketing Assistant; 416-555-5555; john.smith @ rogers.com'

// loop through your contacts and created your Sobjects
for(String contactString: contacts){
   String[] contactInfo = contactString.split(';',0);
   Contact c = new Contact();
   c.FirstName = contactInfo.get(0);
   c.LastName = contactInfo.get(1);
   ...
}

 

All Answers

Alex.AcostaAlex.Acosta

If the order stays the same, I would first explode it using your PIPE symbol so you can seperate your Sobjects as such.

 

String originalString = 'Scott; Kavanagh; The Art Of; Vice President; 416-555-5555; scott@theartof.com | Steve; Comrie; Simplicate Interactive; Owner; 416-555-5555; steve@simplicate.ca | John; Smith; Rogers; Marketing Assistant; 416-555-5555; john.smith @ rogers.com';

String[] contacts = originalString.split('\\|', 0);
// at this point it should be something like
// contacts[0] = 'Scott; Kavanagh; The Art Of; Vice President; 416-555-5555; scott@theartof.com'
// contacts[1] = 'Steve; Comrie; Simplicate Interactive; Owner; 416-555-5555; steve@simplicate.ca'
// contacts[2] = 'John; Smith; Rogers; Marketing Assistant; 416-555-5555; john.smith @ rogers.com'

// loop through your contacts and created your Sobjects
for(String contactString: contacts){
   String[] contactInfo = contactString.split(';',0);
   Contact c = new Contact();
   c.FirstName = contactInfo.get(0);
   c.LastName = contactInfo.get(1);
   ...
}

 

This was selected as the best answer
SLockardSLockard

splitting it by the pipe like that separates it by every single character because it is doing a regex. Just escape it like

.split('\\|') 

 

and then it's perfect.

Alex.AcostaAlex.Acosta

Yeah I was just writing out sudo code, didn't really test it. Thanks for looking out.

AkiraDioAkiraDio

Thank you so much!