[SalesForce] How to create a new instance of a custom object using MetaData API

Hi all — I have code to create a custom object, which works, but I actually already have a custom object in my system called Invoice. I guess what I really want to create is a new instance of a custom object (in this case, I want to create new invoices). Does anyone have any examples of creating instances of custom objects? And perhaps an example of a custom object instance that references another object instance? In my case, my setup is that an Opportunity has many Invoices.

Could this even be done using the REST API? I seem to have been misunderstanding the terminology a bit and confused creating a new custom object type with a new custom object instance.

Best Answer

The MetaData API allows new types of custom object to be created programmatically. It sounds like the type of custom object - Invoice - is already there so the MetaData API is not needed.

In Apex code creating an object in memory uses new and adding it to the database uses insert:

Invoice__c inv = new Invoice__c(Opportunity__c = opp.Id);
insert inv;

with the relationship to other objects established by assigning Id values. This overall creates a new object instance. (There is plenty of reference material you can Google on the details around this.)

You can create a REST API (or use one of the built-in Salesforce APIs) that allows the object instance to be created via a web service call. More commonly a Visualforce page and Apex controller would be used to do that, or just the standard layout-based UI that includes a "New" button.

Related Topic