I'm trying to create a button to execute an Apex Class with the Javascript attached. I'm getting the syntax error: Field object.id does not exist. Check spelling. What am I missing? What is window.location.href= looking for?

enter image description here

Error Msg

Global class InvoiceAccounts_Button {
  Webservice static void createInvoiceAccounts() {
    // Create a list of InvoiceAccounts
    List<InvoiceAccount__c> InvA_List = new List<InvoiceAccount__c> {
         new InvoiceAccount__c(Name='Kim_Button',Account_Owner_Name__c='Shain',Account_Owner_Title__c='Sir')};

    // Bulk insert all InvoiceAccount__cs with one DML call

    insert InvA_List;


You have a few problems with your code. Take a closer look at the example for sforce.apex.execute. The first parameter is the class name, which in your case should be InvoiceAccounts_Button. The second value is the method name, which in your case should be createInvoiceAccounts. The third value is the arguments map, which in your case should be empty.

What you have:

sforce.apex.execute("InvoiceAccount", "createInvoiceAccounts", {});
//                  ^^^^^^^^^^^^^^^^ that's not the name of your class

What you should have:

sforce.apex.execute("InvoiceAccounts_Button", "createInvoiceAccounts", {});

Your second problem is in your merge field. You have {!Object.Id}, but where you have Object, you should instead have the API Name of your specific object.

What you have:

window.location.href = "/{!Object.Id}"

What you should have:

window.location.href = "/{!InvoiceAccount__c.Id}"

Even better:

window.location.href = "{!URLFOR($Action.InvoiceAccount__c.View, InvoiceAccount__c.Id)}"

Object.Id is not a valid field. Use the "Select Field Type" drop down to select the correct field, or assign a return value id from the apex method to a variable and use that.

  • I changed the Object.id to InvoiceAccount.ID and cleared the Syntax problem. I added the button to the pagelayout. The I press the button, I get the error below (which I don't understand).
    – Boots
    Nov 26 '18 at 16:59
  • A problem with the OnClick JavaScript for this button or link was encountered: {faultcode:'soapenv:Client', faultstring:'No service available for class 'InvoiceAccount'', }
    – Boots
    Nov 26 '18 at 17:04

JavaScript Buttons don't work in lightning.

If your code is already in apex, try a URL button, pass necessary information via URL parameters and direct the user to a VF page that has an apex controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.