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
NewbyVFNewbyVF 

Javascript custom button to create new record

I have a button on Lead records that will create a new record and prepopulate fields from the lead to the New record in a custom object, redirect to the new record to edit and complete.  It works fine except for I want it not to automatically save the record until the user clicks save on the newly created record, because they need to be able to cancel and no record created.  Below is my current code.
var Order = new sforce.SObject('New_ad_sales__c');


Order.Name = '{!Lead.Company}';
Order.Contact_First_Name__c = '{!Lead.FirstName}';
Order.Contact_Last_Name__c = '{!Lead.LastName}';
Order.email__c = '{!Lead.Email}';
Order.Advertiser_phone__c = '{!Lead.Phone}';
Order.billing_address__c = '{!Lead.Street}';
Order.city__c = '{!Lead.City}';
Order.state__c = '{!Lead.State}';
Order.zip__c = '{!Lead.PostalCode}';
Order.cell_phone__c = '{!Lead.MobilePhone}';
Order.LeadSource__c = '{!Lead.LeadSource}';


result = sforce.connection.create([Order]); 
var newurl = window.open.href

if(result[0].getBoolean("success")){
window.location = "/" + result[0].id + "/e";
}else{
alert('Could not create record '+result);
}
Suneel#8Suneel#8
Is it fine if you can achieve the same functionality through VF page and Apex itself?
NewbyVFNewbyVF
I really didn't want to create VF page because like I said this works great at creating a new order and prepopulating and redirecting to the page to continue.  I just want the user to have the ability to cancel and not have a record created.
 
NewbyVFNewbyVF
Responding to the other part "Also what is the problem with default Clone button on Leads?"  It is not clone the lead it is creating a record in a custom object with some data from the lead.
 
Suneel#8Suneel#8
Yes,I understand.Here you are copying the data from one object to other object and want to show the data on the page without saving it to database.That is only possible by creating a page and assigning the values to those input text fields.Also you cannot use clone on Order(Custom Object) as source is Lead object
NewbyVFNewbyVF
Ok - I just thought maybe I could add something to the existing code so that it did not save it until the user clicked Save on the new record.  I will think about a VF page.  Thank you.