User Story
Write a code that creates a person account for each contact with the following infos.
- First Name
- Last Name
- Phone
- Email
Codes
List<Contact> contacts = [SELECT Id, FirstName, LastName, Email, Phone FROM Contact];
List<Account> personAccountsToInsert = new List<Account>();
for(Contact con : contacts) {
Account personAccount = new Account();
personAccount.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Account').getRecordTypeId();
personAccount.FirstName = con.FirstName;
personAccount.LastName = con.LastName;
personAccount.PersonEmail = con.Email;
personAccount.PersonMobilePhone = con.Phone;
personAccountsToInsert.add(personAccount);
}
if(!personAccountsToInsert.isEmpty()) {
insert personAccountsToInsert;
}