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
Durai SaravananDurai Saravanan 

Change record type on a Custom button click

Hi,

I would like to have a 'Close Project' (Custom button) in my 'Project'(Custom Object) record's page layout. When i click the 'Close Project' button, the record type of particular record must change from 'Current' to 'Closed'.

How i should do this? Thanks in advance

Thanks and regards,
Durai
Best Answer chosen by Durai Saravanan
Khan AnasKhan Anas (Salesforce Developers) 
Hi Durai,

Greetings to you!

You can update the Record Type through an OnClick JavaScript button.
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

// identify the record
var o = new sforce.SObject("Project__c");
o.id = "{!Project__c.Id}";

// Update the Record Type
o.RecordTypeId = "XXXXXXXXXXXXX"; //You can also Query the RecordTypeId 

// save the change
sforce.connection.update([o]);

//refresh the page
window.location.reload();

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Durai,

Greetings to you!

You can update the Record Type through an OnClick JavaScript button.
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}

// identify the record
var o = new sforce.SObject("Project__c");
o.id = "{!Project__c.Id}";

// Update the Record Type
o.RecordTypeId = "XXXXXXXXXXXXX"; //You can also Query the RecordTypeId 

// save the change
sforce.connection.update([o]);

//refresh the page
window.location.reload();

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Abdul KhatriAbdul Khatri
Please use the followng Javascript in your Custom Button
{!requireScript("/soap/ajax/44.0/connection.js")}

resultSet = sforce.connection.query("Select Name, Id from RecordType Where DeveloperName ='RecordType1' And SObjectType = 'Project__c'");
records = resultSet.getArray("records");

var project = new sforce.SObject("Project__c");
project.Id = '{!Project__c.Id}';
project.RecordTypeId = records[0].Id;
result = sforce.connection.update([project]);

if (result[0].getBoolean("success")) {
  alert("project updated");
  document.location.reload(true);
} else {
  alert(result[0]);
}

Please change the DeveloperName and SObjectType and also SObject Name as per your details as I used for the test.
Shubham4462Shubham4462
Hello Durai ,

In salesforce classic you can do it like that
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')} 
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')} 


var result = sforce.connection.query("select Id,Name from RecordType where sObjectType='Opportunity' And Name='Closed'");
var records = result.getArray("records");
var oppToUpdate = new sforce.SObject("Opportunity");
oppToUpdate .id = "{!Opportunity.Id}";
oppToUpdate.RecordTypeId= records[0].Id;
result = sforce.connection.update([oppToUpdate]);
window.location.reload();