Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. public class AssignPackageLicense {
  2. public static boolean flag=true;
  3. static String PACKAGE_NAMESPACE_PREFIX = System.Label.PACKAGE_NAMESPACE_PREFIX;
  4. public static String exceptionText {get; set;}
  5.  
  6. public AssignPackageLicense() {
  7. exceptionText = 'Initialized';
  8. }
  9.  
  10. static List<User> getUsersWithProfile(list<user> usr) {
  11. List<User> matchingUsers = new List<User>();
  12. list <id>userIds=new list<id>();
  13. for(user u:usr) {
  14. userIds.add(u.id);
  15. }
  16. matchingUsers = [SELECT Id FROM User where id in:userIds ];
  17. return matchingUsers;
  18. }
  19.  
  20. public static void assignLicenseByProfile(List<user> usr) {
  21. //find the PackageLicense Id
  22. List<PackageLicense> pl = new list<PackageLicense>();
  23. pl=[SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses,
  24. ExpirationDate,Status FROM PackageLicense WHERE
  25. NamespacePrefix = :PACKAGE_NAMESPACE_PREFIX];
  26. System.debug('pl>>>'+pl);
  27. if(pl.size()>0){
  28. System.assert(pl != null, 'PackageLicense cannot be null.');
  29. List<User> usersToAssignLicenses = getUsersWithProfile(usr);
  30. List<UserPackageLicense> firstUPLs = new List<UserPackageLicense>();
  31.  
  32. //create a new UserPackageLicense record for each user with the specified profile
  33. for (Integer i = 0; i< usersToAssignLicenses.size(); i++){
  34. UserPackageLicense upl = new UserPackageLicense();
  35. upl.PackageLicenseId = pl[0].Id;
  36. upl.UserId = usersToAssignLicenses[i].Id;
  37. firstUPLs.add(upl);
  38. }
  39. System.debug('firstUPLs>>>'+firstUPLs);
  40. try {
  41. //bulk insert
  42. insert(firstUPLs);
  43. } catch(DmlException e) {
  44. for (Integer i = 0; i < e.getNumDml(); i++) {
  45. // process exception here
  46. System.debug(e.getDmlMessage(i));
  47. String status = e.getDmlStatusCode(i);
  48. System.debug(status + ' ' + e.getDmlMessage(i));
  49. if(status.equals('LICENSE_LIMIT_EXCEEDED')){
  50. exceptionText = 'You tried to assign more licenses than available. ' +' You tried to create '+ firstUPLs.size()+' licenses but only have '+ (pl[0].AllowedLicenses - pl[0].UsedLicenses) + ' licenses free.';
  51. }
  52. }
  53. }
  54. }
  55. }
  56.  
  57. @istest
  58.  
  59. Profile profileId = [SELECT Id,Name FROM Profile WHERE Name = :profileName LIMIT 1];
  60.  
  61. User testUser = new User(LastName = 'User',
  62. FirstName='Test',
  63. Alias = 'Utest',
  64. Email = 'user.test@TestTelstra.com',
  65. Username = userName,
  66. ProfileId = profileId.id,
  67. TimeZoneSidKey = 'GMT',
  68. LanguageLocaleKey = 'en_US',
  69. EmailEncodingKey = 'UTF-8',
  70. LocaleSidKey = 'en_US'
  71. );
  72. insert testUser;
  73. System.debug('testUser>>>>'+testUser);
  74. }
  75. static testMethod void userTest(){
  76. List<user> uList=new List<user>([select id,username from user where username='testuserLicense@testOrg.com' limit 1]);
  77.  
  78. // AssignPackageLicense.assignLicenseByProfile(uList);
  79. //system.debug('u>>>>'+u);
  80. system.assertEquals('testuserlicense@testorg.com',uList[0].username);
  81. List<UserPackageLicense>u=new list<UserPackageLicense>([select userid from UserPackageLicense]);
  82.  
  83. }
  84. static testMethod void userTestcat(){
  85. try{
  86. List<user> uList=new List<user>([select id,username from user where username='testuserL3icense@testOrg.com' limit 1]);
  87. AssignPackageLicense.assignLicenseByProfile(uList);
  88. }
  89. catch(DMLException e) {
  90. //system.assertEquals(e.getMessage().contains('assign more'));
  91. }
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement