0

I've searching options to set my users isFrozen = true, which I found here, but after trying it I got a weird error:

List<UserLogin> users = [SELECT Id,IsFrozen,UserId FROM UserLogin WHERE UserId IN
(SELECT Id FROM User WHERE Profile.Name != 'System Administrator' AND IsActive = true)
AND UserId != :UserInfo.getUserId()];

users[0].IsFrozen = true;

update users;

I don't get why, but its giving me:

System.DmlException: Update failed. First exception on row 0 with id 0Yw29000001XZqMCAW; first error: UNKNOWN_EXCEPTION, You cannot edit this field: []

Am I mistaken in something related to the code?

1

I realized, doing some debugging that when it was going through all the Users, it was breaking on a user with a different license type. So limiting to Salesforce type of license made it work.

So this piece of code, works perfectly:

public static void freezeUsers(){

    List<UserLogin> users = [SELECT Id,IsFrozen,UserId FROM UserLogin WHERE UserId IN
                        (SELECT Id FROM User WHERE Profile.Name != 'System Administrator'
                        AND IsActive = true AND Profile.UserLicense.Name = 'Salesforce')
                        AND UserId != :UserInfo.getUserId()];

    if(users != null && !users.isEmpty()){
        for (UserLogin ul : users) {
            System.debug('User Login: ' + ul);
            ul.IsFrozen = true;
        }
        try {
            update(users);
        } catch (DmlException ex) {
            System.debug('Exception: ' + ex);
        }
    }
}

The change was specifically in the query: Profile.UserLicense.Name = 'Salesforce'.

| improve this answer | |
  • You can update userlogin of other UserLicense too. – sanket kumar Oct 5 '18 at 4:52
0

UserLogin

Represents the settings that affect a user’s ability to log into an organization. To access this object, you need the UserPermissions.ManageUsers permission. This object is available in API version 29.0 and later.

Everything else looks fine in your code and even it worked, maybe you have not the appropriate access as mentioned above. Make sure you have UserPermissions.ManageUsers permission.

Note:- You can update userlogin without keeping any concern on license.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.