-1

I'm trying to save the following controller, it's meant to mass approve a custom object record "Expense__c".

Please help...

public void approveActionFunction()
{
    public void approveActionFunction()
    {
        List<Employee_Detail__c> selectedListViewRecords = (List<Employee_Detail__c>) standardSetController.getSelected();
        system.debug(selectedListViewRecords);
        try
        {
            String idkey = UserInfo.getUserId();
            Boolean hasAccess = false;
            List<Profile> p = [SELECT Id, Name FROM Profile WHERE Name = 'System Administrator'];
            if (UserInfo.getProfileId() == p[0].id) hasAccess = true;
            List<Employee_Detail__c> recordList = new List<Employee_Detail__c>(TFFs[0]);
            List<String> ids = new List<String>();
            for (Employee_Detail__c rec : selectedListViewRecords)
            {
                ids.add(rec.id);
            }
            system.debug('ids' + ids);
            List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
            List<ProcessInstanceWorkitem> workItems;
            if (hasAccess) workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId IN :ids];
            else workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ActorId = :idkey AND ProcessInstance.TargetObjectId IN :ids];
            system.debug(workItems);
            for (ProcessInstanceWorkitem workItem : workItems)
            {
                Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                req.setWorkitemId(workItem.Id);
                req.setAction('Approve'); req.setComments('Auto approved.'); requests.add(req);
            }
            Approval.ProcessResult[] processResults = Approval.process(requests);
        }
        catch (Exception ex)
        {
            return null; system.debug(ex.getMessage());
        }
    }
}
4
  • You should start with "public class approveActionFunction { .... "
    – Wim
    Commented Oct 3, 2017 at 5:38
  • Thank you So much for the quick response, i'm getting the following error now - "Variable does not exist: standardSetController " Commented Oct 3, 2017 at 6:01
  • Basic debugging required
    – Eric
    Commented Oct 3, 2017 at 7:16
  • I'm a newby, can you guys please help me out... Commented Oct 3, 2017 at 8:24

1 Answer 1

0

I understand that you want to build a custom list controller. Maybe you should have a look at this example:

Your VisualForce Page:

<apex:page controller="approveActionController">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!EmployeeDetails}" var="o">
            <apex:column value="{!o.Name}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Your Controller:

public class approveActionController
{
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                    [SELECT Id, Name FROM Employee_Detail__c]));  // you might need to modify this query
            }
            return setCon;
        }
        set;
    }

    public List<Opportunity> getEmployeeDetails() {
        return (List<Opportunity>) setCon.getRecords();
    }


    public void approveActionFunction()
    {
        List<Employee_Detail__c> selectedListViewRecords = (List<Employee_Detail__c>) setCon.getSelected();
        system.debug(selectedListViewRecords);
        try
        {
            String idkey = UserInfo.getUserId();
            Boolean hasAccess = false;
            List<Profile> p = [SELECT Id, Name FROM Profile WHERE Name = 'System Administrator'];
            if (UserInfo.getProfileId() == p[0].id) hasAccess = true;
            List<Employee_Detail__c> recordList = new List<Employee_Detail__c>(TFFs[0]);
            List<String> ids = new List<String>();
            for (Employee_Detail__c rec : selectedListViewRecords)
            {
                ids.add(rec.id);
            }
            system.debug('ids' + ids);
            List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest>();
            List<ProcessInstanceWorkitem> workItems;
            if (hasAccess) workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId IN :ids];
            else workItems = [SELECT Id, ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ActorId = :idkey AND ProcessInstance.TargetObjectId IN :ids];
            system.debug(workItems);
            for (ProcessInstanceWorkitem workItem : workItems)
            {
                Approval.ProcessWorkitemRequest req = new Approval.ProcessWorkitemRequest();
                req.setWorkitemId(workItem.Id);
                req.setAction('Approve'); req.setComments('Auto approved.'); requests.add(req);
            }
            Approval.ProcessResult[] processResults = Approval.process(requests);
        }
        catch (Exception ex)
        {
            system.debug(ex.getMessage());
            return null; 
        }
    }
}

I haven't tested it, but I think it should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .