2

This code works in Execute Anonymous, but when I try to make an Apex class I get this error on line: Expecting '}' but was: 'insert'. Any thoughts? Thanks.

public class InvoiceAccounts {
// Create a list of InvoiceAccounts
List<InvoiceAccount__c> InvA_List = new List<InvoiceAccount__c> {
 new InvoiceAccount__c(Name='Joe',Account_Owner_Name__c='Smith',Account_Owner_Title__c='Sir'),
new InvoiceAccount__c(Name='Kathy',Account_Owner_Name__c='Smith',Account_Owner_Title__c='Sir'),
new InvoiceAccount__c(Name='Caroline',Account_Owner_Name__c='Roth',Account_Owner_Title__c='Sir'),
 new InvoiceAccount__c(Name='Kim',Account_Owner_Name__c='Shain',Account_Owner_Title__c='Sir')};

// Bulk insert all InvoiceAccount__cs with one DML call

insert InvA_List;

}
3

Within a class, executable code must be inside of a method. Typically, this means you simply add a line for the method signature, and another closing curly bracket.

public class InvoiceAccounts {
  public static void createInvoiceAccounts() {
    // Create a list of InvoiceAccounts
    List<InvoiceAccount__c> InvA_List = new List<InvoiceAccount__c> {
      new InvoiceAccount__c(Name='Joe',Account_Owner_Name__c='Smith',Account_Owner_Title__c='Sir'),
      new InvoiceAccount__c(Name='Kathy',Account_Owner_Name__c='Smith',Account_Owner_Title__c='Sir'),
      new InvoiceAccount__c(Name='Caroline',Account_Owner_Name__c='Roth',Account_Owner_Title__c='Sir'),
      new InvoiceAccount__c(Name='Kim',Account_Owner_Name__c='Shain',Account_Owner_Title__c='Sir')};

    // Bulk insert all InvoiceAccount__cs with one DML call

    insert InvA_List;

  }
}

Which you then call as a static method:

InvoiceAccounts.createInvoiceAccounts();

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.