Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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='abc@xyz.com');
insert acc;
share|improve this question
    
you need to create an User Account or a standard salesforce Account? –  highfive Mar 11 at 6:29
    
Can i have some code piece please. –  sunny Mar 11 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 at 6:44
    
I need to create an Account of person recordtype. –  sunny Mar 11 at 6:45
add comment

1 Answer 1

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 at 7:11
    
Is person account enabled in your org? –  Mohith Shrivastava Mar 11 at 8:29
    
Yes i have enabled and i can see it in record type selection too. –  sunny Mar 11 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 at 9:33
1  
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 at 10:47
add comment

Your Answer

 
discard

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.