3

I am getting this error whenever my Process builder calls an Apex Class. I'm not sure if this is allowed so I'd like to ask for your input.

So basically, my Process Builder is in the Case object and it is triggered when User sends an email to a Queue.

When this happens, the Process Builder then calls an Apex class with an invocable method below:

    @InvocableMethod
    public static void methodA(List<Case> caseList) {

    //do some SOQL code here to determine xxxxxxxx

    for(Case c : caseList) {
        c.contactId = 'xxxxxxxx';
    }        
    update caseList;        
    }

Basically, the method just changes the contact Id of the case to something else and then updates the Case.

However, I instead get this error every time I send an email to the Queue.

An Apex error occurred: System.FinalException: Record is read-only

It would seem that I am not allowed to update the contactId of the Case. Is this correct? If so, then how can I update the contactId of the Case by the Apex Class?

Thanks.

3
  • Do you have any trigger written on case? Commented Sep 26, 2018 at 6:49
  • Rather than calling invocable method, you could update in PB field update action Commented Sep 26, 2018 at 6:57
  • there is a trigger in the case but it is for a different action. I don't think it is related to this or has any affect on this. Commented Sep 26, 2018 at 7:25

1 Answer 1

9

let me guess, that records, that are sent to Apex are sent from Process as "Select the Case record that started your process". According to the Order of Execution process is launched after after triggers. And if you are sending context records, looks like they all have "isReadOnly" flag on the sObject records. This flag is responsible for causing the FinalException. This is similar to situation, when you are trying to change trigger context records in after trigger event. In order to update records from current transaction, create new Case record with predefined Id.

@InvocableMethod
public static void methodA(List<Case> caseList) {
    List<Case> toUpdate = new List<Case>();
    for(Case c : caseList) {
        toUpdate.add(new Case(
            Id = c.Id,
            ContactId = 'xxxxxxxx';
        ));
    }
    update toUpdate;
}
3
  • Thanks for this example. I tried adding your AsyncUpdater code and see if it works but I still get the issue when I call AsyncUpdater.run. The error is still about read-only. Commented Sep 26, 2018 at 7:23
  • @JerardDelaVictoria I have updated my answer, check this variant Commented Sep 26, 2018 at 7:51
  • my friend, your code works! Thank you so much! I knew that I was missing something, but I just couldn't figure out what it was. Your explanation was also clear and it helped me understand where I got it wrong. Again, this code now works and thank you very much! Commented Sep 26, 2018 at 8:23

You must log in to answer this question.

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