1

I have an object name Rules__c and there is an approval process written over this object. We also have trigger over this object. Rules__c is common object refer by different Community.

we want this approval process to run only in condition that call is comming from internal community user so we have created one field called is_internal_Community_userrecord.

In my beforeTrigger on Rules__c i am doing like below

if(internaluser){
set is_internal_Community_userrecord=true;
}

in Approval process I am checking if(is_internal_Community_userrecord)==true

My Assumption was before trigger will set the value and in approval process will function accordingly but looks like approval process is firing first. Can you please tell me if Approval process will run first or Trigger and How can i set value on record before approval process fires.

Thanks

2 Answers 2

2

Approval processes and Triggers are generally not going to be run in a common transaction.

We can submit records into approval processes via apex using the Approval class, the given example being

Account a = new Account(Name='Test', annualRevenue=100.0);
insert a;

Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setObjectId(a.id);

// Submit the approval request for the account
Approval.ProcessResult result = Approval.process(req1);

Submitting a record into an approval process can also be done via flows and process builder.

The point I'm trying to make here is that you should be in complete control of when a record is submitted to an approval process. Asking "which runs first?" doesn't make much sense.

  • If you do this as part of apex/process builder/flow, you have full control over when you set your target field and when you submit for approval
    • So perform your field update first (there is nothing special about this step) , then submit for approval
  • If you're doing this by clicking the standard "submit for approval" button in the web UI, then no triggers/process builder/flow on the record being approved are run unless you have at least one initial submission action with a field update in your approval process.
    • In this case, the entry criteria are checked first, and then any initial submission actions are run (which would cause triggers to run if there's a field update action).

There's an argument to be made about not submitting for approval via apex in a before trigger context (as you could lock the record before all changes to the record are made), but I think that's about the only extra consideration here.

1
  • Thanks for help. Can you please guide me if I have to write custom approval process , Can it send approval email to my users for approve the record. We want users should be able to approve the request or reject the request as It happened in standard functionality. Apr 25, 2021 at 3:45
0

You will have to explicitly call the approval process in your trigger. Approval logic should be in after context, you can set the field's value in before trigger and call the approval process in after trigger.

Approval.ProcessSubmitRequest approvalRequest = new Approval.ProcessSubmitRequest(); approvalRequest.setComments('Offer Submitted for approval'); approvalRequest.setObjectId(offer.Id); Approval.ProcessResult approvalResult = Approval.process(approvalRequest);

2
  • thanks for help.Can you please guide me if I have to write custom approval process , Can it send approval email to my users for approve the record. We want users should be able to approve the request or reject the request as It happened in standard functionality. Apr 25, 2021 at 3:46
  • You will need to define a standard approval process. Please go through the trailhead below to create a new approval process and then you can invoke that approval process from the trigger mentioned above. trailhead.salesforce.com/en/content/learn/projects/… Email will be sent to the users but you need to configure that in the approval process not the trigger.
    – Angad
    Apr 28, 2021 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .