5

I'm trying to create a visualforce page that has a lookup field for users, and then one you find a user, I want there to be a custom button. If the user is frozen, I want the user to be unfrozen, and if the user is not frozen, I want the button to freeze the user. I know there is a isFrozen field on the UserLogin object, but from what I can tell that is just able to show whether or not users are frozen, it doesn't deal at all with the freezing/unfreezing of users. Any ideas on how this could be done? Thanks!

******Update******

When I try to set the value of the freeze and unfreeze, it says that the field 'uLogin.isFrozen' is not writeable. I've added the code I have so far below:

Controller

    public with sharing class FreezeUser {

    User_Freeze__c uf;
    User usr;
    UserLogin uLogin;

    public FreezeUser(){
        uf = new User_Freeze__c();
        usr = new User();
        uLogin = new UserLogin();
    }

    public User getUsr(){
        return usr;
    }

    public User_Freeze__c getUf(){
        return uf;
    }

    public UserLogin getULogin(){
        return uLogin;
    }

    public PageReference save(){
        try{
            System.debug('++++++++++++++++++++++++++++++++++ userFreeze on BEFORE SAVE: ' + uf);
            upsert uf;
            System.debug('++++++++++++++++++++++++++++++++++ userFreeze on AFTER SAVE: ' + uf);
            System.debug('++++++++++++++++++++++++++++++++++ userLogin isFrozen AFTER Save ' + uLogin.IsFrozen);
            System.debug('++++++++++++++++++++++++++++++++++ userLogin UserId: ' + uLogin);
            //System.debug('++++++++++++++++++++++++++++++++++ userLogin UserId: ' + uLogin.id);
        } catch(System.DmlException e){
            ApexPages.addMessages(e);
            return null;
        }

        return null;
    }

    public PageReference freeze(){
        try{
            System.debug('+++++++++++++++++++++++++++++++++ User isFrozen BEFORE Update ' + uLogin.isFrozen);
            if(uLogin.isFrozen == false){
                uLogin.IsFrozen = true;
                update uLogin;
            }
            System.debug('+++++++++++++++++++++++++++++++++++++ User isFrozen AFTER Update' + uLogin.isFrozen);

            return null;

        } catch(System.DMLException e){
            ApexPages.addMessages(e);
            return null;
        }
   }

    public PageReference Unfreeze(){
        try{
            if(uLogin.isFrozen == true){
                uLogin.IsFrozen = false;
                update uLogin;
            }
        } catch(System.DMLException e){
            ApexPages.addMessages(e);
            return null;
        }

        return null;
    }

}

Visualforce

<apex:page controller="FreezeUser">
    <apex:form >
        <apex:messages />

        <!-- Input Section-->
        <apex:pageBlock >
            <apex:pageBlockSection title="Select User">
                <apex:inputField label="User" value="{!uf.Usr__c}"/> 
            </apex:pageBlockSection>

        </apex:pageBlock>

        <!-- Save Button -->
        <apex:commandButton action="{!save}" value="Save" rendered="{!IF(uf.Usr__c == null, true, false)}"/>

        <!-- Output Section -->
        <apex:pageBlock id="output" rendered="{!If(uf.Usr__c != null, true, false)}">
            <apex:pageBlockSection title="Output">
                <apex:outputPanel >
                    <apex:outputLabel value="User: "/>
                    <apex:outputField label="User" value="{!uf.Usr__c}"/>
                    <br/>
                    <apex:outputLabel value="is Frozen: "/>
                    <apex:outputText value="{!uLogin.isFrozen}"/>
                </apex:outputPanel>
            </apex:pageBlockSection>

            <!-- Freeze Button -->
            <apex:commandButton action="{!Freeze}" value="Freeze" rendered="{!If(uLogin.isFrozen == False, true, false)}"/>

            <!-- Unfreeze Button -->
            <apex:commandButton action="{!Unfreeze}" value="Unfreeze" rendered="{!If(uLogin.isFrozen == true, true, false)}"/> 
        </apex:pageBlock>


    </apex:form>

</apex:page>
  • 2
    Hey Stephen, by updating the IsFrozen field to true or false, you are essentially freezing or unfreezing the user, just as much as you can deactivate a user based on the value you set the isActive field. – sw6 - KTBFFH Jun 2 '15 at 12:53
  • It's really that easy? Wow. Ok, thanks. – Stephen Jun 2 '15 at 12:54
  • @sw6-KTBFFHChelseafc, so I tried to update the field, and it said it's not writeable. Any ideas? – Stephen Jun 2 '15 at 20:37
10
+50

It looks like Bob Buzzard has blogged about a way to workaround not being able to set the field via apex, take a look at the full blog post here:

After digging through the Apex Developer’s Guide and experimenting with the execute anonymous element of the developer console it quickly became clear that I couldn’t freeze a user in Apex. Searching the SOAP API Developer’s Guide proved more productive when I came across the UserLogin object and its associated IsFrozen field. While this still mean that I couldn’t use Apex, the SOAP API is accessible via the Ajax Toolkit which I can use from a Visualforce page.

It was then short work to create The Freezer - a Visualforce page to output all usernames present in the system and allow them to be frozen/defrosted at the click of a button. The page is shown below:

  • 1
    Thanks. I saw this before, and was hoping there was another way, but it looks like from the support this post got, that this is the way to go. – Stephen Jun 9 '15 at 12:29
  • I have the button and everything seems to work as an administrator, but when I try to use it as another user it doesn't work, and pops up the "Insufficient Privileges" Error. Any suggestions? – Stephen Jun 24 '15 at 18:45
  • @Stephen Do they have the "Manage Users" privilege? – Jenny B Jun 24 '15 at 18:48
  • We have them as a Delegated Administrator right now. – Stephen Jun 24 '15 at 19:25
  • @Stephen it doesn't look like Delegated Administrators can do that at this point, but there is an idea here success.salesforce.com/ideaView?id=08730000000kuEZAAY – Jenny B Jun 24 '15 at 19:42
