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
Beth Gonzalez 3Beth Gonzalez 3 

Error in Opportunity Creation = Type Error:sforce.connection.insert is not a function?

I am in the process of creating a custom javascript that appears on a opportunity list view. However when I test the button, I recieve the following error: Error in Opportunity Creation = Type Error:sforce.connection.insert is not a function?

The code I am using, I found in the forum and I modified. I am not sure how to resolve the error. I am not a developer. I appeciate the help. 
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")};
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Opportunity)};
var CreateNewRecords = []; 
try
{

if (records[0] == null)
{
	alert("Please select at least one record to update.");
} 
else
 {
	for (var a=0; a<records.length; a++) 
	{
		var create_new_opportunity = new sforce.SObject("opportunity");
		create_new_opportunity.RecordTypeId = "012a0000001ZUXg"; 
		create_new_opportunity.AccountId = "{!Opportunity.Account}"; 
		create_new_opportunity.Closedate =new Date("{TODAYY()}");
		create_new_opportunity.OwnerId = "{!Opportunity.OwnerId}";   
		create_new_opportunity.StageName= "Info Gathering/Investigation";
                create_new_opportunity.Strategy_Type__c= "Full Launch Target";
		CreateNewRecords.push(create_new_opportunity);
	}
var result = sforce.connection.insert(CreateNewRecords);
if (result[0].success=='false')
{
	alert(result[0].errors.message);
}
elese
{
	location.reload(true);
}

}
}
catch(error)
{
alert("Error In Opportunity creation  = "+error);
}

 
Best Answer chosen by Beth Gonzalez 3
karthikeyan perumalkarthikeyan perumal
Hello 

use this below updated code its help t create new Opp using javascript, 
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")};
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Opportunity)};
var CreateRecords = sforce.connection.retrieve("Name,CloseDate,StageName,AccountId", "opportunity", records);
var CreateNewRecords = []; 

if (records[0] == null)
{
	alert("Please select at least one record to update.");
} 
else
 {
	for (var a=0; a<CreateRecords.length; a++) 
	{
alert('Test1');
		var create_new_opportunity = new sforce.SObject("opportunity");

		create_new_opportunity.RecordTypeId = "012a0000001ZUXg";

		create_new_opportunity.Name= CreateRecords[a].Name+'New';
               
                create_new_opportunity.AccountId =  CreateRecords[a].AccountId ;

		create_new_opportunity.CloseDate=CreateRecords[a].CloseDate;
		  
		create_new_opportunity.StageName= CreateRecords[a].StageName;
              
		CreateNewRecords.push(create_new_opportunity);
	}
var result = sforce.connection.create(CreateNewRecords);

if (result[0].success=='false')
{
	alert(result[0].errors.message);
}
else
{
	location.reload(true);
}

}

Hope this will help you, 

Mark Best ANSER if its work for you. 

Thanks
karthik
 

All Answers

Brian Cherry FWIBrian Cherry FWI
var result = sforce.connection.insert(CreateNewRecords);

change to:

var result = sforce.connection.create(CreateNewRecords);
 
karthikeyan perumalkarthikeyan perumal
Hello 

use this below updated code its help t create new Opp using javascript, 
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")};
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Opportunity)};
var CreateRecords = sforce.connection.retrieve("Name,CloseDate,StageName,AccountId", "opportunity", records);
var CreateNewRecords = []; 

if (records[0] == null)
{
	alert("Please select at least one record to update.");
} 
else
 {
	for (var a=0; a<CreateRecords.length; a++) 
	{
alert('Test1');
		var create_new_opportunity = new sforce.SObject("opportunity");

		create_new_opportunity.RecordTypeId = "012a0000001ZUXg";

		create_new_opportunity.Name= CreateRecords[a].Name+'New';
               
                create_new_opportunity.AccountId =  CreateRecords[a].AccountId ;

		create_new_opportunity.CloseDate=CreateRecords[a].CloseDate;
		  
		create_new_opportunity.StageName= CreateRecords[a].StageName;
              
		CreateNewRecords.push(create_new_opportunity);
	}
var result = sforce.connection.create(CreateNewRecords);

if (result[0].success=='false')
{
	alert(result[0].errors.message);
}
else
{
	location.reload(true);
}

}

Hope this will help you, 

Mark Best ANSER if its work for you. 

Thanks
karthik
 
This was selected as the best answer
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi,

In connection plug-in, we don't have method with name sforce.connection.insert. Try using sforce.connection.create it should work.

Thanks!
shiva.sfdc.backup@gmail.com