I am new to apex triggers and starting new with trailhead i have this task to do :

Implement database trigger for Order object

● Trigger should create an invoice record for the order record always when a new

order record is created

● Newly created invoice record should be linked to the order record

● Link should be visible in the UI

I have already created a lookup relationship between order and invoice.

trigger createInvoiceRecord on Order (after insert) // After inserting a new order


for (order newOrder : trigger.new){ //for condition on the new order 



i believe this is a good place to start , just cant figure out a logic to connect the new order to the new invoice record.

1 Answer 1


Records are related to one another using Ids. When you view a record in a web browser, Salesforce will display the name of the related record, but that's just something Salesforce does automatically as a convenience. The data stored in the relationship field is actually the record Id.

Another important point to consider is which SObject is the parent, and which is the child. A single relationship in Salesforce is always a one-to-many. The parent record can have many child records (and a child can only have one parent). There are ways to emulate one-to-one and many-to-many relationships, but that doesn't seem to be important to this question.

Based on what you've described so far, it sounds like Order is the parent, and Invoice is the child. That means that you need to take the Order Id (which isn't available in a before insert trigger context, but is available in an after insert context) and put it into the relationship field you created on Invoice, then insert your Invoice records.

That's pretty much all there is to it. You'll probably want to set a few other fields on the Invoice in addition to the relationship field, but that's generally a non-issue.

A quick and dirty example

// DML and queries inside of loops are problematic (unless you really know what
//   you're doing).
// To avoid that, we use collections (lists, maps, and sets) to gather data inside
//   of a loop, and then perform operations on it outside of the loop
List<ChildObject__c> childRecordsToInsert = new List<ChildObject__c>();

for(ParentObject__c parent :trigger.new){
    ChildObject__c aChild = new ChildObject__c(
        // Use the parent's Id to set the relationship
        Parent_Relationship_Field__c = parent.Id,
        Some_Other_Field__c = 'some other value'

    // You don't need to use the SObject constructor to set fields (but it does save 
    //   some typing)
    // You could instead set fields using dot notation (or even set some in
    //   the constructor, and some using dot notation)
    //ChildObject__c aChild = new ChildObject__c();
    //aChild.Parent_Relationship_Field__c = parent.Id;
    //aChild.Some_Other_Field__c = 'some other value';


// DML is OUTSIDE of all loops to help us comply with Salesforce's
//   various governor limits 
insert childRecordsToInsert;

You must log in to answer this question.

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