When using a client script's fieldChanged event to perform actions on a sublist, the situation might be encountered in which nlapiSelectLineItem fails.
Consider the following scenario as an example. On a custom record there are:
- a custom body field of type List/Record,
- a custom child record sublist.
The following code snippet shows the content of the client script's fieldChangded function, which is trying to remove all the line items from the sublist when the body field is changed:
if (name =='custrecord0') { var lineCount = nlapiGetLineItemCount('recmachcustrecord1'); for (var i=lineCount; i>0; i--) { nlapiSelectLineItem('recmachcustrecord1', i); nlapiRemoveLineItem('recmachcustrecord1', i); } }
It is a prerequisite in client side scripts that the line item is selected before calling nlapiRemoveLineItem.
The outcome of changing the value of the body field (internal id: custrecord0) might be that the line items are removed, it is however possible that the code will fail (without throwing an error) and the line items are not removed. The code is correct and should be able to remove the line items, however the trigger is not the right one.
When the fieldChanged event is triggered an asynchronous server call is done to source the field values. As long as a response has not been received, the call to nlapiSelectLineItem will fail. Depending on the response time the code might succeed, however it is not guaranteed.
A better approach is to use the postSourcing event instead of the fieldChanged event. The post sourcing event handler is triggered only after the fields are sourced from the server. However, please note that the postSourcing event is triggered only by sourced field changes. Therefore the choice between the fieldChanged and the postSourcing events depends on the type of field, whose change is triggering the actions.
No comments:
Post a Comment