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
honey.naranghoney.narang 

Automate Approval Process

Hi,

 

I have written approval process automation by trigger. I need some help in that. I need to set submitter of the record as record creator, how i can do that?

 

Thanks in advance.

SRKSRK

 

It a trigger on opp Opportunity

use the code for ref:-

 

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());
 
		}
 
	}
 
}

 

 

@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);
 
    }
 
}