Prior to enabling LWS in Spring '22 we were able to successfully 'modify' objects returned from an Apex Call. After enabling LWS we cannot modify the data. Allow me to give an example.
Below is some 'pseudo code' which :
Calls an Apex method
The result of the call is a list of custom objects, each of which has two attributes : quantity and price
After the result is returned, we loop through all the items and perform a simple calculation using the above mentioned attributes, and store the result on the item in an attribute called TotalPriceAmount.
The updated line items are stored in a variable that is bound to the UI, so we can see all three values :)
return getLineItems({id : recordId}) .then(result => { result.forEach(oneLineItemVm => { /* Embellish the line item with a TotalPriceAmount so we can show it on the UI :) */ let quantity = oneLineItemVm.Quantity; let unitprice = oneLineItemVm.UnitPrice; oneLineItemVm.TotalPriceAmount = unitprice * quantity; }); /* Store the embellished on a @tracked attribute somewhere lineItems = result; ....
That 'pattern' works fine before enabling LWS. The line that sets the TotalPriceAmount has NO effect after enabling LWS.
It appears that data returned from Apex is now not able to be modified? Can anyone confirm that they have seen similar behavior?
Of course, this has made me question the original pattern, and perhaps I agree that we should NOT be attempting to modify the data. It's not hard to imagine unintended side effects of this way of coding.
But the question remains still. Does anyone else see this changed behavior?
as an aside .... If a clone (using whatever method you like (in our case _.deepClone) is made of the returned data, then behavior is as expected (we can see the TotalPriceAmount) on the item.
We'll most likely be adopting this (cloning any returned data if it needs to be embellished) as standard practice from now on.
Any thoughts would be appreciated. Just looking for some confirmation here on what I'm seeing.
Thanks
edit : in case it is not clear, the apex method (in my pseudo code example : getLineItems) is NOT a cacheable method. It would look like this in the real world
@AuraEnabled
public static List < LineItemVm > getLineItems(Id id) {
If this method was decorated with (cacheable=true) then I of course would not be even asking the question. Attempting to modify the result of such a method has NEVER worked, nor should it.
the api version of the Apex Class : 48 the api version of the LWC : 52
My question stands as is : Would someone be able to confirm for me (like a yes or no answer) whether the behavior I am describing can be reproduced.