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
PlainviewPlainview 

Custom Button to Convert Contact to Person Account

Hello All,

I've been assigned the task of creating a custom button on the Contact object to automagically convert Contacts to Person Accounts. I came across a great post online here:

http://www.x2od.com/2008/08/19/convert-between-business-and-person-accounts-b2b-b2c#comment-70797

... which seems like the direction I need to go. I did create the custom button to execute Java - here's the code:

{!REQUIRESCRIPT("/soap/ajax/13.0/connection.js")} 
var ContactObj = new sforce.SObject("Account"); 
ContactObj.Id = '{!Account.Id}'; 
ContactObj.RecordTypeId = '0120000000099Lo'; 
sforce.connection.update([ContactObj]); 
location.reload(true);

No syntax errors, created the button, put it on the Contact layout, created a test Contact, hit the button ... and ... zippo; nothing happens, not even an error message.

Anyone have a clue what I might be missing?

Thanks!
Best Answer chosen by Plainview
PlainviewPlainview
... for those of you playing along at home; I did finally get this all working. One important tip - you must have a "Contact" record type on the Contact Object for these to work; that was why I was getting the cryptic "Cannot read property 'Id' of undefined" error message.

JavaScript for Custom Button to create a Person Account from a Contact:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 
var AccountObj = new sforce.SObject("Account"); 
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Person Account\' limit 1"); 
var records = result.getArray("records"); 
AccountObj.RecordTypeId = records[0].Id; 
AccountObj.LastName= "{!Contact.LastName}"; 
AccountObj.FirstName= "{!Contact.FirstName}"; 
res = sforce.connection.create([AccountObj]); 
window.open('/'+res[0].id);

JavaScript for Custom Button to create a Contact from a Person Account:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 
var ContactObj = new sforce.SObject("Contact"); 
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Contact\' limit 1"); 
var records = result.getArray("records"); 
ContactObj.RecordTypeId = records[0].Id; 
ContactObj.FirstName= "{!Account.FirstName}"; 
ContactObj.LastName= "{!Account.LastName}"; 
res = sforce.connection.create([ContactObj]); 
window.open('/'+res[0].id);


 

All Answers

AshwaniAshwani
Perhaps the clue is behind the statement "location.reload(true);". You are not able to see any error becasue as soon as you click and statement execute page reloads again. So any error occurs just omitted. 

If you still don't find working the you can put alert(); always.

Thanks
Ashwani
PlainviewPlainview
Great - thanks! The larger issue for me is that a Person Account is not being automatically created by the button, either. Hmmm ....
SaranshSaransh
Few things you need to consider :

1. Never use hard coded ids in your code.
2.If you creating a person account in that case you have to copy all values of Contact to new account object and then have to perform create operation instead of update because person account is a contact but still it is an Account. So you have to create a object of Account not contact.

Try below code and here I have only considered "Name" field but you have to copy other fields too in same way.
 
{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 
var AccountObj = new sforce.SObject("Account"); 

result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Person Account\'  limit 1");

var records = result.getArray("records");
AccountObj.RecordTypeId = records[0].Id;
AccountObj.Name= "{!Contact.Name}";

sforce.connection.create([AccountObj]); 

location.reload(true);
PlainviewPlainview
Thanks, Saransh! I revised the code per your suggestion and the desired action is unfortunately still not occurring - creating a Person Account record from the Contact. However, your suggestions are great and thanks for reminding me about hardcoding; I was just copying the format that I found on the link for this button. If you have any more ideas why this is not working, I'm all ears - I do appreciate your time.
PlainviewPlainview
New to Java, so please bear with me:

Created the Custom Button to convert a Contact to a Person Account, which is working. Here's the final code for that:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var AccountObj = new sforce.SObject("Account");
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Person Account\' limit 1");
var records = result.getArray("records");
AccountObj.RecordTypeId = records[0].Id; AccountObj.LastName= "{!Contact.LastName}";
AccountObj.FirstName= "{!Contact.FirstName}";
res = sforce.connection.create([AccountObj]);
window.open('/'+res[0].id);

Now, I want to go the other way - Person Account to Contact:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}
var ContactObj = new sforce.SObject("Contact");
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Contact\' limit 1");
var records = result.getArray("records");
ContactObj.RecordTypeId = records[0].Id;
ContactObj.FirstName= "{!Account.FirstName}";
ContactObj.LastName= "{!Account.LastName}";
res = sforce.connection.create([ContactObj]); window.open('/'+res[0].id);

... but receive this message when I test the button to convert a Person Account to a Contact:

"Cannot read property 'Id' of undefined"

Hmmm ....
PlainviewPlainview
... for those of you playing along at home; I did finally get this all working. One important tip - you must have a "Contact" record type on the Contact Object for these to work; that was why I was getting the cryptic "Cannot read property 'Id' of undefined" error message.

JavaScript for Custom Button to create a Person Account from a Contact:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 
var AccountObj = new sforce.SObject("Account"); 
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Person Account\' limit 1"); 
var records = result.getArray("records"); 
AccountObj.RecordTypeId = records[0].Id; 
AccountObj.LastName= "{!Contact.LastName}"; 
AccountObj.FirstName= "{!Contact.FirstName}"; 
res = sforce.connection.create([AccountObj]); 
window.open('/'+res[0].id);

JavaScript for Custom Button to create a Contact from a Person Account:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")} 
var ContactObj = new sforce.SObject("Contact"); 
result = sforce.connection.query("Select r.Name, r.IsActive, r.Id From RecordType r where r.Name = \'Contact\' limit 1"); 
var records = result.getArray("records"); 
ContactObj.RecordTypeId = records[0].Id; 
ContactObj.FirstName= "{!Account.FirstName}"; 
ContactObj.LastName= "{!Account.LastName}"; 
res = sforce.connection.create([ContactObj]); 
window.open('/'+res[0].id);


 
This was selected as the best answer
Darren FerneyhoughDarren Ferneyhough
I used your code for a button to comvert contact to person account and I got your same error message "Cannot read property 'Id' of undefined" - the contact record does indeed have a record type, is there something I'm missing?