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>