You need to sign in to do that
Don't have an account?
Tom Blamire 8
Code Help!!
Hi,
I'm new to all this and need some urgent help. I found a trigger that i modified in our sandbox which works fine. The problem i am having is i do not know how to create a test so i can push it to production. Can you help?
The code is:
I'm new to all this and need some urgent help. I found a trigger that i modified in our sandbox which works fine. The problem i am having is i do not know how to create a test so i can push it to production. Can you help?
The code is:
trigger Guest_Relations_CaseTeamTrigger on Case (after insert) { Map<Id, CaseTeamMember> membersToAdd = new Map<Id, CaseTeamMember>(); List<Case> cases = [Select Id,OwnerId,RecordTypeId,RecordType.Name from Case where id IN :Trigger.newMap.keySet()]; for (Case c : cases) { if (c.RecordType.Name == 'Geneva Guest Relations' || c.RecordType.Name == 'Guest Relations') { membersToAdd.put(c.Id, new CaseTeamMember( ParentId = c.Id, MemberId = UserInfo.getUserId() ) ); System.debug('*******************************************'); System.debug('User = '+UserInfo.getName()); System.debug('*******************************************'); } } if (!membersToAdd.isEmpty()) { try { CaseTeamRole caseTeamRole = [SELECT Id FROM CaseTeamRole WHERE Name = 'Creator' LIMIT 1]; for (CaseTeamMember ctm : membersToAdd.values()) { ctm.TeamRoleId = caseTeamRole.Id; } for (CaseTeamMember ctm : [SELECT Id, MemberId, ParentId FROM CaseTeamMember WHERE ParentId IN :membersToAdd.keySet() AND MemberId = :UserInfo.getUserId() ORDER BY ParentId]) { System.debug('*******************************************'); System.debug('User = '+UserInfo.getName()); System.debug('*******************************************'); if (membersToAdd.containsKey(ctm.ParentId)) { membersToAdd.remove(ctm.ParentId); } } if (!membersToAdd.isEmpty()) { insert membersToAdd.values(); } } catch (System.QueryException qe) {} } }
Each test should follow the following structure:
- Setup of test data. This includes creation of any data needed by your class. Account, Contacts etc
- Starting the test. This is calling Test.startTest() to reset the governor limits
- Calling your class / method
- Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
- Asserting that your changes have worked
- If you have inserted/updated/deleted data, you need to query for the updates
- Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/