0

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.

2 Answers 2

2

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.

1
  • Thank you! I was able to use the REST API to create new Invoices by referencing Invoice__c as you suggested. Nov 9, 2015 at 14:04
1

Since you mentioned terminology, in Salesforce.com :

  1. A CustomObject is equivalent to a Database table. There are some "standard" customobjects like Opportunity which are part of the CRM product offering ( provided by Salesforce ). You can create your own custom customobjects like Invoice__c. But ultimately they are tables.

  2. While refering to a standard object in code, only the Objectname( API name) is required and for custom objects the API name takes the form of Objectname__c

    eg. :

    Opportunity oppty;
    
    Account acnt;
    
    Invoice__c customerInvoice; 
    
    Order__c prdOrder;
    
  3. Instance of a CustomObject ( Standard or Custom) is equivalent to a Row in the table.

  4. CustomField is equivalent to a column in the database table. Again, there are standard fields provided by Salesforce and we can create our own customfields on standard or custom customobjects.

  5. Relationship is equivalent to Foreign-Key.

    Example :

    1. Account acnt = [Select Id from Account where ... Limit 1 ];      
    2. Opportunity oppty = new Opportunity( Name="TestOppty1", AccountId= acnt.Id);
    

    Here AccountId is the Foreign Key field name on Opportunity object which holds the Id of the parent Account record.

  6. We can use appropriate DML operation on the instances thus created to make them persist in the Salesforce database table ( custom object )

    e.g. :

    3. insert oppty;
    
    • Before oppty is inserted it is a new In-Memory instance of the object ( same like a Java object)

    • After oppty is inserted it becomes a new row in the Opportunity table and also continues to be the In-Memory instance as well

You must log in to answer this question.

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