When I create a Lead, I want Process Builder to execute an Apex routine to update the lead's OwnerId according to some business rules.

I have tried this in a simple fashion where my Apex sets OwnerId then updates the record, but this fails as "record is read-only". I can see that this is because the record will have been written to the database and is awaiting commit.

The process I have in mind then is:

  1. Insert Lead invokes Process.
  2. Process invokes Apex.
  3. Apex changes [Lead].OwnerId.
  4. (Something) updates Lead.

How can I do this in Process Builder?

My code in essence:

public static void assignNewLeadsToEndUsers(List<Lead> theLeadList) {

    for(Integer i = 0; i < theLeadList.size(); i++) {
        Lead leadToAssign = theLeadList[i];
        Id newOwnerId = chooseOwner();
        leadToAssign.OwnerId = newOwnerId; // <<-----------<< Exception here

    } // end for

I have also tried requerying the leads and using that list in my for loop. Is that a good approach?

  • Can you share some of the Apex that you're using to update the OwnerId? Jul 10, 2017 at 14:59
  • Added some code to my question @todd-sprinkel
    – Dizzley
    Jul 10, 2017 at 16:02
  • Is there some reason the owner needs to be selected via Apex and not via Lead Assignment rules? You can still force the assignment rules to be run via Apex following this pattern here: salesforce.stackexchange.com/questions/181034/… Jul 12, 2017 at 13:23
  • We have some dynamic rules as to which team/queue we assign leads to. They essentially replace the assignment rules.
    – Dizzley
    Jul 13, 2017 at 7:59

2 Answers 2


Why don't you use triggers?

From your error message

but this fails as "record is read-only"

I guess you had something like this:

trigger myTrig on Lead(before update, after update) {
    for (Lead l : Trigger.new) {
        l.OwnerId = //new owner;
    update Trigger.new;

If you were indeed using before triggers, you should not update the records, because the value will be changed anyway.

If you were using after triggers, you will first have to query the records, make changes in the queried records and then update them.

  • Thanks @Novarg, but I want to try Process Builder. I appreciate the difference between Before Insert and After Insert. A PB process will be effectively After Insert. If I can't achieve what I want I will be back using my Before Insert Trigger Handler.
    – Dizzley
    Jul 10, 2017 at 12:27
  • There are limitations with Process Builder. Another big factor is the lack of bulkification - a PB process will invoke many SOQL updates. I will rough out a new structure where I change the lead owner in the Before Insert trigger.
    – Dizzley
    Jul 11, 2017 at 7:10

You can create a process builder to update the Lead owner during its creation.

Follow these steps:

  1. Create new process builder and specify the process name.
  2. Add the object (Lead here) and when to Start the process
  3. You can filter the Leads on some condition or can skip it.
  4. Add action when the criteria will be True or False.
  5. Select update record(in your case)
  6. Select the Owner Id field and specify the value.

NOTE: These steps are not exact and will change according to your requirement. For more details refer Process builder Trailhead

  • Is there a specific relevant example in that tutorial @Ankita?
    – Dizzley
    Jul 10, 2017 at 12:27
  • Not specific to your use case. But those examples will help you.
    – Ankita
    Jul 10, 2017 at 12:39

You must log in to answer this question.

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