Hii I need to create an onclick javascript button on a custom object called "Resource"

On clicking the button an Account will be created in the org with the same Name as the Resource Name and if the account already exists with that name it should thrw an error

An account already exists with the same name

I have written the code below but I am getting an error

Cannot set property "Name" of undefined

I am writing my code here.Please guide me with this as I am relatively new to javascript

 {!REQUIRESCRIPT("/soap/ajax/22.0/apex.js" )}
 var comAcc = "{!sked__Resource__c.Name}"; 
 if( comAcc >1) { 
 alert('An Account  already exists; you cannot create another Account.'); 
 } else {
 var connection = sforce.connection;
 var newaccount= new sforce.SObject("Account");
 newaccount.Name = "{!sked__Resource__c.Name}" ; 
 result = sforce.connection.create([newaccount]);
 alert(result );
  • Hi, at which line you are getting the error? Also try to add debugger; before var comAcc = "{!sked__Resource__c.Name}"; then press F12 on object detail page - for Windows keyboard - and then refresh the object detail page and click on the custom button. That will help you debug the code. May 23, 2017 at 5:32

1 Answer 1


Instead of assigning the record type name, assign the ID or do it as following ways:

var recType = sforce.connection.query("SELECT Name, Id FROM RecordType WHERE DeveloperName = 'OR_Open'");
  • 1
    Query would return a list not Id.
    – Raul
    May 23, 2017 at 6:06
  • Hii Shailesh I have used the code something like this "var recordtypeid=sforce.connection.query("Select Id, Name From RecordType Where SobjectType = 'Account' AND Name = 'Employees' ");" .The account should be created with recordType name "Employees" but instead it is created with the record type name "facility account" May 23, 2017 at 6:07
  • Use developer name for the record type. Label of record type can be changed. May 23, 2017 at 6:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .