I have a need to generate an approval request and then immediately approve it via Apex. This is being done purely for record keeping and to properly show that a user approved a record. For instance, this is the type of code I am looking to run (taken from Salesforce):
// Insert an account
Account a = new Account(Name='Test',annualRevenue=100.0);
insert a;
// Create an approval request for the account
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval.');
req1.setObjectId(a.id);
// Submit the approval request for the account
Approval.ProcessResult result = Approval.process(req1);
// Approve the submitted request
// First, get the ID of the newly created item
List<Id> newWorkItemIds = result.getNewWorkitemIds();
// Instantiate the new ProcessWorkitemRequest object and populate it
Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve');
req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});
// Use the ID from the newly created item to specify the item to be worked
req2.setWorkitemId(newWorkItemIds.get(0));
// Submit the request for approval
Approval.ProcessResult result2 = Approval.process(req2);
Now, as with all approval processes, this will generate an email to the user. However, the user has already approved the request so that email is irrelevant. Is there a way to completely stop that email? I know we can override what is sent, but I just don't want to send anything. Does anyone have any ideas?
EDIT: For clarification, the use case for this would be something like:
- User A has the Standard Sales Rep profile.
- User B has the Sales Manager profile and is the manager of User A.
- When User A creates a record of Custom_Object__c, that object is automatically submitted for approval to User B.
- When User B Submits a record of Custom_Object__c, the system sees they have the proper profile and automatically submits and approves the record.
The issue is when User B does that, they get an email indicating that there is an item waiting for their approval even though that approval already occurred.
Best Answer
Is there a reason you are using a User as approver? Approver can also be a queue. What about creating a queue, no users, and assigning that as approver?