Advertisement
Guest User

RetrieveNextUtils

a guest
Nov 3rd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. global with sharing class RetrieveNextUtils {
  2. webService static Id retrieveNextCase(String userId)
  3. {
  4. //Really we're only specifying the user ID for the sake of the test methods
  5. if (userId=='') {
  6. //Use the currently running user
  7. userId = UserInfo.getUserId();
  8. }
  9.  
  10. //First find out which queues this user is a member of
  11. List<Id> listGroupIds = getQueuesForUser(userId);
  12.  
  13. if(listGroupIds.size()>0)
  14. {
  15. //Find an open case that is assigned to one of those queues
  16. Case caseObj = [select c.Id,c.OwnerId from Case c where
  17. c.IsClosed=false
  18. and c.OwnerId in :listGroupIds
  19. limit 1
  20. for update];
  21.  
  22. if (caseObj!=null) {
  23. //If we found one, assign it to the current user
  24. caseObj.OwnerId = userId;
  25. update caseObj;
  26.  
  27. return caseObj.Id;
  28. }
  29. }
  30.  
  31. return null;
  32. }
  33.  
  34. webService static Id retrieveNextLead(String userId)
  35. {
  36. //Really we're only specifying the user ID for the sake of the test methods
  37. if (userId=='') {
  38. //Use the currently running user
  39. userId = UserInfo.getUserId();
  40. }
  41.  
  42. //First find out which queues this user is a member of
  43. List<Id> listGroupIds = getQueuesForUser(userId);
  44.  
  45. if(listGroupIds.size()>0)
  46. {
  47. //Find an open lead that is assigned to one of those queues
  48. List<Lead> leads = [select l.Id,l.OwnerId from Lead l where
  49. l.IsConverted=false
  50. and l.OwnerId in :listGroupIds
  51. limit 1
  52. for update];
  53.  
  54. if (leads.size()>0) {
  55. //If we found one, assign it to the current user
  56. leads[0].OwnerId = userId;
  57. update leads;
  58.  
  59. return leads[0].Id;
  60. }
  61. }
  62.  
  63. return null;
  64. }
  65.  
  66. //Returns a list of ids of queues that this user is a member of
  67. public static List<Id> getQueuesForUser(String userId)
  68. {
  69. List<Id> listGroupIds = new List<Id>();
  70. List<GroupMember> listGroupMembers = [Select g.GroupId From GroupMember g
  71. where g.Group.Type='Queue'
  72. and g.UserOrGroupId=:userId];
  73.  
  74. if (listGroupMembers!=null && listGroupMembers.size()>0) {
  75. for (GroupMember gm:listGroupMembers) {
  76. listGroupIds.add(gm.GroupId);
  77. }
  78. }
  79.  
  80. return listGroupIds;
  81. }
  82. @istest
  83.  
  84. public static Group createTestGroup()
  85. {
  86. Group g = new Group(Type='Queue',Name='testRetrieveNextCase');
  87. insert g;
  88.  
  89. //Make this queue assignable to leads and cases
  90. List<QueueSobject> qs = new List<QueueSobject>();
  91. qs.add(new QueueSobject(QueueId=g.Id,SObjectType='Case'));
  92. qs.add(new QueueSobject(QueueId=g.Id,SObjectType='Lead'));
  93. insert qs;
  94.  
  95. return g;
  96. }
  97.  
  98. public static User createTestUser() {
  99. User user = new User();
  100. user.Username = 'test'+System.currentTimeMillis()+'@RetrieveNextUtils.com';
  101. user.LastName = 'LastTestName';
  102. user.Email = 'test@RetrieveNextUtils.com';
  103. user.alias = 'testAl';
  104. user.TimeZoneSidKey = 'America/New_York';
  105. user.LocaleSidKey = 'en_US';
  106. user.EmailEncodingKey = 'ISO-8859-1';
  107. user.ProfileId = [select id from Profile where Name='System Administrator'].Id;
  108. user.LanguageLocaleKey = 'en_US';
  109. insert user;
  110. //setUser(user);
  111.  
  112. return user;
  113. }
  114.  
  115. public static testMethod void testRetrieveNextLead()
  116. {
  117. User u = createTestUser();
  118.  
  119. Group g = createTestGroup();
  120.  
  121. GroupMember gm = new GroupMember(UserOrGroupId=u.Id,GroupId=g.Id);
  122. insert gm;
  123.  
  124. Test.startTest();
  125.  
  126. //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
  127. System.runAs(u) {
  128. Lead l = new Lead(LastName='Test',OwnerId=g.Id,Company='Test');
  129. insert l;
  130.  
  131. Id leadId = retrieveNextLead(u.Id);
  132. System.assertEquals(leadId,l.Id);
  133.  
  134. Lead ownedLead = [select OwnerId from Lead where Id=:l.Id];
  135. System.assertEquals(ownedLead.OwnerId,u.Id);
  136. }
  137. }
  138.  
  139.  
  140. public static testMethod void testNegativeRetrieveNextLead()
  141. {
  142. User u = createTestUser();
  143.  
  144. Group g = createTestGroup();
  145.  
  146. Test.startTest();
  147.  
  148. //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
  149. System.runAs(u) {
  150. //Do not insert this user in the queue -- he should not get the case
  151.  
  152. Lead l = new Lead(LastName='Test',OwnerId=g.Id,Company='Test');
  153. insert l;
  154.  
  155. Id leadId = retrieveNextLead(u.Id);
  156. System.assertEquals(leadId,null);
  157.  
  158. Lead ownedLead = [select OwnerId from Lead where Id=:l.Id];
  159. System.assertNotEquals(ownedLead.OwnerId,u.Id);
  160. }
  161. }
  162.  
  163. public static testMethod void testRetrieveNextCase()
  164. {
  165. User u = createTestUser();
  166.  
  167. Group g = createTestGroup();
  168.  
  169. GroupMember gm = new GroupMember(UserOrGroupId=u.Id,GroupId=g.Id);
  170. insert gm;
  171.  
  172. Test.startTest();
  173. //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
  174. System.runAs(u) {
  175. Case c = new Case(Subject='Test',OwnerId=g.Id);
  176. insert c;
  177.  
  178. Id caseId = retrieveNextCase(u.Id);
  179. System.assertEquals(caseId,c.Id);
  180.  
  181. Case ownedCase = [select OwnerId from Case where Id=:c.Id];
  182. System.assertEquals(ownedCase.OwnerId,u.Id);
  183. }
  184. }
  185.  
  186.  
  187. public static testMethod void testNegativeRetrieveNextCase()
  188. {
  189. User u = createTestUser();
  190.  
  191. Group g = createTestGroup();
  192.  
  193. Test.startTest();
  194.  
  195. //We have to runAs so that we don't get a MIXED_DML_EXCEPTION
  196. System.runAs(u) {
  197.  
  198. //Do not insert this user in the queue -- he should not get the case
  199. Case c = new Case(Subject='Test',OwnerId=g.Id);
  200. insert c;
  201.  
  202. Id caseId = retrieveNextCase(u.Id);
  203. System.assertEquals(caseId,null);
  204.  
  205. Case ownedCase = [select OwnerId from Case where Id=:c.Id];
  206. System.assertNotEquals(ownedCase.OwnerId,u.Id);
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement