Trigger :
- trigger RestrictInvoiceDeletion on Invoice_Statement__c (before delete)
- {
- // With each of the invoice statements targeted by the trigger and that have line items, add an error to prevent them from being deleted.
- for (Invoice_Statement__c invoice :[SELECT Id
- FROM Invoice_Statement__c WHERE Id IN (SELECT Invoice_Statement__c FROM Line_Item__c) AND Id IN :Trigger.old])
- {
- Trigger.oldMap.get(invoice.Id).addError('Cannot delete invoice statement with line items');
- }
- }
Line_Item__c is the Junction Object
Screenshot for the Relationship
Test Data Factory
Create a Test Data factory class instead of every time creating
records in test class.
- @isTest
- public class TestDataFactory
- {
- public static Invoice_Statement__c createInvoice(boolean inv)
- {
- Invoice_Statement__c invoice=createInvoice();
- if(inv==true)
- {
- Merchandise__c me=createMerchandise();
- createLineItem(invoice,me);
- }
- return invoice;
- }
- private static Merchandise__c createMerchandise()
- {
- Merchandise__c mer=new Merchandise__c(Name='Test',Description__c='test description',Price__c=1000,Total_Inventory__c=10);
- insert mer;
- return mer;
- }
- private static Invoice_Statement__c createInvoice()
- {
- Invoice_Statement__c invRec=new Invoice_Statement__c(Description__c='test description');
- insert invRec;
- return invRec;
- }
- private static Line_Item__c createLineItem(Invoice_Statement__c inv,Merchandise__c mer)
- {
- Line_Item__c li=new Line_Item__c(Invoice_Statement__c=inv.id,Merchandise__c=mer.Id,Unit_Price__c=mer.Price__c);
- insert li;
- return li;
- }
- }
Test Class
- @isTest
- private class RestrictInvoiceDeleteTest
- {
- static testMethod void nonDeletedData()
- {
- Invoice_Statement__c inv=TestDataFactory.createInvoice(true);
- Test.startTest();
- Database.DeleteResult delRes=Database.Delete(inv,false);
- Test.stopTest();
- System.assert(!delRes.isSuccess());
- }
- static testMethod void deletedData()
- {
- Invoice_Statement__c inv=TestDataFactory.createInvoice(false);
- Test.startTest();
- Database.DeleteResult delRes=Database.Delete(inv,false);
- Test.stopTest();
- System.assert(delRes.isSuccess());
- }
- static testMethod void testBulkDelete()
- {
- List<Invoice_Statement__c> invList=new List<Invoice_Statement__c>();
- invList.add(TestDataFactory.createInvoice(true));
- invList.add(TestDataFactory.createInvoice(false));
- Test.startTest();
- Database.DeleteResult[] delRec=Database.Delete(invList,false);
- Test.stopTest();
- System.assert(!delRec[0].isSuccess());
- System.assert(!delRec[1].isSuccess());
- }
- }
Note: The above
example provided by salesforce only. This example is very useful for
beginners. If you have a doubts on this example please comment it.. I will help for you...!
Thanks for reading this post...! Hope this will helpful for you.........!
Hi
ReplyDeleteWould you pls post the same using Lead and Campaign sObjects
Thanks
suresh.vempally@gmail.com
hi Arun,why we use start test and stop test?
ReplyDeleteplx explain
@Anji,
ReplyDelete1. If you want to test your schedule, batch classes you need the test within the startTest() and stopTest(), Because within this method the process will be asynchronous.
2. This method allocate separate governor limit within the same method.
For more info Refer the below link ,
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_tools_start_stop_test.htm
I have found great and massive information. Thanks for sharing
ReplyDeleteSalesforce CPQ Online Training
CPQ Salesforce Training