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
Nils11Nils11 

How to use sforce in javascript to update record ?

I have to update the lead's record type and owner name and assign new record type and current user and owner using javascript code.

I have written below code code on button but its not working.

var LeadToUpdate = new sforce.SObject("Lead"); 
if (LeadToUpdate.RecordTypeId=="012V0000000werfdgt") { 
LeadToUpdate.Id = "{!Lead.Id}"; 
LeadToUpdate.RecordTypeId = "01220000000aser"; 
LeadToUpdate.ownerId = "{!$User.Id}"; 
sforce.connection.update([LeadToUpdate]); 
window.location.href='/{!Lead.Id}/e?012120000012cZt'; 
}
Amit Chaudhary 8Amit Chaudhary 8
Please try like below
if( "{!Lead.RecordTypeId}" == "012V0000000werfdgt") 
{
	var LeadToUpdate = new sforce.SObject("Lead"); 
	LeadToUpdate.Id = "{!Lead.Id}";
	LeadToUpdate.RecordTypeId = "01220000000aser"; 
	LeadToUpdate.ownerId = "{!$User.Id}"; 

	var result = sforce.connection.update([LeadToUpdate]);
	if (result[0].getBoolean("success"))
	{
		//window.location.href="/{!Lead.Id}"
		window.location.href='/{!Lead.Id}/e?012120000012cZt'; 
	} 
}
Let us know if this will help you
 
Nils11Nils11
Hi Amit CHoudhary 8,

I am facing below error in console.

Refused to set unsafe header "User-Agent"