+ Start a Discussion
MayTheForceBeWithYouMayTheForceBeWithYou 

Triggers to Fire Dependent Approval Processes

I have two triggers-the first populates an object "loan application" from an XML document stored in my organization.  The second takes some of the values (Specifically Approver1, Approver2, Approver3, and Approver4) and sees if they are null or not.  Based on this, the second trigger knows what approval process must be fired off-additionally, there is a special case in which an alternate protocol is followed should the loan application have "Johnny Good" stored in its name field.  The first trigger works beautifully but the second is not starting any approval processes-I've double-checked to make sure the logic matches the Approval Process entry criteria; any ideas? Thanks!

 

trigger fillApprovers on Loan_Application__c (before insert, before update) {
    list<Loan_Application__c> theLoanApps = new list<Loan_Application__c>();
    //Append all batched Loan Applications to a Loan App list (Bulkifying the trigger)
    for (Loan_Application__c aLoanApp:trigger.new){
        theLoanApps.add(aLoanApp);
    }
    //Populate the fields in each loan app contained within the list
    for (Integer i=0; i<theLoanApps.size(); i++){
        //Utilize Dynamic Apex to dynamically create the query that returns the XML document to be parsed
        list<Document> docResult = [SELECT Body FROM Document WHERE Name LIKE :'LoanApp'+theLoanApps[i].ID_Number__c];
        //Convert the body (of type Blob) of the document resulting from the SOQL query to a string
        String xml = docResult[0].Body.toString();
        //Create a Dom.Document from the xml string
        Dom.Document doc = new Dom.Document();
        doc.load(xml);
        //Define root element
        Dom.XMLNode loanApp = doc.getRootElement();
        //Populate list of approvers while simultaneously removing this information from the document
        list<String> approvers = new list<String>();
        while (loanApp.getChildElement('Approver',null) != null) {
            approvers.add(loanApp.getChildElement('Approver',null).getText());
            loanApp.removeChild(loanApp.getChildElement('Approver',null));
        }
        //Repeat the while loop logic above for the other fields using if statements
        if (loanApp.getChildElement('OrderDate',null) != null) {
            String orderDate = loanApp.getChildElement('OrderDate',null).getText();
            theLoanApps[i].Date__c = orderDate;
        }
        if (loanApp.getChildElement('Name',null) != null) {
            String name = loanApp.getChildElement('Name',null).getText();
            theLoanApps[i].Name__c = name;
        }
        if (loanApp.getChildElement('Address',null) != null) {
            String address = loanApp.getChildElement('Address',null).getText();
            theLoanApps[i].Address__c = address;
        }
        if (loanApp.getChildElement('Score',null) != null) {
            String score = loanApp.getChildElement('Score',null).getText();
            theLoanApps[i].Credit_Score__c = score;
        }
        if (loanApp.getChildElement('History',null) != null) {
            String history = loanApp.getChildElement('History',null).getText();
            theLoanApps[i].Credit_History__c = history;
        }
        if (loanApp.getChildElement('Notes',null) != null) {
            String notes = loanApp.getChildElement('Notes',null).getText();
            theLoanApps[i].Notes__c = notes;
        }
        //Iterate through approvers list, updating the corresponding fields of the loan application
        //Depending on the approvers list size, some of the loan app's custom approver fields may be left blank
        if (approvers.size()>=1) {
            theLoanApps[i].Approver1__c=[SELECT Id FROM User WHERE Name = :approvers[0]][0].Id;
            if (approvers.size()>=2) {
                theLoanApps[i].Approver2__c=[SELECT Id FROM User WHERE Name = :approvers[1]][0].Id;
                if (approvers.size()>=3) {
                    theLoanApps[i].Approver3__c=[SELECT Id FROM User WHERE Name = :approvers[2]][0].Id;
                    if (approvers.size()==4) {
                        theLoanApps[i].Approver4__c=[SELECT Id FROM User WHERE Name = :approvers[3]][0].Id;
                    }
                }
            }
        }
    }
}

 

 

 

 

trigger Approval_Process on Loan_Application__c (after insert, after update) {
    //Bulkify the trigger
    for (Loan_Application__c loanapp : Trigger.new){
    //Override Scenario
    if (loanapp.Name__c=='Johnny Good'){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(loanapp.Id);
        //Submit the approval request for processing
        Approval.ProcessResult result = Approval.process(req);
    }
    //Single Approver
    else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id==null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(loanapp.Id);
        //Submit the approval request for processing
        Approval.ProcessResult result = Approval.process(req);
    }
    //Double Approver
    else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(loanapp.Id);
        //Submit the approval request for processing
        Approval.ProcessResult result = Approval.process(req);
    }
    //Triple Approver
    else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id==null){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(loanapp.Id);
        //Submit the approval request for processing
        Approval.ProcessResult result = Approval.process(req);
    }
    //Quadruple Approver
    else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id!=null){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(loanapp.Id);
        //Submit the approval request for processing
        Approval.ProcessResult result = Approval.process(req);
    }
    }
}

 

 

 

Note: I received a warning about non HTML friendly text being removed, both triggers save; but the second does not work at run time (no errors though) so if you notice a typo I think it's b/c the forum removed it....

 

 

 

 

Afzal MohammadAfzal Mohammad

Code looks ok to me. However, I have trimmed the 2nd trigger code here, for better debugging/understanding.

 

 

trigger Approval_Process on Loan_Application__c (after insert, after update) {
    //Bulkify the trigger
    for (Loan_Application__c loanapp : Trigger.new){
        //Override Scenario
        if (loanapp.Name__c=='Johnny Good'){
            submit4Approval(loanapp.Id);
        }
        //Single Approver
        else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id==null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
            submit4Approval(loanapp.Id);
        }
        //Double Approver
        else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id==null && loanapp.Approver4__r.Id==null){
            submit4Approval(loanapp.Id);
        }
        //Triple Approver
        else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id==null){
            submit4Approval(loanapp.Id);
        }
        //Quadruple Approver
        else if (loanapp.Approver1__r.Id!=null && loanapp.Approver2__r.Id!=null && loanapp.Approver3__r.Id!=null && loanapp.Approver4__r.Id!=null){
            submit4Approval(loanapp.Id);
        }
    }

    public void submit4Approval(Id objId){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setObjectId(objId);
        Approval.ProcessResult result = Approval.process(req);
    }
}

 

 

Hope that helps.

 

Afzal

MayTheForceBeWithYouMayTheForceBeWithYou

Good idea on extracting the logic; I'll update my code. Unfortunately I'm still having issues though, thanks for the help!