+ 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' Button and “Reject Comments” 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:
Approval Complete (as of now Max is 2 approvals – You can see approvers in Project record as “Level 1” and “Level 2” approvers).

User-added image
 
See below Image:
This is our requirement to enable one “Reject” button and “Reject - comment text” and cancel button is common when “Status = Approved” only.

User-added image
 
Below image: TimeCards in Approval View

User-added image
 
Below are Object and Fields data:

User-added image


First Action Item completed.
I successed to display reject button and comments on record page after "status = Approved" only for Admin user.
But when i'm click on reject, reject button function showing error.

Image: successed to display reject button and comments

User-added image
 
Below is Reject button code in class (ProjectWeekTimeCardNewController : 1566 to 1598 lines).

 
    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.size() == 0)
                procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Rejected' and TargetObjectId = :pt.id]);
             
            if(procins == null || procins.size() == 0)
                procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Approved' and TargetObjectId = :pt.id]);
             
            system.debug('procins:: '+procins);    
            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! '));   


If need further information, please let me know.

Thanks,
Manu