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
mcc22mcc22 

How to create new Contact (firstname & lastname) from Account.Name

Hello all,

We have orders with associated accounts being created as part of an integration with eCommerce and ERP.

It is essentially creating person accounts since these are consumers. Instead of enabling person accounts, I would like to create a new contact record using the data from the account where Account.Person__c = true (custom field created to designate consumer accounts). 

This would be an easy PB but I'm having trouble setting values with the name fields. Any shared experience on how to "reverse-concatenate" Account.Name into Contact first name & last name?

Thanks in advance
Best Answer chosen by mcc22
ShirishaShirisha (Salesforce Developers) 
Hi Matt,

Greetings!

Hope you find this (https://developer.salesforce.com/forums/?id=906F00000008vVvIAI) thread helpful while creating the formula to get firstName and Lastname from Account Name.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Matt,

Greetings!

Hope you find this (https://developer.salesforce.com/forums/?id=906F00000008vVvIAI) thread helpful while creating the formula to get firstName and Lastname from Account Name.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
This was selected as the best answer
Akshay Dhiman 63Akshay Dhiman 63
Hi MattCole,

Please go through with below line of code

    String AccountName = 'New Account MA';
        if(String.isNotBlank(AccountName)) {
            Contact con = new Contact();
            if(AccountName.contains(' ')) {
                con.FirstName = AccountName.substringBefore(' ');
                con.LastName = AccountName.substringAfter(' ');
                insert con;
            } else {
                con.LastName = AccountName;
                insert con;
            }
        }

Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
mcc22mcc22
Thank you both! I went with the formula fields mentioned in Shirisha's link as this allowed me to easily reference them in the process.
All the best 
David Roberts 4David Roberts 4
And if there's a middle name:
firstName = fullName;
                middleName = '';
                lastName = '';
                if (firstName .contains(' ')){
                    firstName = firstName .substringBefore(' ');
                    lastName = firstName .substringAfter(' ');
                    if (lastname.contains(' ')){
                        middleName = lastname.substringBefore(' ');
                        lastName = lastname.substringAfter(' ');
                    }//endif lastname has space
                }//endif firstname has space
                system.debug('first = '+firstName+' - middle = '+middleName+' - last = '+lastname);