The user is encountering the error: “Record has Changed” or “The record has been edited since you retrieved it” when trying to save a Sales Order.
There is a script that updates all Item Fulfillment records (related to the Sales Order) to ‘Shipped’ before saving the Sales Order record and being able to create an Invoice afterwards
Solution:
These errors are due to the updates done to the Item Fulfillment while saving the Sales Order record at the same time. The Sales order fails to save and the Invoice is not created.
function beforeSubmit(scriptContext){
var newRecord = scriptContext.newRecord;
var status = newRecord.getText('orderstatus');
var customField = newRecord.getValue('custbody100');
if(customField){
if(status != 'Pending Billing'){
var updateSuccess = UpdateItemFulfillment(newRecord, customField);
//function that has a search of Item Fulfillment records related to the Sales Order
//Iterates through the search results set to load the Item Fulfillment record
//Sets values for the customField passed and status to Shipped and saves the record
}
}
}
function afterSubmit(scriptContext){
var newRecord = scriptContext.newRecord;
var customField = newRecord.getValue('custbody100');
if(customField){
createInvoice(newRecord);
//transforms Sales Order to Invoice to bill items fulfilled
}
}
The current Sales Order record has ‘Pending Fulfillment’ as the status. In the script above, it calls a function to update all the Item Fulfillment records (related to the Sales Order) status to ‘Shipped’. Updating all these Item Fulfillment records would trigger a change to the Sales Order’s status to ‘Pending Billing’ or ‘Pending Billing/Partially Fulfilled’ (depending on the situation).
Notice that the function being used is before submit. The errors are being encountered due to the change in status made to the Sales Order by the system due to the Item Fulfillment updates. The current Sales Order record object being used in the before submit function does not contain the updated record hence the system tells the user to refresh the page.
In order to eliminate and avoid these errors, before submit should not be used. Instead, after submit can be used.
function afterSubmit(scriptContext){
var newRecord = scriptContext.newRecord;
var status = newRecord.getText('orderstatus');
var customField = newRecord.getValue('custbody100');
if(customField){
if(status != 'Pending Billing'){
var updateSuccess = UpdateItemFulfillment(newRecord, customField);
if(updateSuccess == true){
createInvoice(newRecord.id);
}
}
}
}
We can update the Item Fulfillment records in after submit in order for them not to conflict with the simultaneous status change made by the system.