0

No trickery involving API callouts is necessary - UserLogin.IsFrozen is absolutely "updateable" from Apex, however, check these things:

  • Your API version supports it. I used Apex API v33.0. Winter 14, when this feature was released may not have
  • You have access edit to the object, record and field in question (I did this as SysAd)
  • If you're building a Visualforce controller, you may need to make the controller "without sharing" or have it call a method in a class "without sharing" should the intended users not have the permission to freeze/thaw users directly
  • The relationship from UserLogin to User doesn't seem to be traversable with SOQL
  • You're not freezing your own (current) user as the test

Here is quick code demo to run via anonymous apex (yes, I ran this, then copy/pasted it, only anonymizing the username):

String uname = 'testuser@sfdcorg.com';
User u = [Select Id, Name FROM User WHERE Username = :uname LIMIT 1];
UserLogin ul = [SELECT Id, UserId, IsFrozen from UserLogin WHERE UserId = :u.Id LIMIT 1];
System.debug(ul);

ul.IsFrozen = true;
update ul;

// Just to be sure I'm reading the stored value
ul = [SELECT Id, UserId, IsFrozen from UserLogin WHERE UserId = :u.Id LIMIT 1];
System.debug(ul);

ul.IsFrozen = false;
update ul;

ul = [SELECT Id, UserId, IsFrozen from UserLogin WHERE UserId = :u.Id LIMIT 1];
System.debug(ul);

results in the following log output:

17:34:46.252 (252891589)|EXECUTION_STARTED
17:34:46.252 (252913297)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
17:34:46.257 (257380661)|SOQL_EXECUTE_BEGIN|[2]|Aggregations:0|SELECT Id, Name FROM User WHERE Username = :tmpVar1 LIMIT 1
17:34:46.277 (277875089)|SOQL_EXECUTE_END|[2]|Rows:1
17:34:46.278 (278713872)|SOQL_EXECUTE_BEGIN|[3]|Aggregations:0|SELECT Id, UserId, IsFrozen FROM UserLogin WHERE UserId = :tmpVar1 LIMIT 1
17:34:46.296 (296880812)|SOQL_EXECUTE_END|[3]|Rows:1
17:34:46.297 (297326250)|USER_DEBUG|[4]|DEBUG|UserLogin:{IsFrozen=false, Id=0Yw80000000eNvbCAE, UserId=00580000003MNpuAAG}
17:34:46.297 (297642255)|DML_BEGIN|[7]|Op:Update|Type:UserLogin|Rows:1
17:34:46.408 (408553415)|DML_END|[7]
17:34:46.408 (408871836)|SOQL_EXECUTE_BEGIN|[10]|Aggregations:0|SELECT Id, UserId, IsFrozen FROM UserLogin WHERE UserId = :tmpVar1 LIMIT 1
17:34:46.440 (440290289)|SOQL_EXECUTE_END|[10]|Rows:1
17:34:46.440 (440577419)|USER_DEBUG|[11]|DEBUG|UserLogin:{IsFrozen=true, Id=0Yw80000000eNvbCAE, UserId=00580000003MNpuAAG}
17:34:46.440 (440838182)|DML_BEGIN|[14]|Op:Update|Type:UserLogin|Rows:1
17:34:46.480 (480095990)|DML_END|[14]
17:34:46.480 (480462945)|SOQL_EXECUTE_BEGIN|[16]|Aggregations:0|SELECT Id, UserId, IsFrozen FROM UserLogin WHERE UserId = :tmpVar1 LIMIT 1
17:34:46.493 (493842120)|SOQL_EXECUTE_END|[16]|Rows:1
17:34:46.494 (494130212)|USER_DEBUG|[17]|DEBUG|UserLogin:{IsFrozen=false, Id=0Yw80000000eNvbCAE, UserId=00580000003MNpuAAG}
17:34:46.495 (495471034)|CODE_UNIT_FINISHED|execute_anonymous_apex
17:34:46.497 (497230588)|EXECUTION_FINISHED
-1

the isFrozen field can be updated. I hope the code below could be of help to you:

public with sharing class FreezeUser {
    user u {get;set;}
    public userlogin ul;
    ul = [SELECT Id, UserId,IsFrozen FROM UserLogin WHERE userId =:u.Id][0]; //assuming the user id has been set from the visualforce as you described
    system.debug('ul is: '+ul);
    public PageReference freeze(){      
        if(ul.isFrozen == false){
            ul.isFrozen = true;
            update ul;
        }
        system.debug('ul is: '+ul);
        return null;
    }
}
  • That didn't seem to work. Any other ideas? – Stephen Jun 4 '15 at 17:29
  • What part of the code didn't work? What errors did you get? What was the result of the "system.debug('ul is: '+ul);" statement? – sw6 - KTBFFH Jun 4 '15 at 18:17
  • @sw6-KTBFFHChelseafc there's a bug in your code. if(ul.isFrozen = false){ should use a double equals for comparison, this is an assignment statement. The code within the scope brackets never gets executed as written. – Mark Pond Jun 4 '15 at 19:55
  • @MarkPond - thanks for catching that - missed that one when typing my answer in. Will make the edit to my answer. – sw6 - KTBFFH Jun 4 '15 at 19:59
  • @sw6-KTBFFHChelseafc when I use your code I get "unexpected token: '='" on line 4 where you set the value of userLogin ul. Not sure why I'm getting that. – Stephen Jun 5 '15 at 13:41

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.