[SalesForce] Approval Matrix using custom settings and its method

Could you help me with my method for my approval flow? I created a custom settings named Approver_Matrix . Here is a sample:

enter image description here

Note: Private Account and Public Account fields were checkbox.
For my method, the expected return of the method is a Map of Integer and object type Approver_Matrix : Map.

Public Map<Integer, Approval_Matrix__c > ApprovalProcess(Opportunity oppty) {
    Map<Integer, Approval_Matrix__c > approvalMap = new Map<Integer, Approval_Matrix__c >();

    Decimal contractAmount = Opportunity.Contract_Amount__c;
    String acctMgrRole =  Opportunity. Account_Manager__r.UserRole.Name;
    Boolean pubAcc =  acctMgrRole.contains(‘Public Account’);
    Boolean prvAcc = acctMgrRole.contains(‘Private Account’);

    List< Approval_Matrix__c > approverList = new List< Approval_Matrix__c > ([SELECT Name, Limit__c, Position__c, Approver__c, Approver_Email__c, Public_Account__c, Private_Account__c WHERE  Public_Account__c  =: pubAcc && Private_Account__c  =: prvAcc]);
    Integer approverCtr = 1;
    For (Approval_Matrix__c apr :approverList) {
        If (contractAmount <= apr.Limit__c) {
            approvalMap.add(approverCtr, apr);
            approverCtr ++;
        }
    }

    Return approvalMap;
}

Questions:

  1. What will be the differences between querying the custom settings using these List< Approval_Matrix__c > apr = Approval_Matrix__c.getall().values() VS SELECT Name, Limit__c, Position__c, Approver__c, Approver_Email__c, Public_Account__c, Private_Account__c WHERE Public_Account__c =: pubAcc && Private_Account__c =: prvAcc?
  2. What would be the effective one to be used?
  3. When using the SOQL query (see above), got an error saying unexpected token: ‘:’. What to do here?
  4. What missing code will do the work if the opportunity’s contract amount is 25, 000 and role of Account Manager is neither Public nor Private the following approver would be retrieved.

enter image description here

Best Answer

On 1, 2 and 3, I would choose to query for the value because all the conveniences of SOQL are then available:

Integer approverCtr = 1;
for (Approval_Matrix__c am : [
        SELECT Name, Limit__c, Position__c, Approver__c, Approver_Email__c,
                Public_Account__c, Private_Account__c
        FROM Approval_Matrix__c
        WHERE Public_Account__c = :pubAcc
        AND Private_Account__c =:prvAcc
        AND Limit__c >= :contractAmount
        ORDER BY Approver_Email__c
        ]) {
    approvalMap.put(approverCtr, am);
}

If you use Approval_Matrix__c.getAll().values(), you have to do more work in your Apex code with the only benefit being avoiding one SOQL query.

I don't understand your question 4.

Related Topic