Wednesday, November 16, 2016

Inline VisualForce Page to Freeze/UnFreeze the User without Having Manage Users permission to Profile.


Page:


<apex:page standardController="user" extensions="Freezer" id="pg">
    <apex:form id="frm">
        <apex:pageBlock >
      <!--     <span style="font-size:16pt;font-style:bold;color:red;"> Press The Button to Freeze/Unfreeze The User </span> -->
           <apex:commandButton style="width:150px;height:50px;background:green;color:white;font-size:14pt;font-style:bold" action="{!Freezermethod}" value="{!btnname}" oncomplete="RefreshPrimaryTab('{!user.id}')"/> 
        </apex:pageBlock>
    </apex:form>
    <script type="text/javascript">

        function RefreshPrimaryTab(id) 
        {
                  window.top.location.href = '/'+id+'?noredirect=1&isUserEntityOverride=1';
        }

   </script>
</apex:page>

Controller:


public class Freezer {
    public string btnname {get;set;}
    set<id> ids=new set<id>();
    user urs = [SELECT Id,Name FROM User WHERE      id=:apexpages.currentpage().getparameters().get('id')];
    
    public Freezer(ApexPages.StandardController Controller) {
        
        UserLogin users = [SELECT IsFrozen,UserId FROM UserLogin WHERE UserId=:urs.id];
            if(users.isFrozen)
                btnname='Unfreeze';
            else
                btnname='Freeze';
        }
    
    public void Freezermethod(){   

      UserLogin users = [SELECT IsFrozen,UserId FROM UserLogin WHERE UserId=:urs.id];
             if(users.isFrozen) 
                users.isFrozen= false; 
            else
                users.isFrozen= true; 

            update(users); 
        }
        
    }

Note:This Page will work only After It is added to the User Page layout Other wise it will display QueryException.

No comments:

Post a Comment