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
Mahurrinah SimsMahurrinah Sims 

Approval Process - Ownership Transfer

How would I have an approval process start once someone tries to change ownership of an account record? I know I will need a bit of apex code. Is this possible for a beginner?
Tyler Mowbrey 1Tyler Mowbrey 1
Hello,

Yes this is possible. Create a trigger on the account record for after update, check to ensure that the owners are different once done, create your approval request and submit them. Ensure that you have built an approval request declaritavely that the new records will match first. You may want to set a flag such as Account.submitForApproval (checkbox) that gets set in code when the change is found and unchecked once the approval process is approved or rejected. Here is some quick code to help you out:
 
List<Approval.ProcessSubmitRequest> requests = new List<Approval.processSubmitRequest>();
        
        for(Account acc : Trigger.new)
        {
            Account oldAcc = trigger.oldMap.get(acc.Id);
            if(acc.OwnerId != oldAcc.OwnerId)
            {
                acc.submitForApproval = TRUE;
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Approving change of ownership');
                req.setObjectId(account.Id);
                requests.add(req);
            }
        }
        if(requests.size() > 0)
        {
            List<Approval.ProcessResult> results = Approval.process(requests);
        }

Let me know if you have any questions.
Mahurrinah SimsMahurrinah Sims
Hello Tyler,

Thanks for working with me on this! I have a few questions.

1) This approval process would only be for 4 reps who report to one manager. Is there any way to specify exactly who the approval process to happen for.

2) Can you outline the items in the code that has to be changed by me in order for this to work?

3) Will this code require sandbox testing?
 
4) Will you be able to walk me through these steps that you outlined here "create your approval request and submit them. Ensure that you have built an approval request declaritavely that the new records will match first. You may want to set a flag such as Account.submitForApproval (checkbox) that gets set in code when the change is found and unchecked once the approval process is approved or rejected."?

Thanks again for your help. I know there are a lot of questions but I am really new at this.
Tyler Mowbrey 1Tyler Mowbrey 1
Hello,

1. Yes this is possible in your approval process. In your criteria you would have something like Owner = Sales Rep 1, Owner = Sales Rep 2, .... Owner.Manager = Manager.
2. You do not need to change any portion of the code for this to work. The code provided just submits a record to the approval processing engine.
3. Yes it will
4. Please review https://help.salesforce.com/HTViewHelpDoc?id=approvals_creating_approval_processes.htm&language=en_US This help file will walk you through creating approval processes.Once you've run through the documentation I would be more than happy to answer further questions.

Tyler
Naiomi SpencerNaiomi Spencer
Hi Tyler, 

Can you explain the functions that make the trigger work? ie.  req.setComments