[SalesForce] Condition on RecordType.Name onClick JavaScript on Custom Button in Salesforce

I am trying this on my Custom JavaScript button

IF( '{!Account.recordtype.name}' ='First RecordType'){
     // Do Something 
 }
ELSE{
    // Do something else
 }

and when I click on "Check Syntax" I got this error –

Error: Field Account.recordtype.name does not exist. Check spelling.

Answer : Please correct if I am missing something/doing in wrong way in below code- to get what I want.

    {!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
    var recordtypeResult1 = sforce.connection.query("Select id FROM RecordType WHERE Name='First RecordType'");
    var recordType1 = recordtypeResult1.getArray("records");
    var recordtypeResult2 = sforce.connection.query("Select id FROM RecordType WHERE Name='Second RecordType'");
    var recordType2 = recordtypeResult2.getArray("records");

    if( ('{!Account.recordtypeid}'== recordType1[0].Id || '{!Account.recordtypeid}' ==recordType2[0].Id)
  {
      //DO Something
}  

Any help or suggestions ??!

Best Answer

If you don't mind hard-coding the record type ID:

if('{!Account.RecordTypeId}' == '012i0000000xC4AAAU') {
    ...
}

There is no simple way to use the recordtype's name in this context. (You can query to get the ID from the name.)

Related Topic