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
CaitlinGMCaitlinGM 

OnClick Javascript button to create new Opportunity

Is it possible to use a custom button to create a new record? I'd like to put the button on a custom object (Task Order__c) and have it create a new Opportunity, pulling information from the Task Order into the Opportunity fields. I would also like to be able to specify the record type of the new Opportunity. I have cobbled this together based on some examples I found of other buttons. Am I on the right track? Thanks in advance.

 

Here is what I am trying:

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
var newopportunity= new sforce.SObject("Opportunity");
newopportunity.Name = "my new opportunity";
newopportunity.Account="{!Task_Order__c.Account__c}";
newopportunity.StageName="Active";
newopportunity.CloseDate=new Date();
newopportunity.Student_Name__c="TBD";
var result = sforce.connection.create([newopportunity]);
window.location.reload();

 

CaitlinGMCaitlinGM

OK, got it working now, in case anyone needs a reference:

 

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
var connection = sforce.connection;
var newopportunity= new sforce.SObject("Opportunity");
newopportunity.RecordTypeId="01260000000LmxR"
newopportunity.Name = "my new opportunity";
newopportunity.AccountId="{!Task_Order__c.AccountId__c}";
newopportunity.StageName="Active";
newopportunity.CloseDate=new Date();
newopportunity.Student_Name__c="00360000014tpJM";
result = sforce.connection.create([newopportunity]);
alert(result );

 

Mark JaundooMark Jaundoo
Is there a way to create a nested If that says if a picklist is a certain value, create a certain record type of an opportunity? Help would be apprecaited 
Hilary HendersonHilary Henderson
Is there a way to do this same thing but it still shows the record type selection page?