You need to sign in to do that
Don't have an account?
Mike Rethage
Apex Test Case "For" Statement
Hello,
I have written the following code and have done extensive sandbox testing. I have several triggers that are very similar but go off of different fields in a contract. I have a test written for lines 1-12 but can't seem to find the best methodology to writing from line 14 down. Any help is appreciated.
Code:
Test:
I have written the following code and have done extensive sandbox testing. I have several triggers that are very similar but go off of different fields in a contract. I have a test written for lines 1-12 but can't seem to find the best methodology to writing from line 14 down. Any help is appreciated.
Code:
trigger Contract_CreateTV_New on Contract (after update){ System.debug('Contract_CreateTV_New triggered'); Set<Id> activatedContractIds = new Set<Id>(); for (Contract each : trigger.new) { if (each.Status == 'In Progress' && each.RecordTypeId == [Select Id From RecordType WHERE Name = 'Touchtown Contract'].Id && trigger.oldMap.get(each.Id).Status != 'In Progress') { System.debug('Contract ' + each.Id + ' transitioned into IN PROGRESS status.'); activatedContractIds.add(each.Id); if (each.TV_Systems__c != null) { for (Integer i = 0; i < each.TV_Systems__c; i++) { Equipment__c newEQPT = new Equipment__c ( Account__c = each.AccountId, Name = [Select Name From Account WHERE Id =: each.AccountId].Name, RecordTypeId = [Select Id From RecordType WHERE Name = 'TV+'].Id, Contract__c = each.Id, Product_Type__c = 'TV+', Warranty__c = 'Original (Lifestyle)' ); insert newEQPT; List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact' and ContractId in :activatedContractIds]; for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId); Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c( Equipment__c = newEQPT.Id, Contact__c = eachRole.ContactId ); insert newJunct; System.debug('Added DS: ' + newEQPT.Id); } } } } } }
Test:
@isTest(seealldata=true) private class TEST_EquipmentCreation { static testmethod void TEST_EquipmentCreation() { Account Acct = new Account(); acct.Name = 'Test Account'; insert Acct; Contact Contact = new Contact(); Contact.FirstName = 'Mike'; Contact.LastName = 'Test'; Contact.AccountId = acct.Id; Contact.Email = 'test@test.com'; insert Contact; Contract newContract = new Contract(); newcontract.Name = 'test contract'; newcontract.AccountId = acct.Id; newcontract.Status = 'draft'; newcontract.StartDate = system.today(); newcontract.RecordTypeId = [Select Id From RecordType WHERE Name = 'Touchtown Contract'].id; insert newcontract; ContractContactRole CCR = new contractContactRole(); CCR.ContractId = newcontract.Id; CCR.ContactId = contact.Id; CCR.Role = 'Install Contact'; insert CCR; newcontract = [SELECT Status FROM Contract WHERE ID=: newcontract.Id]; newcontract.status = 'In Progress'; newcontract.ActivatedDate = system.today(); update newcontract; newcontract.TV_Systems__c=1; update newcontract; newcontract.DS_Systems__c=1; update newcontract; newcontract.Calendars2__c=1; update newcontract; newcontract.TTRA_Licenses2__c=500; newcontract.Resident_Apps_Level__c = 'Premium'; newcontract.Web_Shows__c =1; newcontract.TTRA_Licenses2__c =500; update newcontract; for (Integer i = 0; i<newcontract.DS_Systems__c; i++) { Equipment__c newEQPT = new Equipment__c ( Account__c = acct.id, Name = acct.Name, RecordTypeId = [Select Id From RecordType WHERE Name = 'Digital Signs'].Id, Contract__c = newcontract.Id, Product_Type__c = 'Digital Sign', Warranty__c = 'Original (Lifestyle)' ); insert newEQPT; //List<ContractContactRole> contactRoles = [select ContractId, ContactId from ContractContactRole where Role = 'Install Contact']; //for (ContractContactRole eachRole : contactRoles) {Contract eachContract = trigger.newMap.get(eachRole.ContractId); //Junction_Equipment_Contact__c newJunct = new Junction_Equipment_Contact__c( //Equipment__c = newEQPT.Id, //Contact__c = eachRole.ContactId //); //insert newJunct; // System.debug('Added DS: ' + newEQPT.Id); } //} } }
As for testing your Equpiment__c insert I would do the following:
I would also create a test where you do batch testing and update multiple contracts at the same time to verify that it works correctly. You'll also want to add tests after the Equipment checks to make sure your junction object was also created as expected. I would suggest reading over this [1] page to learn more about testing strategies.
[1] http://blog.deadlypenguin.com/blog/testing/strategies/
I've been sorting through your code and trying to understand it. I'm getting an error when trying to run it:
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Contract_CreateTV_New_r1 caused an unexpected exception, contact your administrator: Contract_CreateTV_New_r1: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: RecordType.Name: Trigger.Contract_CreateTV_New_r1: line 13, column 1".
I've tried a number of modifications but after seeing similar cases, I'm not sure I understand why the error. I've only adjusted the code slightly in a way that should not matter.
If I understand the error, is it because we are selecting the ID and then putting the name? do we need to select the name above in order to enter it?
Sorry, but now I'm getting:
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Contract_CreateTV_New_r1 caused an unexpected exception, contact your administrator: Contract_CreateTV_New_r1: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Contract_CreateTV_New_r1: line 23, column 1".
but the logic seems correct....
And if the 'TV+' Record Type does not exist you will get problems on line 44 with the same error.