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
nununinununi 

Button on Lead to create two records on two custom objects using field information from Lead

I have a requirement to create a button on the lead object that creates two custom object records with fields pre-opulated from the Lead record. Can someone provide me a sample code to start with? 
Best Answer chosen by nununi
KaranrajKaranraj
Using custom javascript button you can able to do that.
  • Navigate to Customize | Lead| Button, Links and Actions
  • Create new button of type “Detail Page Button” , behavior “Execute Javascript” and content source “Onclick JavaScript”.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

//Custom Object 1
var customObj = new sforce.SObject("CustomObject1__c"); // Your custom object name
customObj.Id = '{!Lead.Id}';
customObj.Name = '{!Lead.Name}';
var result = sforce.connection.create([customObj]);

if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

All Answers

KaranrajKaranraj
Using custom javascript button you can able to do that.
  • Navigate to Customize | Lead| Button, Links and Actions
  • Create new button of type “Detail Page Button” , behavior “Execute Javascript” and content source “Onclick JavaScript”.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

//Custom Object 1
var customObj = new sforce.SObject("CustomObject1__c"); // Your custom object name
customObj.Id = '{!Lead.Id}';
customObj.Name = '{!Lead.Name}';
var result = sforce.connection.create([customObj]);

if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}

This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help u 
https://success.salesforce.com/answers?id=90630000000gwhCAAQ
http://hometeamconsulting.com/create-a-record-with-a-button/

Please check below blog with step my step process
http://hometeamconsulting.com/create-a-record-with-a-button/

Please let us know if this will help u

Thanks
Amit Chaudhary
nununinununi
Hello guys, thanks for your responses! I have the following code, but when i click on the button,  i get the error: A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected token ILLEGAL
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

//List
var List = new sforce.SObject("List__c"); // Your custom object name
List.Name = '{!Lead.LastName}';
List.Author__c = '{!Lead.List_Author__c}';
List.City__c = '{!Lead.List_City__c}';
List.Category__c = '{!Lead.List_Category__c}';
List.List_Text__c = '{!Lead.List_Introduction__c}';
List.List_Image__c = '{!Lead.List_Picture_URL__c}';
var result = sforce.connection.create([List]);

var p1 = new sforce.SObject("Place__c"); // Your custom object name
p1.Name = '{!Lead.Place_1__c}';
p1.Place_Address__c = '{!Lead.Place_1_Address__c}';
p1.Place_Phone__c = '{!Lead.Place_1_Phone_1__c}';
p1.Phone_2__c = '{!Lead.Place_1_Phone_2__c}';
p1.Place_Cost__c = '{!Lead.Place_1_Cost__c}';
p1.Place_Type__c = '{!Lead.Place_1_Cuisine__c}';
p1.Place_Timmings__c = '{!Lead.Place_1_Timings__c}';
p1.Place_Email__c = '{!Lead.Email}';
p1.Place_Website__c = '{!Lead.Place_1_Website__c}';
p1.Place_Text__c = '{!Lead.Place_1_About__c}';
p1.Place_Picture_URLs__c = '{!Lead.Place_1_Picture_URLs__c}';
p1.List_Name__c = '{!Lead.LastName}';
p1.Author__c = '{!Lead.List_Author__c}';
p1.City__c = '{!Lead.List_City__c}';
p1.Category__c = '{!Lead.List_Category__c}';
var result = sforce.connection.create([p1]);

if(result[0].getBoolean("success")){
   alert('New record updated successfully');
}
else{
  alert('Error : '+result);
}