Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a person account using the following code. Its throwing the following error: "Invalid constructor syntax, name=value pairs can only be used for SObjects". How to fix this.

Account Acc = new Account(FirstName='test1',LastName='test2',Email='');
insert acc;
share|improve this question
you need to create an User Account or a standard salesforce Account? – highfive Mar 11 '14 at 6:29
Can i have some code piece please. – sunny Mar 11 '14 at 6:33
sure, but need some details. you need to create an User or an Account? if you are talking about salesforce Person Accounts, then this will be helpful – highfive Mar 11 '14 at 6:44
I need to create an Account of person recordtype. – sunny Mar 11 '14 at 6:45
RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];
Account newPersonAccount = new Account();

// for person accounts we can not update the Name field instead we have to update the    FirstName and LastName individually

 newPersonAccount.FirstName = 'Fred';
 newPersonAccount.LastName = 'Smith';
 newPersonAccount.RecordType = personAccountRecordType;
 insert newPersonAccount;

You will need to insert Account of RecordType of Person Account .Use developer name of record type as best practice.

share|improve this answer
I tried to create the same and when i save the code i get the error as following "Variable does not exist: FirstName" or "Variable does not exist: LastName" – sunny Mar 11 '14 at 7:11
Is person account enabled in your org? – Mohith Shrivastava Mar 11 '14 at 8:29
Yes i have enabled and i can see it in record type selection too. – sunny Mar 11 '14 at 8:43
Monith's code works fine for me (with trailing ' added) in a person account org I have. There is probably some other syntax error in your code because the error message would be "Invalid field FirstName for SObject Account" if the issue was the field name. – Keith C Mar 11 '14 at 9:33
FYI If your code is in a Managed Package then you will get errors when you try to create a managed package because you cannot reference PersonAccount fields in a managed package. You will need to reference the fields dynamically to solve this. The fields can be set like this: newPersonAccount.put('FirstName', 'Fred'); – BarCotter Mar 11 '14 at 10:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.