+ Start a Discussion
Salesforce2015Salesforce2015 

Approval Process - Admin reject the record

Hi Experts,

My Scenario:
1. User with system administrator profile should see an option to 'Reject' the time sheet that is already in "approved" status.
2. When the system administrator rejects the timesheet, it should be changed to 'new' status and the consultant should be able to edit the hours and resubmit it.

See below image:

Final Approval Completed

Thanks,
Manu
 
surasura
I dont think you can reject a record that is alrady approved using Salesforce Approval prcoess. 
waht you can do is add a custom button called reject  and show it only to admin users (you can do it by checking llogged users profile)
on click of your reject button writer the logic to update the record status to New .
on you have to set unlock the record on Approval actions,
maintain you own custom property to determine whether record is allowed to edit . and set it to true on reject click
 
Salesforce2015Salesforce2015
Hi Sura,

Thanks for quick reply.
Please find below is my two peaces of code in Apex class.


pt = [select id,name, Start_Date__c, End_date__c, Employee__r.Name,Status__c, Time_Approval__c, Approval_Status__c, Project_Type__c, 
                Project_Id__r.id, Project_Id__r.name, Customer_Name_PDF__c, Project_Id__r.Customer_Name__c, Project_Id__r.MPM4_BASE__Status__c,  
                Project_Id__c, Total_Hours__c, Employee__c, Week_Number__c, Level1_Approver_Time_Sheet__r.name, Level2_Approver_Time_Sheet__r.name, 
                Level3_Approver_Time_Sheet__r.name from WeeklyTimeCards__c where id =:ptcid
                ];
            if (pt.Approval_Status__c == 'Pending Approval') {
                if (UserInfo.getUserId() == pt.Level1_Approver_Time_Sheet__c   || profileName == 'System Administrator') {
                     isApproveDisable = false;  
                } else {
                    isApproveDisable = true;  
                }
            } else if (pt.Approval_Status__c == 'Level1 Approval Complete') {
                if (UserInfo.getUserId() == pt.Level2_Approver_Time_Sheet__c  || profileName == 'System Administrator') {
                     isApproveDisable = false;  
                } else {
                    isApproveDisable = true;  
                }
            }  else if (pt.Approval_Status__c == 'Level2 Approval Complete') {
                if (UserInfo.getUserId() == pt.Level3_Approver_Time_Sheet__c || profileName == 'System Administrator') {
                     isApproveDisable = false;  
                } else {
                    isApproveDisable = true;  
                }
            } else if (pt.Approval_Status__c == 'Level2 Rejected') {
                if (UserInfo.getUserId() == pt.Level1_Approver_Time_Sheet__c|| profileName == 'System Administrator') {
                     isApproveDisable = false;  
                } else {
                    isApproveDisable = true;  
                }
            } else if (pt.Approval_Status__c == 'Level3 Rejected') {
                if (UserInfo.getUserId() == pt.Level2_Approver_Time_Sheet__c|| profileName == 'System Administrator') {
                     isApproveDisable = false;  
                } else {
                    isApproveDisable = true;  
                }
            } else {
                isApproveDisable = true;
            }   




public pageReference doReject() {
        if (ApproveRejectComments == null || ApproveRejectComments == '') {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Please enter reject comments and resubmit!! '));
            return null;
        }    
        try {    
            Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(pt.id);  
            Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
            List<ProcessInstance> procins;
            procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Pending' and TargetObjectId = :pt.id]);

            if(procins == null)
                procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Rejected' and TargetObjectId = :pt.id]);
                
            List<ProcessInstanceWorkitem>  workitem = new List<ProcessInstanceWorkitem>([select Id from ProcessInstanceWorkitem where ProcessInstanceId = :procins[0].id]);
            
            if ((workitem != null) && (workitem.size() > 0)) {
                if (ApproveRejectComments == null || ApproveRejectComments == '') {
                    pwr.SetComments('Rejected');
                } else {
                    pwr.SetComments(ApproveRejectComments);
                }    
                pwr.setWorkItemId(workitem[0].id);
                pwr.setAction('Reject');
            }
            Approval.ProcessResult pr = Approval.process(pwr); 
            System.assert(pr.isSuccess(), 'Reject Result Status:'+pr.isSuccess());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Your TimeSheet has been Rejected! '));   


Thanks,
Manu