Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AssignPackageLicense {
- public static boolean flag=true;
- static String PACKAGE_NAMESPACE_PREFIX = System.Label.PACKAGE_NAMESPACE_PREFIX;
- public static String exceptionText {get; set;}
- public AssignPackageLicense() {
- exceptionText = 'Initialized';
- }
- static List<User> getUsersWithProfile(list<user> usr) {
- List<User> matchingUsers = new List<User>();
- list <id>userIds=new list<id>();
- for(user u:usr) {
- userIds.add(u.id);
- }
- matchingUsers = [SELECT Id FROM User where id in:userIds ];
- return matchingUsers;
- }
- public static void assignLicenseByProfile(List<user> usr) {
- //find the PackageLicense Id
- List<PackageLicense> pl = new list<PackageLicense>();
- pl=[SELECT Id, NamespacePrefix, AllowedLicenses, UsedLicenses,
- ExpirationDate,Status FROM PackageLicense WHERE
- NamespacePrefix = :PACKAGE_NAMESPACE_PREFIX];
- System.debug('pl>>>'+pl);
- if(pl.size()>0){
- System.assert(pl != null, 'PackageLicense cannot be null.');
- List<User> usersToAssignLicenses = getUsersWithProfile(usr);
- List<UserPackageLicense> firstUPLs = new List<UserPackageLicense>();
- //create a new UserPackageLicense record for each user with the specified profile
- for (Integer i = 0; i< usersToAssignLicenses.size(); i++){
- UserPackageLicense upl = new UserPackageLicense();
- upl.PackageLicenseId = pl[0].Id;
- upl.UserId = usersToAssignLicenses[i].Id;
- firstUPLs.add(upl);
- }
- System.debug('firstUPLs>>>'+firstUPLs);
- try {
- //bulk insert
- insert(firstUPLs);
- } catch(DmlException e) {
- for (Integer i = 0; i < e.getNumDml(); i++) {
- // process exception here
- System.debug(e.getDmlMessage(i));
- String status = e.getDmlStatusCode(i);
- System.debug(status + ' ' + e.getDmlMessage(i));
- if(status.equals('LICENSE_LIMIT_EXCEEDED')){
- 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.';
- }
- }
- }
- }
- }
- @istest
- Profile profileId = [SELECT Id,Name FROM Profile WHERE Name = :profileName LIMIT 1];
- User testUser = new User(LastName = 'User',
- FirstName='Test',
- Alias = 'Utest',
- Email = 'user.test@TestTelstra.com',
- Username = userName,
- ProfileId = profileId.id,
- TimeZoneSidKey = 'GMT',
- LanguageLocaleKey = 'en_US',
- EmailEncodingKey = 'UTF-8',
- LocaleSidKey = 'en_US'
- );
- insert testUser;
- System.debug('testUser>>>>'+testUser);
- }
- static testMethod void userTest(){
- List<user> uList=new List<user>([select id,username from user where username='testuserLicense@testOrg.com' limit 1]);
- // AssignPackageLicense.assignLicenseByProfile(uList);
- //system.debug('u>>>>'+u);
- system.assertEquals('testuserlicense@testorg.com',uList[0].username);
- List<UserPackageLicense>u=new list<UserPackageLicense>([select userid from UserPackageLicense]);
- }
- static testMethod void userTestcat(){
- try{
- List<user> uList=new List<user>([select id,username from user where username='testuserL3icense@testOrg.com' limit 1]);
- AssignPackageLicense.assignLicenseByProfile(uList);
- }
- catch(DMLException e) {
- //system.assertEquals(e.getMessage().contains('assign more'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement