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
kelly.lu1.3940702104676975E12kelly.lu1.3940702104676975E12 

use Javascript and the Salesforce API behind custom button how to get the recordtype

I want to get the recordtype or other fields in javascript on custom button the example code as next: How to get the recordtypeId  from the for loop?



{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")}
var url = parent.location.href;
var records = {!GETRECORDIDS($ObjectType.Account)};
var updateRecords = [];

if (records[0] == null) {
    alert("Please select at least one record to update.");
} else {
   for (var a=0; a < records.length; a++) {
     var update_Item = new sforce.SObject("Account");
     update_Item.Id = records[a];
     update_Item.MyCustomField__c = "My Special Value";
     updateRecords.push(update_Item);
   }
   //This line does the actual update
   result = sforce.connection.update(updateRecords);
      
   //Now check if there are any errors
   var hasErrors = false;
   var errorReport = "The following Accounts could not be updated:\n";
   for (var j = 0; j < result.length; j++) {
          if (!result[j].getBoolean("success")) {
            hasErrors = true;
            errorReport += ("AccountId: " + updateRecords[j].Id + ", error: " + result[j].errors.message + "\n");
            if (j == 10 && j < result.length) {
              errorReport += ("Maximum errors to display.  Will show first 10 out of " + "result.length\n");
              break;
            }
          }
        }

   if (hasErrors) {
     alert(errorReport);
   }
   parent.location.href = url;
}
Best Answer chosen by kelly.lu1.3940702104676975E12
Ramu_SFDCRamu_SFDC
You would have to query for all the record id's and record type id's initially  something as below

var qr = sforce.connection.query(“SELECT id,recordtypeid FROM Account);

and then loop through the records using for loop

for (var i=0;i<qr.records.length;i++)
{
update_Item.recordtypeid= qr.records[i].recordtypeid;

}

hope this helps.

All Answers

Ramu_SFDCRamu_SFDC
You would have to query for all the record id's and record type id's initially  something as below

var qr = sforce.connection.query(“SELECT id,recordtypeid FROM Account);

and then loop through the records using for loop

for (var i=0;i<qr.records.length;i++)
{
update_Item.recordtypeid= qr.records[i].recordtypeid;

}

hope this helps.
This was selected as the best answer
kelly.lu1.3940702104676975E12kelly.lu1.3940702104676975E12
Thank you ,this works great:)