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
The WalrusThe Walrus 

Adding a database row using an apex class

I built a very simple apex class, based on a test class, to add a row of data to a database table:

 

Private class Invoice_Balance_Record_Inserter {
static void validateInvoice_Payment() {
Invoice_Balance__c i = new
Invoice_Balance__c(Name='I110811EP3-1', 
Invoice__c = 'a00U00000020IgSIAU',
Invoice_Balance_Amount__c=300);
insert i;
 }
}

 The code will run, either in system log or in force ide, but I don't end up with a record in Invoice_Balace__c.   Is there a way I can run this code that will result in a record being added?  thanks

Best Answer chosen by Admin (Salesforce Developers) 
AdyAdy

In System Log: do not use the class or method to run the code. Just write the code and click execute. The record will be inserted.

 

Invoice_Balance__c i = new Invoice_Balance__c(Name='I110811EP3-1',
Invoice__c = 'a00U00000020IgSIAU',
Invoice_Balance_Amount__c=300);
insert i;

All Answers

venturec3venturec3

The first question I would have is where is this being called from? Is this being called from a trigger? or Another class?

 

You may need to also create a trigger to call the method:

trigger Invoice_AfterInsertUpdate on Invoice__c(after insert, after update) {

     Invoice_Balance_Record_Inserter.validateInvoice_Payment(trigger.new);

}

 

Try changing the Private class to Public, then in the method section add:

 

public class Invoice_Balance_Record_Inserter {

     public static void validateInvoice_Payment(list<Invoice__c> lstInvoice) {

          list<Invoice_Balance__c> invBal = new list<Invoice_Balance__c>();

          for(Invoice__c inv : lstInvoice) {

               Invoice_Balance__c i = new Invoice_Balance__c(Name='I110811EP3-1',

                                                                                              Invoice__c = 'a00U00000020IgSIAU',

                                                                                              Invoice_Balance_Amount__c=300);

               invBal.add(i);

          }

 

          database.insert(invBal);

 

     }

}

 

Private classes are generally used only for @isTest classes

 

See if that works for you.

The WalrusThe Walrus

It's not really being called from anywhere - I was hoping to get a quick method to add some rows to my table for testing purposes. 

 

Eventually, I will probable use the data loader or a similar capability to get some excel spreadsheet rows into a table, but that is a little further down the line. Right now, a little script that would add a row would be handy.  Thanks 

AdyAdy

In System Log: do not use the class or method to run the code. Just write the code and click execute. The record will be inserted.

 

Invoice_Balance__c i = new Invoice_Balance__c(Name='I110811EP3-1',
Invoice__c = 'a00U00000020IgSIAU',
Invoice_Balance_Amount__c=300);
insert i;

This was selected as the best answer
The WalrusThe Walrus

thank you - that worked great!