<apex:page standardController="Leave_Plan__c" extensions="_Leaveplan_CTE" action="{!approvalNeeded}">
<center>
<apex:pageMessages ></apex:pageMessages>
<apex:outputPanel rendered="{!IF(Leave_Plan__c.Status__c == 'Approved',true,false)}">
<font color="green" size="2"><b>
<apex:image id="theImage" value="{!$Resource.Smily}" width="15" height="15"/>Your request has been approved.</b></font>
</apex:outputPanel><br/><br/>
<apex:outputPanel rendered="{!IF(Leave_Plan__c.Status__c == 'Rejected',true,false)}">
<font color="red" size="2"><b>
<apex:image id="theImage1" value="{!$Resource.Sad}" width="15" height="15"/>Your request has been rejected.</b></font>
</apex:outputPanel>
<apex:outputPanel rendered="{!IF(AND(Leave_Plan__c.Status__c == 'Cancel',CONTAINS($UserRole.Name,'Employee')),true,false)}">
<font color="red" size="2"><b>
<apex:image value="{!$Resource.Sad}" width="15" height="15"/>Something went wrong please create new record don't edit this record</b></font>
</apex:outputPanel>
</center>
<center><apex:outputLabel rendered="{!showAlert}">
<apex:image id="theImage2" value="{!$Resource.Smily}" width="15" height="15"/>
<font color="red" size="2"><b>Please refresh the page.This record has been sent for Approval already.</b></font>
</apex:outputLabel></center>
<center><apex:outputLabel rendered="{!showAlert1}">
<apex:image id="theImage4" value="{!$Resource.Smily}" width="15" height="15"/>
<font color="red" size="2"><b>You are not an authorized to submit this record for Approval</b></font>
</apex:outputLabel></center>
<center>
<apex:outputpanel rendered="{!refreshPage}" style="margin-left:-30%;">
<script>
window.top.location='/{!Leave_Plan__c.id}';
</script>
</apex:outputpanel>
<apex:form >
<apex:outputPanel rendered="{!requiredSection}">
<b>Please Select cancel reason : </b>
<apex:selectList value="{!strComment}" size="1" required="true">
<apex:selectOptions value="{!Options}"></apex:selectOptions>
</apex:selectList><br/>
<apex:commandButton value="Cancel Leave" action="{!cmdCancelLeave}" title="Cancel Leave" alt="Cancel Leave"/>
</apex:outputPanel>
<apex:commandButton value="Approve" action="{!cmdApprove}" rendered="{!approveShow}"/>
<apex:commandButton value="Reject" action="{!cmdReject}" rendered="{!rejectShow}" />
<apex:commandButton value="Submit for Approval" action="{!cmdSubmitforApproval}"
rendered="{!IF(Leave_Plan__c.Status__c=="New Request",true,false)}"/>
<apex:commandButton value="Cancel Request" action="{!cmdCancelRequest}"
rendered="{!IF(AND(Leave_Plan__c.Status__c!="Cancel",NOT(CONTAINS($UserRole.Name, 'Employee'))),true,false)}"/>
<!--<apex:commandButton value="Cancel Leave" action="{!cmdCancelLeave}"
rendered="{!IF(AND(OR(Leave_Plan__c.Status__c=="New Request",
Leave_Plan__c.Status__c=="Approval Pending",
Leave_Plan__c.Status__c=="Approved",
Leave_Plan__c.Status__c=="Recancelled"),
NOT(CONTAINS($UserRole.name, 'Employee'))),true,false)}"/>-->
<apex:commandButton value="Reassign" action="{!Reassign}" rendered="{!reassignShow}"/>
<apex:commandButton value="Cancel" action="{!cmdCancel}" rendered="{!IF(OR(Leave_Plan__c.Status__c=="New Request",
Leave_Plan__c.Status__c=="Approval Pending",
Leave_Plan__c.Status__c=="Approved",
Leave_Plan__c.Status__c=="Recancelled"),true,false)}"/>
<apex:commandButton value="Re-Request" action="{!cmdRerequest}"
rendered="{!IF(Leave_Plan__c.Status__c=="Rejected",true,false)}"/>
</apex:form>
</center>
</apex:page>
Apex:--
public class _Leaveplan_CTE{
//@Initial varialbles declaration
public final Leave_Plan__c leaveIds ;
public final Leave_Plan__c leaveDetail;
public boolean refreshPage {get;set;}
public boolean approveShow {get;set;}
public boolean rejectShow {get;set;}
public boolean reassignShow {get;set;}
public boolean showAlert {get;set;}
public boolean showAlert1 {get;set;}
public boolean cmdCancelLeaveShow {get;set;}
public boolean cmdCancelShow {get;set;}
public boolean requiredSection {get;set;}
public string strComment {get;set;}
//@Standard controller defination
public _Leaveplan_CTE(Apexpages.standardcontroller controller){
refreshPage = false;
showAlert = false;
showAlert1 = false;
cmdCancelLeaveShow = true;
requiredSection = false;
this.leaveIds = (Leave_Plan__c) controller.getRecord();
//@fetching complete record details from standard UI to class based on Id
leaveDetail = [select id,name,Company__c,Current_Approval_Status__c,Date_From__c,Date_To__c,
Email_Id__c,Employee_ID__c,Employee_Name1__c,First_Approval__c,Description__c,
Leave_Type__c,Location__c,Manager_Approval__c,Number_of_Days__c,Request_Submitted_on__c,
Status__c,Time_out_of_office__c,Employee_Name__c from Leave_Plan__c where id =:leaveIds.id limit 1];
}
//@This method will call at the time of user clicks on submit for approval process button
public pageReference cmdSubmitforApproval(){
cmdCancelLeaveShow = true;
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c from Leave_Plan__c where id=:leaveDetail.id];
if(l.status__c == 'Approval Pending'){
// ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Info, 'Your Lead is sent to Approval process already.');
//ApexPages.addMessage(msg);
showAlert = true;
return null;
}
if(usr.UserRole.Name.contains('Employee')){
try{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(leaveIds.id);
Approval.ProcessResult result = Approval.process(req1);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
}
return null;
}else{
showAlert1 = true;
return null;
}
return null;
}
//@Method: This will call and display Approve or Reject buttons based on user group membership
public void approvalNeeded(){
cmdCancelLeaveShow = true;
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
LIST < GroupMember > actList = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
set<Id> actId = new set<Id>();
if (actList.size() > 0) {
for(GroupMember g:actList){
actId.add(g.GroupId);
}
}
Integer i = [SELECT count()
FROM ProcessInstanceWorkItem
WHERE ProcessInstance.Status = 'Pending'
AND ProcessInstance.TargetObjectId = : leaveIds.Id AND(ActorId IN : actId OR ActorId = : UserInfo.getUserId())
LIMIT 1
];
if (i > 0) {
approveShow = true;
rejectShow = true;
reassignShow = true;
} else {
approveShow = false;
rejectShow = false;
reassignShow = false;
}
}
//@Method:This method will call at the time of user clicks on Approval button
public pageReference cmdApprove(){
cmdCancelLeaveShow = true;
Set<id> actId = new Set<Id>();
//@Fetching group membership here
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Approve');
req.setComments('Approved Happy vacation');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
refreshPage = true;
return null;
}
//@Method : This method will call at the time of Lead or Manager clicks on reject button
public pageReference cmdReject(){
try{
cmdCancelLeaveShow = true;
Set<id> actId = new Set<Id>();
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments('Rejected adjust your days');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
}
return null;
}
//@Method:This method will refresh standard and custom UI
public pageReference cmdCancel(){
refreshPage = true;
return null;
}
//@Method : Re-request will happen here
public pageReference cmdRerequest(){
try{
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
if(usr.UserRole.Name.contains('Employee')){}
cmdCancelLeaveShow = true;
Leave_Plan__c l1 = new Leave_Plan__c();
l1 = [select id,status__c,Date_From__c,Date_To__c,First_Approval__c,Manager_Approval__c,
Current_Approval_Status__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
l1.status__c = 'New Request';
l1.Date_From__c = null;
l1.Date_To__c = null;
l1.Time_out_of_office__c = null;
l1.First_Approval__c = false;
l1.Manager_Approval__c = false;
l1.Current_Approval_Status__c = '';
Database.update(l1);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
refreshPage = true;
}
refreshPage = true;
return null;
}
//@Method:method re-assignment will be done here
public PageReference Reassign(){
try{
cmdCancelLeaveShow = true;
List<ProcessInstanceWorkItem> workItemList = [Select p.ProcessInstance.Status, p.ProcessInstance.TargetObjectId,p.ProcessInstanceId,p.OriginalActorId,p.Id,p.ActorId
From ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :leaveIds.Id];
Leave_Plan__c l = new Leave_Plan__c();
l = [select id from Leave_Plan__c where id=:leaveDetail.id limit 1];
String retId =l.id;
String strAppId = workItemList[0].Id;
String partialURL = '/' + strAppId + '/e?et=REASSIGN&retURL=%2F'+retId;
PageReference pageRef = new PageReference(partialURL);
refreshPage = true;
return pageRef;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}
//@Method:It will cancel leaves and leave process will end here
/*public Pagereference cmdCancelLeave(){
try{
Set<id> actId = new Set<Id>();
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
if(leaveDetail.status__c == 'Approval Pending'){
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments('Cancelled apply for new leave');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
}
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c,recordtypeid from Leave_Plan__c where id=:leaveDetail.id limit 1];
l.status__c = 'Cancel';
l.First_Approval__c = false;
l.Manager_Approval__c = false;
l.recordtypeid = '012280000005BKGAA2';
Database.update(l);
refreshPage = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
cmdCancelLeaveShow = false;
cmdCancelShow = false;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}*/
//@Method:It will cancel leaves and leave process will end here
public Pagereference cmdCancelLeave(){
ObjectRecordTypeIds__c orgConstant = ObjectRecordTypeIds__c.getInstance();
String closedLeaveRecordtypeId = orgConstant.Marketing_Lead_RecordType__c;
try{
if(leaveDetail.status__c == 'Approval Pending'){
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and processinstance.targetobjectid = :leaveIds.id limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments(strComment);
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
}
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c,First_Approval__c,Manager_Approval__c,recordtypeid,Cancel_Reason__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
l.status__c = 'Cancel';
l.First_Approval__c = false;
l.Manager_Approval__c = false;
l.Cancel_Reason__c=strComment;
l.recordtypeid = ObjectRecordTypeId__c.getInstance('Closed Leave').Marketing_Lead_RecordType__c;
Database.update(l);
refreshPage = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
cmdCancelLeaveShow = false;
cmdCancelShow = false;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}
public pagereference cmdCancelRequest(){
requiredSection = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
return null;
}
public List<SelectOption> getOptions(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Adjust your days','Adjust your days'));
options.add(new SelectOption('Cancelled no requalification','Cancelled no requalification'));
options.add(new SelectOption('Cancel your request','Cancel your request'));
options.add(new SelectOption('Something went wrong on dates','Something went wrong on dates'));
options.add(new SelectOption('Dont plan this week','Dont plan this week'));
return options;
}
//@Method : Invoke apex method at the time of user clicking send to leaves button
public pagereference cmdSendLeave(){
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
if(usr.UserRole.Name.contains('Employee')){}
cmdCancelLeaveShow = true;
Leave_Plan__c l1 = new Leave_Plan__c();
l1 = [select id,status__c,Date_From__c,Date_To__c,First_Approval__c,Manager_Approval__c,
Current_Approval_Status__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
//Map<string,ObjectRecordTypeId__c> orga =ObjectRecordTypeId__c.getAll();
//if(orga.containsKey('Closed Leave')){
//l1.recordtypeId = orga.get(l1.id).name;
//}
l1.status__c = 'send to report';
l1.Date_To__c = null;
l1.Time_out_of_office__c = null;
l1.First_Approval__c = false;
l1.Manager_Approval__c = false;
l1.Current_Approval_Status__c = '';
Database.update(l1);
refreshPage = true;
requiredSection = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
return null;
}
public static void deletePreventTriggerMethod(List<Leave_Plan__c> leaveIds){
User currentUserRole = [select id,profileid,profile.name from User where id=:UserInfo.getUserId() limit 1 ];
if(!currentUserRole.profile.name.contains('System Administrator')){
for(Leave_Plan__c leaveId : leaveIds){
leaveId.addError('You are not an authorized to delete this record contact your Admin');
}
}
}
}
<center>
<apex:pageMessages ></apex:pageMessages>
<apex:outputPanel rendered="{!IF(Leave_Plan__c.Status__c == 'Approved',true,false)}">
<font color="green" size="2"><b>
<apex:image id="theImage" value="{!$Resource.Smily}" width="15" height="15"/>Your request has been approved.</b></font>
</apex:outputPanel><br/><br/>
<apex:outputPanel rendered="{!IF(Leave_Plan__c.Status__c == 'Rejected',true,false)}">
<font color="red" size="2"><b>
<apex:image id="theImage1" value="{!$Resource.Sad}" width="15" height="15"/>Your request has been rejected.</b></font>
</apex:outputPanel>
<apex:outputPanel rendered="{!IF(AND(Leave_Plan__c.Status__c == 'Cancel',CONTAINS($UserRole.Name,'Employee')),true,false)}">
<font color="red" size="2"><b>
<apex:image value="{!$Resource.Sad}" width="15" height="15"/>Something went wrong please create new record don't edit this record</b></font>
</apex:outputPanel>
</center>
<center><apex:outputLabel rendered="{!showAlert}">
<apex:image id="theImage2" value="{!$Resource.Smily}" width="15" height="15"/>
<font color="red" size="2"><b>Please refresh the page.This record has been sent for Approval already.</b></font>
</apex:outputLabel></center>
<center><apex:outputLabel rendered="{!showAlert1}">
<apex:image id="theImage4" value="{!$Resource.Smily}" width="15" height="15"/>
<font color="red" size="2"><b>You are not an authorized to submit this record for Approval</b></font>
</apex:outputLabel></center>
<center>
<apex:outputpanel rendered="{!refreshPage}" style="margin-left:-30%;">
<script>
window.top.location='/{!Leave_Plan__c.id}';
</script>
</apex:outputpanel>
<apex:form >
<apex:outputPanel rendered="{!requiredSection}">
<b>Please Select cancel reason : </b>
<apex:selectList value="{!strComment}" size="1" required="true">
<apex:selectOptions value="{!Options}"></apex:selectOptions>
</apex:selectList><br/>
<apex:commandButton value="Cancel Leave" action="{!cmdCancelLeave}" title="Cancel Leave" alt="Cancel Leave"/>
</apex:outputPanel>
<apex:commandButton value="Approve" action="{!cmdApprove}" rendered="{!approveShow}"/>
<apex:commandButton value="Reject" action="{!cmdReject}" rendered="{!rejectShow}" />
<apex:commandButton value="Submit for Approval" action="{!cmdSubmitforApproval}"
rendered="{!IF(Leave_Plan__c.Status__c=="New Request",true,false)}"/>
<apex:commandButton value="Cancel Request" action="{!cmdCancelRequest}"
rendered="{!IF(AND(Leave_Plan__c.Status__c!="Cancel",NOT(CONTAINS($UserRole.Name, 'Employee'))),true,false)}"/>
<!--<apex:commandButton value="Cancel Leave" action="{!cmdCancelLeave}"
rendered="{!IF(AND(OR(Leave_Plan__c.Status__c=="New Request",
Leave_Plan__c.Status__c=="Approval Pending",
Leave_Plan__c.Status__c=="Approved",
Leave_Plan__c.Status__c=="Recancelled"),
NOT(CONTAINS($UserRole.name, 'Employee'))),true,false)}"/>-->
<apex:commandButton value="Reassign" action="{!Reassign}" rendered="{!reassignShow}"/>
<apex:commandButton value="Cancel" action="{!cmdCancel}" rendered="{!IF(OR(Leave_Plan__c.Status__c=="New Request",
Leave_Plan__c.Status__c=="Approval Pending",
Leave_Plan__c.Status__c=="Approved",
Leave_Plan__c.Status__c=="Recancelled"),true,false)}"/>
<apex:commandButton value="Re-Request" action="{!cmdRerequest}"
rendered="{!IF(Leave_Plan__c.Status__c=="Rejected",true,false)}"/>
</apex:form>
</center>
</apex:page>
Apex:--
public class _Leaveplan_CTE{
//@Initial varialbles declaration
public final Leave_Plan__c leaveIds ;
public final Leave_Plan__c leaveDetail;
public boolean refreshPage {get;set;}
public boolean approveShow {get;set;}
public boolean rejectShow {get;set;}
public boolean reassignShow {get;set;}
public boolean showAlert {get;set;}
public boolean showAlert1 {get;set;}
public boolean cmdCancelLeaveShow {get;set;}
public boolean cmdCancelShow {get;set;}
public boolean requiredSection {get;set;}
public string strComment {get;set;}
//@Standard controller defination
public _Leaveplan_CTE(Apexpages.standardcontroller controller){
refreshPage = false;
showAlert = false;
showAlert1 = false;
cmdCancelLeaveShow = true;
requiredSection = false;
this.leaveIds = (Leave_Plan__c) controller.getRecord();
//@fetching complete record details from standard UI to class based on Id
leaveDetail = [select id,name,Company__c,Current_Approval_Status__c,Date_From__c,Date_To__c,
Email_Id__c,Employee_ID__c,Employee_Name1__c,First_Approval__c,Description__c,
Leave_Type__c,Location__c,Manager_Approval__c,Number_of_Days__c,Request_Submitted_on__c,
Status__c,Time_out_of_office__c,Employee_Name__c from Leave_Plan__c where id =:leaveIds.id limit 1];
}
//@This method will call at the time of user clicks on submit for approval process button
public pageReference cmdSubmitforApproval(){
cmdCancelLeaveShow = true;
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c from Leave_Plan__c where id=:leaveDetail.id];
if(l.status__c == 'Approval Pending'){
// ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Info, 'Your Lead is sent to Approval process already.');
//ApexPages.addMessage(msg);
showAlert = true;
return null;
}
if(usr.UserRole.Name.contains('Employee')){
try{
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(leaveIds.id);
Approval.ProcessResult result = Approval.process(req1);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
}
return null;
}else{
showAlert1 = true;
return null;
}
return null;
}
//@Method: This will call and display Approve or Reject buttons based on user group membership
public void approvalNeeded(){
cmdCancelLeaveShow = true;
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
LIST < GroupMember > actList = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
set<Id> actId = new set<Id>();
if (actList.size() > 0) {
for(GroupMember g:actList){
actId.add(g.GroupId);
}
}
Integer i = [SELECT count()
FROM ProcessInstanceWorkItem
WHERE ProcessInstance.Status = 'Pending'
AND ProcessInstance.TargetObjectId = : leaveIds.Id AND(ActorId IN : actId OR ActorId = : UserInfo.getUserId())
LIMIT 1
];
if (i > 0) {
approveShow = true;
rejectShow = true;
reassignShow = true;
} else {
approveShow = false;
rejectShow = false;
reassignShow = false;
}
}
//@Method:This method will call at the time of user clicks on Approval button
public pageReference cmdApprove(){
cmdCancelLeaveShow = true;
Set<id> actId = new Set<Id>();
//@Fetching group membership here
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Approve');
req.setComments('Approved Happy vacation');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
refreshPage = true;
return null;
}
//@Method : This method will call at the time of Lead or Manager clicks on reject button
public pageReference cmdReject(){
try{
cmdCancelLeaveShow = true;
Set<id> actId = new Set<Id>();
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments('Rejected adjust your days');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
}
return null;
}
//@Method:This method will refresh standard and custom UI
public pageReference cmdCancel(){
refreshPage = true;
return null;
}
//@Method : Re-request will happen here
public pageReference cmdRerequest(){
try{
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
if(usr.UserRole.Name.contains('Employee')){}
cmdCancelLeaveShow = true;
Leave_Plan__c l1 = new Leave_Plan__c();
l1 = [select id,status__c,Date_From__c,Date_To__c,First_Approval__c,Manager_Approval__c,
Current_Approval_Status__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
l1.status__c = 'New Request';
l1.Date_From__c = null;
l1.Date_To__c = null;
l1.Time_out_of_office__c = null;
l1.First_Approval__c = false;
l1.Manager_Approval__c = false;
l1.Current_Approval_Status__c = '';
Database.update(l1);
refreshPage = true;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Error occured' + e.getMessage()));
refreshPage = true;
}
refreshPage = true;
return null;
}
//@Method:method re-assignment will be done here
public PageReference Reassign(){
try{
cmdCancelLeaveShow = true;
List<ProcessInstanceWorkItem> workItemList = [Select p.ProcessInstance.Status, p.ProcessInstance.TargetObjectId,p.ProcessInstanceId,p.OriginalActorId,p.Id,p.ActorId
From ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :leaveIds.Id];
Leave_Plan__c l = new Leave_Plan__c();
l = [select id from Leave_Plan__c where id=:leaveDetail.id limit 1];
String retId =l.id;
String strAppId = workItemList[0].Id;
String partialURL = '/' + strAppId + '/e?et=REASSIGN&retURL=%2F'+retId;
PageReference pageRef = new PageReference(partialURL);
refreshPage = true;
return pageRef;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}
//@Method:It will cancel leaves and leave process will end here
/*public Pagereference cmdCancelLeave(){
try{
Set<id> actId = new Set<Id>();
map<Id,Group> mapIdGroup = new map<Id,Group>([SELECT Id from Group where RelatedId = : UserInfo.getUserRoleId()]);
List<GroupMember > grp = [SELECT GroupId from GroupMember where UserOrGroupId =: UserInfo.getUserId() OR
UserOrGroupId IN : mapIdGroup.keySet()];
for(GroupMember g : grp){
actId.add(g.GroupId);
}
if(leaveDetail.status__c == 'Approval Pending'){
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and
processinstance.targetobjectid = :leaveIds.id AND(ActorId = :UserInfo.getUserId() or ActorId=:actId ) limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments('Cancelled apply for new leave');
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
}
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c,recordtypeid from Leave_Plan__c where id=:leaveDetail.id limit 1];
l.status__c = 'Cancel';
l.First_Approval__c = false;
l.Manager_Approval__c = false;
l.recordtypeid = '012280000005BKGAA2';
Database.update(l);
refreshPage = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
cmdCancelLeaveShow = false;
cmdCancelShow = false;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}*/
//@Method:It will cancel leaves and leave process will end here
public Pagereference cmdCancelLeave(){
ObjectRecordTypeIds__c orgConstant = ObjectRecordTypeIds__c.getInstance();
String closedLeaveRecordtypeId = orgConstant.Marketing_Lead_RecordType__c;
try{
if(leaveDetail.status__c == 'Approval Pending'){
List <ProcessInstanceWorkItem> items = [select id,processinstanceid,processinstance.status,processinstance.targetobjectid,ActorId from ProcessInstanceWorkitem
where processinstance.status='Pending' and processinstance.targetobjectid = :leaveIds.id limit 1];
Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
req.setAction('Reject');
req.setComments(strComment);
req.setWorkitemId(items[0].Id);
Approval.ProcessResult result = Approval.process(req);
}
Leave_Plan__c l = new Leave_Plan__c();
l = [select id,status__c,First_Approval__c,Manager_Approval__c,recordtypeid,Cancel_Reason__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
l.status__c = 'Cancel';
l.First_Approval__c = false;
l.Manager_Approval__c = false;
l.Cancel_Reason__c=strComment;
l.recordtypeid = ObjectRecordTypeId__c.getInstance('Closed Leave').Marketing_Lead_RecordType__c;
Database.update(l);
refreshPage = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
cmdCancelLeaveShow = false;
cmdCancelShow = false;
}catch(Exception e){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,''+ e.getMessage()));
}
return null;
}
public pagereference cmdCancelRequest(){
requiredSection = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
return null;
}
public List<SelectOption> getOptions(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('Adjust your days','Adjust your days'));
options.add(new SelectOption('Cancelled no requalification','Cancelled no requalification'));
options.add(new SelectOption('Cancel your request','Cancel your request'));
options.add(new SelectOption('Something went wrong on dates','Something went wrong on dates'));
options.add(new SelectOption('Dont plan this week','Dont plan this week'));
return options;
}
//@Method : Invoke apex method at the time of user clicking send to leaves button
public pagereference cmdSendLeave(){
User usr = [select id,name,userroleid,UserRole.Name from user where userroleid=:UserInfo.getUserRoleId() limit 1];
if(usr.UserRole.Name.contains('Employee')){}
cmdCancelLeaveShow = true;
Leave_Plan__c l1 = new Leave_Plan__c();
l1 = [select id,status__c,Date_From__c,Date_To__c,First_Approval__c,Manager_Approval__c,
Current_Approval_Status__c from Leave_Plan__c where id=:leaveDetail.id limit 1];
//Map<string,ObjectRecordTypeId__c> orga =ObjectRecordTypeId__c.getAll();
//if(orga.containsKey('Closed Leave')){
//l1.recordtypeId = orga.get(l1.id).name;
//}
l1.status__c = 'send to report';
l1.Date_To__c = null;
l1.Time_out_of_office__c = null;
l1.First_Approval__c = false;
l1.Manager_Approval__c = false;
l1.Current_Approval_Status__c = '';
Database.update(l1);
refreshPage = true;
requiredSection = true;
reassignShow = false;
approveShow = false;
rejectShow = false;
return null;
}
public static void deletePreventTriggerMethod(List<Leave_Plan__c> leaveIds){
User currentUserRole = [select id,profileid,profile.name from User where id=:UserInfo.getUserId() limit 1 ];
if(!currentUserRole.profile.name.contains('System Administrator')){
for(Leave_Plan__c leaveId : leaveIds){
leaveId.addError('You are not an authorized to delete this record contact your Admin');
}
}
}
}