Getting System.FinalException: Record is read-only when PB calls an apex class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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.










share|improve this question























  • Do you have any trigger written on case?
    – Tushar Sharma
    1 hour ago










  • Rather than calling invocable method, you could update in PB field update action
    – Santanu Boral
    1 hour ago










  • 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.
    – Jerard Dela Victoria
    1 hour ago

















up vote
2
down vote

favorite












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.










share|improve this question























  • Do you have any trigger written on case?
    – Tushar Sharma
    1 hour ago










  • Rather than calling invocable method, you could update in PB field update action
    – Santanu Boral
    1 hour ago










  • 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.
    – Jerard Dela Victoria
    1 hour ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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.










share|improve this question















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.







apex process-builder invocable-method order-of-execution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Oleksandr Berehovskiy

6,50611532




6,50611532










asked 1 hour ago









Jerard Dela Victoria

485




485











  • Do you have any trigger written on case?
    – Tushar Sharma
    1 hour ago










  • Rather than calling invocable method, you could update in PB field update action
    – Santanu Boral
    1 hour ago










  • 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.
    – Jerard Dela Victoria
    1 hour ago

















  • Do you have any trigger written on case?
    – Tushar Sharma
    1 hour ago










  • Rather than calling invocable method, you could update in PB field update action
    – Santanu Boral
    1 hour ago










  • 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.
    – Jerard Dela Victoria
    1 hour ago
















Do you have any trigger written on case?
– Tushar Sharma
1 hour ago




Do you have any trigger written on case?
– Tushar Sharma
1 hour ago












Rather than calling invocable method, you could update in PB field update action
– Santanu Boral
1 hour ago




Rather than calling invocable method, you could update in PB field update action
– Santanu Boral
1 hour ago












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.
– Jerard Dela Victoria
1 hour ago





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.
– Jerard Dela Victoria
1 hour ago











1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










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;






share|improve this answer






















  • 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.
    – Jerard Dela Victoria
    1 hour ago










  • @JerardDelaVictoria I have updated my answer, check this variant
    – Oleksandr Berehovskiy
    45 mins ago










  • 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!
    – Jerard Dela Victoria
    13 mins ago










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233860%2fgetting-system-finalexception-record-is-read-only-when-pb-calls-an-apex-class%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










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;






share|improve this answer






















  • 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.
    – Jerard Dela Victoria
    1 hour ago










  • @JerardDelaVictoria I have updated my answer, check this variant
    – Oleksandr Berehovskiy
    45 mins ago










  • 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!
    – Jerard Dela Victoria
    13 mins ago














up vote
2
down vote



accepted










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;






share|improve this answer






















  • 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.
    – Jerard Dela Victoria
    1 hour ago










  • @JerardDelaVictoria I have updated my answer, check this variant
    – Oleksandr Berehovskiy
    45 mins ago










  • 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!
    – Jerard Dela Victoria
    13 mins ago












up vote
2
down vote



accepted







up vote
2
down vote



accepted






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;






share|improve this answer














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;







share|improve this answer














share|improve this answer



share|improve this answer








edited 46 mins ago

























answered 1 hour ago









Oleksandr Berehovskiy

6,50611532




6,50611532











  • 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.
    – Jerard Dela Victoria
    1 hour ago










  • @JerardDelaVictoria I have updated my answer, check this variant
    – Oleksandr Berehovskiy
    45 mins ago










  • 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!
    – Jerard Dela Victoria
    13 mins ago
















  • 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.
    – Jerard Dela Victoria
    1 hour ago










  • @JerardDelaVictoria I have updated my answer, check this variant
    – Oleksandr Berehovskiy
    45 mins ago










  • 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!
    – Jerard Dela Victoria
    13 mins ago















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.
– Jerard Dela Victoria
1 hour ago




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.
– Jerard Dela Victoria
1 hour ago












@JerardDelaVictoria I have updated my answer, check this variant
– Oleksandr Berehovskiy
45 mins ago




@JerardDelaVictoria I have updated my answer, check this variant
– Oleksandr Berehovskiy
45 mins ago












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!
– Jerard Dela Victoria
13 mins ago




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!
– Jerard Dela Victoria
13 mins ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233860%2fgetting-system-finalexception-record-is-read-only-when-pb-calls-an-apex-class%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

Confectionery