You need to sign in to do that
Don't have an account?
MikeGill
Code Coverage Results
Hi All,
I have two triggers which are working fine, however the code coverage results are showing 64% only. Not sure what more I should be testing in order to achieve 100%.
Any tips/pointers very much welcome
Trigger 1
trigger CreateContractHeader on Order__c (after update) { //Get Order object values for(Order__c ord : Trigger.new){ Boolean bTerms = ord.Accepted_Terms__c; Boolean bPayment = ord.Payment_Terms_Agreed__c; List<Contract> ordId = [select Id from Contract where Order__c=:ord.Id]; // Create contract if not existing if (ordId.size() == 0){ // Check terms have been agreed if (bTerms==true && bPayment == true){ Contract newContract = new Contract(); newContract.AccountId = ord.Account__c; newContract.Order__c = ord.Id; Date startdate = Date.today(); newContract.StartDate = startdate; newContract.ContractTerm = 12; insert newContract; } } } }
Trigger 2
trigger CreateContractCredits on Contract (after insert) { for (Contract cont : Trigger.new){ // Fetch order credits Order__c orderCredits = [select Total_Order_Credits__c from Order__c where id=:cont.Order__c]; // Convert credits to int double d = orderCredits.Total_Order_Credits__c; integer x; x = d.intValue(); // Create credits for total number of credits for (Integer i = 0; i < x; i++){ Credit__c credit = new Credit__c(); credit.Contract__c = cont.Id; credit.Account__c = cont.AccountId; insert credit; } } }
Here is my test class
@isTest private class testContract { static testMethod void myUnitTest() { // Insert Test Product Product2 prod = new Product2(name ='WR'); insert prod; // Insert Test Account Account acc = new Account(name='Mike'); insert acc; // Insert Test Order Order__c ord = new Order__c(); ord.Account__c = acc.Id; insert ord; // Insert Single Order Line OrderItems__c ordlines = new OrderItems__c(); ordlines.Order__c = ord.Id; ordlines.Product__c = prod.Id; insert ordlines; // Update Order ord.Payment_Terms_Agreed__c = true; ord.Accepted_Terms__c = true; update ord; // Insert Contract Contract contract = new Contract(); contract.AccountId = acc.Id; contract.Order__c = ord.Id; insert contract; // Insert Credits Credit__c credits = new Credit__c(); credits.Account__c = acc.Id; credits.Contract__c = contract.Id; insert credits; } }
Excellent, good point. Still learning the API's.
Thx
All Answers
Figured it out, missing one custom field which needed setting at product level.
Still interested in any advice on improvements in the testing class.
Thanks,
Mike
May I suggest you to rewrite your triggers to make them bulk.
What if you have to insert more than 20 objects in your trigger.
insert newContract should be replaced by listContracts.add(newcontract);
and the insert should be done outside the loop : insert listContracts;
Excellent, good point. Still learning the API's.
Thx