function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
devfredevfre 

Nedd help for approval process

can somebody help me with the apex class for approval process submission and its test class

SRKSRK

Code
trigger OpportunitySubmitForApproval on Opportunity (after update) {
 
    for (Integer i = 0; i < Trigger.new.size(); i++) {
 
        if (Trigger.old[i].Probability < 30 && Trigger.new[i].Probability >= 30) {
 
            // create the new approval request to submit
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
            req.setComments('Submitted for approval. Please approve.');
            req.setObjectId(Trigger.new[i].Id);
            // submit the approval request for processing
            Approval.ProcessResult result = Approval.process(req);
            // display if the reqeust was successful
            System.debug('Submitted for approval successfully: '+result.isSuccess());
 
        }
 
    }
 
}

 

 

Test Method

@isTest
private class TestOpportunitySubmitForApproval {
 
    static testMethod void testApprovalSuccess() {
 
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.Amount = 100;
        opp.CloseDate = Date.today();
        opp.Probability = 10;
        opp.StageName = 'Prospecting';
        // insert the new opp
        insert opp;
        // change the probability of the opp so the trigger submits it for approval
    opp.Probability = 40;
    // update the opp which should submit it for approval
    update opp;
 
        // ensure that the opp was submitted for approval
        List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :opp.id];
    System.assertEquals(processInstances.size(),1);
 
    }
 
}

 

 

Use the example for more ref

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_example.htm

devfredevfre

i need the code by using apex classes not by trigger and also please let me know what is the process instance mean which you have used in test class.. could you please help with the apex class

SRKSRK

no metter what are u using apex class or trigger code remain same

Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

req.setComments('Submitted for approval. Please approve.');
req.setObjectId(Trigger.new[i].Id);                                       // In place of trigger.nw[i].Id in apex class u can use obj.id

Approval.ProcessResult result = Approval.process(req);

 

 

 

process instance is a standard obj it carry all the record which is submited for arrpoval
& it have a field TargetObjectId which have the Id of the record which is submited for approval


devfredevfre

i tried writing the test class as above but it is not covering the code

devfredevfre

hi i'm submitting the approval process for custom object called hierarchy.. how to write apex class for that?

SRKSRK

Write a visual froce page where use hierarchy as a standared controller & user Apex class as a extenstion