Hi all, I have a Restlet that updates a transactio...
# suitescript
m
Hi all, I have a Restlet that updates a transaction line level. Unfortunately sometimes I get an error Record has Changed. I understand why this is happening, but can’t avoid users or other scripts interacting with the same records at the same time. Is there a good way how to avoid this error? A way how to make the script wait until the record is saved before trying to modify it?
m
Here's a pattern I use. Basically it's a loop of load the record, update the record, save the record. If we get a RCRD_HAS_BEEN_CHANGED error, then we retry up to 5 times.
Copy code
let recordHasChanged = false;
let retryCount = 0;

do {
    try {
        const salesOrderRec = record.load({
            type: record.Type.SALES_ORDER,
            id: context.key,
            isDynamic: true,
        });

        context.values.forEach((item) => {
            const itemObj = JSON.parse(item);
            const lineNumber = salesOrderRec.findSublistLineWithValue({
                sublistId: 'item',
                fieldId: 'line',
                value: itemObj.line
            });
            
            salesOrderRec.selectLine({
                sublistId: 'item',
                line: lineNumber,
            });
            
            salesOrderRec.setCurrentSublistValue({
                sublistId: 'item',
                fieldId: 'job',
                value: itemObj.projectId,
            });
            
            salesOrderRec.commitLine({
                sublistId: 'item',
            });
        });

        salesOrderRec.save();
    } catch (ex) {
        recordHasChanged = ex.name === 'RCRD_HAS_BEEN_CHANGED';

        if (recordHasChanged) {
            log.error({
                title: 'Error updating SO',
                details: `Attempt ${++retryCount}: The sales order was changed while trying to update it.  Retrying...`
            });
        }

        // Rethrow if this is not the error we're looking for or the retryCount has been exceeded
        if (!recordHasChanged || retryCount >= 5) {
            // Perform some cleanup work here if needed.
            throw ex;
        }
    }
} while (recordHasChanged && retryCount < 5)
a
I would say that 90% of the time of the record has changed errors are due to improper use of worflows, a quick test would be to disable all workflows linked to that record and check if you dont see that error anymore in which case you need to identify and fix the workflow @Mike Robbins solution my solve your problem however that is extremely inefficient re-loading the record multiple times…
s
+1 on the retry if you can't control who or what is modifying the record. Alternatively, using record.submitFields (depending on the use case and fields being set) seems to avoid this issue via inline editing.
m
Thanks for the retry suggestion. I was really hoping I could avoid it 🙂
There are no workflows. There are various user event scripts where some of these I can’t influence as these are 3rd party bundles 😕
a
I still recommend you to reach out to the bundle owner or script owner to ask them to fix that because that is bad scripting/design if updating a record triggers the error: Record changed… The re-try approach is a band-aid but not a real solution because again is very very inefficient and not a good practice.
👍 1
245 Views