Tuesday, December 15, 2015

Dynamically select the recordtype by custom javascript Button

When i tried to create a contact record without selection of record type it is not possible from the standard button. So i came up with some custom java script button.

To create a custom button navigate to Setup- Customize - Contacts - Buttons, Links, and Actions
Click on Buttons, Links, and Actions and click new Button or link. Give the Name and api name and select the Display Type as List Button, behavior as Execute javascript and content source as onclick javascript.

Paste the below code in the space provided. change the recordtype name to your record type name.
{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 

var qr = sforce.connection.query("SELECT Id FROM RecordType WHERE SobjectType ='" + "Contact" + "'" +" AND DeveloperName = '"+"RecordTypeName"+ "'"); 
var records = qr.getArray("records"); 

if(!records.isEmpty)
{ 
 var redirecturl="/003/e?retURL={!Account.Id}&con4={!Account.Name}&con4_lkid={!Account.Id}&RecordType="+qr.records.Id; 
 window.location=redirecturl; 
}