You need to sign in to do that
Don't have an account?
Apex Process to Split Client Name to First and Last Name
Hi all,
I have these 3 fields:
SELECT Name, First_Name__c, Last_Name__c FROM Clients__c
I would like to run a periodical apex process to split the values from Name into First Name and Last Name.
All charachters until 1st space is First Name and everything else is Last Name.
Can anyone please help?
Thanks
I have these 3 fields:
SELECT Name, First_Name__c, Last_Name__c FROM Clients__c
I would like to run a periodical apex process to split the values from Name into First Name and Last Name.
All charachters until 1st space is First Name and everything else is Last Name.
Can anyone please help?
Thanks
You could do this using workflow on the object - using a Field Update to set the "First_Name__c" field to
and the Last_Name__c to
Which is the sections of text left, and right of the first space.
If you really did want to do this using some sort of periodical apex, you would have to think carefully about how many Clients you might load? A scheduled apex job once a day that looked something like:
BE WARNED - there is ZERO error catching and capability in that code though (I'm not your slave!!) but it will pretty much do what you want. You just need to schedule it using "Schedule Apex" - and obviously put in test coverage (and error handling).
I would still ask immediatly why this wasn't done using work flow though, if I saw it in a client org.
All Answers
You could do this using workflow on the object - using a Field Update to set the "First_Name__c" field to
and the Last_Name__c to
Which is the sections of text left, and right of the first space.
If you really did want to do this using some sort of periodical apex, you would have to think carefully about how many Clients you might load? A scheduled apex job once a day that looked something like:
BE WARNED - there is ZERO error catching and capability in that code though (I'm not your slave!!) but it will pretty much do what you want. You just need to schedule it using "Schedule Apex" - and obviously put in test coverage (and error handling).
I would still ask immediatly why this wasn't done using work flow though, if I saw it in a client org.
I do have a Process Builder flow that catches all new and modified records. I would probably only need to run this apex process once, actually, as I want to make sure that all existing records are updated too.
I will modify your process to only look at records where there is no value in First & Last name.
Thanks again
Should be simple enough to modify the Apex then to your needs, as you say, change the SOQL query to something like
the key error checking to do will be that "Name" contains a at least something, and a space..
Otherwise you will throw a bunch of index out of bounds errors on Substring.
(and as always, do a test run in a sandbox, and back your data up before running!)
Good luck.
Much appreciated.
thisClient.Name.Contains(' ') is already boolean so doesn't need the "!= -1".