Show List of Subordinate User In Apex: In Role hierarchy
Requirement: To Update the Lead Owner field with his sub-ordinate users
Solution : Create a custom Button on Lead Detail Page and Call a visualforce page
Page Code:
<apex:page standardController="Lead" extensions="AssigneLeadController" sidebar="false">
<apex:form id="frmid">
<apex:pageMessages id="msgid"></apex:pageMessages>
<apex:pageBlock rendered="{!showPanel}">
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
Sub-Ordinate User:
<apex:selectList multiselect="false" size="1" value="{!UserId}" id="UserVal">
<apex:selectOptions value="{!options}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Self Assign:
<apex:inputCheckbox value="{!selfAssign}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!SaveRecord}"/>
<apex:commandButton value="Cancel" action="{!ReturnToPage}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------------------Class Code--------------------------
public class AssigneLeadController {
public list<SelectOption> options {get;set;}
public Lead objLead{get;set;}
public boolean selfAssign{get;set;}
public String LeadId;
public String ParentLeadId;
public String AccountId;
public string UserId{get;set;}
public boolean showPanel{get;set;}
public boolean showButtonPanel{get;set;}
public id currentuserId;
public AssigneLeadController(ApexPages.StandardController controller) {
options = new list<SelectOption>();
objLead= (Lead)controller.getRecord();
LeadId = objLead.id;
currentuserId= userinfo.getUserId();
UpdateLeadOwner();
showPanel =true;
showButtonPanel =false;
objLead =[select Lead__c,Converted_Account__c from Lead where ID =:LeadId ];
}
public void UpdateLeadOwner()
{
List<Lead> listDayPlan = new List<lead>();
set<Id> userRoleID = new Set<ID>();
List<User> listUser = [select id,
Name,
ManagerId,
UserRoleId,
isActive
from User
where id=:userinfo.getUserId()
and isActive=true limit 1];
system.debug('listUser-----'+listUser);
for(User uobj:listUser)
{
if(uobj.UserRoleId!=null)
{
userRoleID.add(uobj.UserRoleId);
}
}
List<UserRole> lstofuserRole = new List<UserRole>();
set<Id> setofChildID = new Set<ID>();
lstofuserRole =[ Select u.ParentRoleId, u.Name, u.Id From UserRole u where ParentRoleId IN:userRoleID];
if(lstofuserRole!=null && lstofuserRole.size()>0)
{
for(UserRole uroleObj:lstofuserRole )
{
setofChildID.add(uroleObj.ID);
}
}
List<user> listofChildUser = new List<User>();
listofChildUser =[select id, Name,ManagerId,UserRoleId, isActive from User where UserRoleId IN:setofChildID];
options.add(new SelectOption('','--Select--'));
if(listofChildUser!=null && listofChildUser.size()>0)
{
for(User objuser: listofChildUser)
{
options.add(new SelectOption(objuser.id,objuser.name));
}
}
}
public pagereference SaveRecord()
{
PageReference pageRef;
if(objLead.id!=null)
{
if(UserId==null && selfAssign==false)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select User or Self Assign!!'));
return null;
}
if(objLead.Lead__c!=null ||objLead.Converted_Account__c!=null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You can not change owner either Parent lead or Converted Account exists !!'));
return null;
}
if(UserId!=null && selfAssign==true)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Either Choose Self Assign or User List value !!'));
return null;
}
if(selfAssign==true && UserId==null)
{
objLead.ownerId = currentuserId;
update objLead;
pageRef = new PageReference('/'+objLead.id);
}
else
{
objLead.ownerId = UserId;
update objLead;
pageRef = new PageReference('/'+objLead.id);
}
}
return pageRef;
}
public pagereference BackToRecord()
{
PageReference pageRef =new PageReference('/'+objLead.id);
return pageRef;
}
public pagereference ReturnToPage()
{
PageReference pageRef =new PageReference('/'+objLead.id);
return pageRef;
}
}
Solution : Create a custom Button on Lead Detail Page and Call a visualforce page
Page Code:
<apex:page standardController="Lead" extensions="AssigneLeadController" sidebar="false">
<apex:form id="frmid">
<apex:pageMessages id="msgid"></apex:pageMessages>
<apex:pageBlock rendered="{!showPanel}">
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
Sub-Ordinate User:
<apex:selectList multiselect="false" size="1" value="{!UserId}" id="UserVal">
<apex:selectOptions value="{!options}"/>
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
Self Assign:
<apex:inputCheckbox value="{!selfAssign}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!SaveRecord}"/>
<apex:commandButton value="Cancel" action="{!ReturnToPage}" />
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
----------------------------------------Class Code--------------------------
public class AssigneLeadController {
public list<SelectOption> options {get;set;}
public Lead objLead{get;set;}
public boolean selfAssign{get;set;}
public String LeadId;
public String ParentLeadId;
public String AccountId;
public string UserId{get;set;}
public boolean showPanel{get;set;}
public boolean showButtonPanel{get;set;}
public id currentuserId;
public AssigneLeadController(ApexPages.StandardController controller) {
options = new list<SelectOption>();
objLead= (Lead)controller.getRecord();
LeadId = objLead.id;
currentuserId= userinfo.getUserId();
UpdateLeadOwner();
showPanel =true;
showButtonPanel =false;
objLead =[select Lead__c,Converted_Account__c from Lead where ID =:LeadId ];
}
public void UpdateLeadOwner()
{
List<Lead> listDayPlan = new List<lead>();
set<Id> userRoleID = new Set<ID>();
List<User> listUser = [select id,
Name,
ManagerId,
UserRoleId,
isActive
from User
where id=:userinfo.getUserId()
and isActive=true limit 1];
system.debug('listUser-----'+listUser);
for(User uobj:listUser)
{
if(uobj.UserRoleId!=null)
{
userRoleID.add(uobj.UserRoleId);
}
}
List<UserRole> lstofuserRole = new List<UserRole>();
set<Id> setofChildID = new Set<ID>();
lstofuserRole =[ Select u.ParentRoleId, u.Name, u.Id From UserRole u where ParentRoleId IN:userRoleID];
if(lstofuserRole!=null && lstofuserRole.size()>0)
{
for(UserRole uroleObj:lstofuserRole )
{
setofChildID.add(uroleObj.ID);
}
}
List<user> listofChildUser = new List<User>();
listofChildUser =[select id, Name,ManagerId,UserRoleId, isActive from User where UserRoleId IN:setofChildID];
options.add(new SelectOption('','--Select--'));
if(listofChildUser!=null && listofChildUser.size()>0)
{
for(User objuser: listofChildUser)
{
options.add(new SelectOption(objuser.id,objuser.name));
}
}
}
public pagereference SaveRecord()
{
PageReference pageRef;
if(objLead.id!=null)
{
if(UserId==null && selfAssign==false)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select User or Self Assign!!'));
return null;
}
if(objLead.Lead__c!=null ||objLead.Converted_Account__c!=null)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'You can not change owner either Parent lead or Converted Account exists !!'));
return null;
}
if(UserId!=null && selfAssign==true)
{
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Either Choose Self Assign or User List value !!'));
return null;
}
if(selfAssign==true && UserId==null)
{
objLead.ownerId = currentuserId;
update objLead;
pageRef = new PageReference('/'+objLead.id);
}
else
{
objLead.ownerId = UserId;
update objLead;
pageRef = new PageReference('/'+objLead.id);
}
}
return pageRef;
}
public pagereference BackToRecord()
{
PageReference pageRef =new PageReference('/'+objLead.id);
return pageRef;
}
public pagereference ReturnToPage()
{
PageReference pageRef =new PageReference('/'+objLead.id);
return pageRef;
}
}
Comments
Post a Comment