Skip to content

Instantly share code, notes, and snippets.

@sbob-sfdc
Created September 2, 2012 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbob-sfdc/3605667 to your computer and use it in GitHub Desktop.
Save sbob-sfdc/3605667 to your computer and use it in GitHub Desktop.
Workshop 201, Tutorial 5, Step 1, Apex
trigger DeleteRestrictInvoice on Invoice__c (before delete) {
// create a list of Invoices in Trigger.oldMap along with their Line Items
List<Invoice__c> invoices = [Select i.Name, (Select Name From Line_Items__r)
From Invoice__c i
Where i.Id IN :Trigger.oldMap.keySet()];
// loop through the Invoices, attaching errors to those that have Line Items
for (Invoice__c invoice : invoices) {
if (!invoice.Line_Items__r.isEmpty()) {
Trigger.oldMap.get(invoice.id).addError('Cannot delete Invoice with Line Items');
}
}
}
@PlatinumItguru
Copy link

@istest
private class TestDeleteRestrictInvoice {
// Invoice generator, with or without a Line Item
static Invoice__c createNewInvoice(Boolean withLineItem) {
// Create test Merchandise
Merchandise__c merchandise = new Merchandise__c(
Name = 'Test Laptop',
Quantity__c = 1000,
Price__c = 500
);
insert merchandise;

    // Create test Invoice
    Invoice__c invoice = new Invoice__c();
    insert invoice;

    // Create test Line Item and insert it into the database, if withLineItem == true
    if (withLineItem) {
        Line_Item__c item = new Line_Item__c(
            name = '1',
            Quantity__c = 1,
            Merchandise__c = merchandise.Id,
            Invoice__c = invoice.Id
        );
        insert item;
    }
    return invoice;
}
// Single row Invoice with no Line Items => delete
static testMethod void verifyInvoiceNoLineItemsDelete(){
    // Create test Invoice and insert it
    Invoice__c invoice = createNewInvoice3(false);

    // Delete the Invoice, capture the result
    Database.DeleteResult result = Database.delete(invoice, false);

    // Assert success, because target Invoice3 doesn't have Line Items
    System.assert(result.isSuccess());
}

// Single row Invoice with Line Items => delete restrict
static testMethod void verifyInvoiceLineItemsRestrict(){
    // Create test Invoice and Line Item and insert them
    Invoice__c invoice = createNewInvoice(true);

    // Delete the Invoice, capture the result
    Database.DeleteResult result = Database.delete(invoice, false);

    // Assert failure-not success, because target Invoice has tracks
    System.assert(!result.isSuccess());
}

// Bulk delete of Invoice, one without Line Items, another with
static testMethod void verifyBulkInvoiceDeleteRestrict(){
    // Create two test Invoices, one with and without a Line Item
    Invoice__c[] invoices = new List<Invoice__c>();
    invoices.add(createNewInvoice(false));
    invoices.add(createNewInvoice(true));

    // Delete the Invoices, opt_allOrNone = false, capture the results.
    Database.DeleteResult[] results = Database.delete(invoices, false);

    // Assert success for first Invoice
    System.assert(results[0].isSuccess());
    // Assert not success for second Invoice
    System.assert(!results[1].isSuccess());
}

}

@zwb1988
Copy link

zwb1988 commented Dec 27, 2016

Thanks for the tip.

Merchandise is an required field. I realized that I can't access current DB by selecting first record from the Merchandise table in an Unite Test.

You must create a dummy object for testing, which it does make sense. Finally, I can complete my hands on tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment