1

like lead object i developed one custom button..when ever i click on button i need to create account and person account(person account is enabled in my org).

this my code. account is created but person account is not created

{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}

var accnt = new sforce.SObject("Account");
accnt.Id = '{!Account.Id}';
accnt.Name = prompt('', '{!My_Leads__c.Lead_Name__c}');

var result = sforce.connection.create([accnt]);
if (result[0].getBoolean("success")) {
    alert('Account created successfully');

}
var cnt = new sforce.SObject("Account");
cnt.Name = prompt('', '{!My_Leads__c.Lead_Name__c}');

var result1 = sforce.connection.create([cnt]);
if (result1[0].getBoolean("success")){
    alert('Person contact created successfully');
    window.location.reload();
}
else {
    alert('Error : ' + result);
}

2 Answers 2

1

In order to create a person account, you'll have to set the recordtype id.

cont.RecordTypeId = '<person account record type id>';

You can hardcode the record type Id, use a Label, use a Custom Setting or query for it.

0

{!REQUIRESCRIPT("/soap/ajax/37.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/37.0/apex.js")}

var accnt = new sforce.SObject("Account"); var recType=sforce.connection.query("SELECT name, id FROM RecordType" ); //alert(recType); var records = recType.getArray("records"); //alert(records); accnt.Name='{!My_Leads__c.Company__c}'; if(accnt.Name !="") { accnt.RecordTypeId = records[0].Id; //alert(records[0].Id);

accnt.Id = '{!Account.Id}'; accnt.Name = prompt('','{!My_Leads__c.Company__c}'); accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]); if(result[0].getBoolean("success")) { alert('Account created successfully');

} var cnt= new sforce.SObject("Contact"); cnt.Id = '{!Contact.Id}'; cnt.OwnerId='{!My_Leads__c.OwnerId}'; cnt.AccountId=result[0].id; cnt.lastName = prompt('','{!My_Leads__c.Lead_Name__c}');

var result = sforce.connection.create([cnt]); if(result[0].getBoolean("success")) { alert('contact created successfully'); window.location.reload();

}

else{ alert('Error : '+result); } } else { accnt.RecordTypeId=records[1].Id; //alert(records[1].Id); accnt.Id = '{!Account.Id}'; accnt.LastName = prompt('','{!My_Leads__c.Lead_Name__c}'); accnt.OwnerId='{!My_Leads__c.OwnerId}';

var result = sforce.connection.create([accnt]); if(result[0].getBoolean("success")) { alert('Account created successfully');

} else { alert('Error : '+result); } }

2
  • I need to delete this records after converted ?how to do it Commented Jun 27, 2016 at 5:49
  • After convert my lead object records i need to delete those records automatically Commented Jun 27, 2016 at 8:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .