To start, I have already read Bob Buzzard's article, http://bobbuzzard.blogspot.com/2013/11/freezing-users-from-visualforce.html, and incorporated a lot of what he's used. At this point, I am now trying to create a button on a specific user detail that I could click to freeze/unfreeze a user (as if the user was a System Administrator, but isn't) We want to give selected access to this capability to certain users. Because then after the special user freezes another user, we want them to click a custom button also on the user detail page to pull up a report to see what sales desk they are on, so that we can reassign those responsibilities before we deactivate the user completely. What I currently have involves pulling up a visualforce page to select the user. The difficulty is that we have to work with the UserLogin object, which isn't accessible through apex. I've attached the code I have at the moment, which works, but is a visualforce page instead of being accessed directly from the User detail page. Thanks in advance!
Update Looks like you can't create custom buttons for the User object per https://help.salesforce.com/HTViewHelpDoc?id=links_considerations.htm&language=en_US
Controller
public with sharing class FreezeUser {
public User_Freeze__c uf;
public FreezeUser(){
uf = new User_Freeze__c();
}
public User_Freeze__c getUf(){
return uf;
}
public PageReference save(){
try{
upsert uf;
} catch(System.DmlException e){
ApexPages.addMessages(e);
return null;
}
return null;
}
public PageReference salesDeskMemberReport(){
PageReference salesDeskMemberReport = new PageReference('/');
salesDeskMemberReport.setRedirect(true);
return salesDeskMemberReport;
}
}
Visualforce
<apex:page controller="FreezeUser">
<script type="text/javascript">
var __sfdcSessionId = '{!GETSESSIONID()}';
</script>
<script src="../../soap/ajax/29.0/connection.js"
type="text/javascript"></script>
<script type="text/javascript">
function getUserInfo(){
var userInfoById = {};
//var result = sforce.connection.query("Select Id, UserId, IsFrozen, IsPasswordLocked From UserLogin WHERE UserId = :ApexPages.currentPage().getParameters().get('userId')");
var result = sforce.connection.query("Select Id, UserId, IsFrozen, IsPasswordLocked From UserLogin order by UserId");
var it = new sforce.QueryResultIterator(result);
while(it.hasNext()){
var record = it.next();
userInfoById[record.UserId] = record;
}
var output='<table><tr><th>User</th><th>Action</th></tr>';
var selectedUser = "'{!uf.usr__c}'";
result = sforce.connection.query("SELECT Id, FirstName, LastName, isActive from User where Id = " + selectedUser);
it = new sforce.QueryResultIterator(result);
while(it.hasNext()){
var record = it.next();
if (record.Id in userInfoById){
var userInfo=userInfoById[record.Id];
var name=record.FirstName + ' ' + record.LastName;
output+='<tr><td>' + name + '</td><td>';
if (userInfo.IsFrozen=='true'){
output+="<td><button onclick=\"unfreeze('" + userInfo.Id + "', '" + name + "', false);\">Unfreeze</button>";
} else {
output+="<button onclick=\"freeze('" + userInfo.Id + "', '" + name + "', true);\">Freeze</button>";
}
output+='</td></tr>';
}
}
output+='</table>';
document.getElementById('output').innerHTML=output;
}
function freeze(id, name, freezerState){
alert("Freezing " + name);
var userlogin = new sforce.SObject("UserLogin");
userlogin.Id = id;
userlogin.IsFrozen = freezerState;
var result = sforce.connection.update([userlogin]);
if (result[0].getBoolean("success")) {
console.log(name + " " + (freezerState?'frozen':'unfrozen'));
} else {
console.log("failed to freeze " + name + " " + result[0]);
}
window.location.reload();
}
function unfreeze(id, name, freezerState){
alert("Unfreezing " + name);
var userlogin = new sforce.SObject("UserLogin");
userlogin.Id = id;
userlogin.IsFrozen = freezerState;
var result = sforce.connection.update([userlogin]);
if (result[0].getBoolean("success")) {
console.log(name + " " + (freezerState?'unfrozen':'frozen'));
} else {
console.log("failed to unfreeze " + name + " " + result[0]);
}
window.location.reload();
}
</script>
<apex:pageMessages/>
<apex:form>
<apex:pageBlock rendered="{!IF(uf.Usr__c == null, true, false)}">
<apex:pageBlockSection title="Select User">
<apex:inputField value="{!uf.Usr__c}"/>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton action="{!Save}" value="Save" rendered="{!IF(uf.Usr__c == null, true, false)}"/>
<apex:pageBlock title="Freeze/Unfreeze User" rendered="{!IF(uf.Usr__c != null, true, false)}">
<apex:pageBlockSection>
<!-- <apex:outputField value="{!uf.Usr__c}"/> -->
</apex:pageBlockSection>
<apex:pageBlockSection>
<apex:pageBlockSectionItem>
<div id="output"></div>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:commandButton action="{!salesDeskMemberReport}" value="Reassign User Sales Desks" rendered="{!IF(uf.Usr__c != null, true, false)}"/>
</apex:form>
<script>
getUserInfo();
</script>
</apex:page>