I'm seeing some peculiar behavior when adding an i...
# suitescript
j
I'm seeing some peculiar behavior when adding an item to an item sublist. You can see in my client script code below, all that i'm doing is calculating a credit card fee and adding a new item (called "Credit Card Fee", w/item id = 14506). All I do to add the new line item is to select a new line in the sublist, populate the item and amount fields and commit the line. What happens is that it creates the new line, then adds another one below it. It appears to be a timing issue, that while NS goes to retrieve the information for the item, I am commiting the first line. When NS returns the other information, like description and price level, It puts them in a new line item. I just want the one line to be added and not have a second one in "edit mode" appear. This seems like very odd behavior to me and undesirable in most cases. I have tried numerous ways to remove that last line item, including cancelline and remove line with no luck. It has been suggested to create another event handler to remove that line, but i have to believe that there is a simpler way to do this. Any thoughts?
Copy code
define(['N/record', 'N/currentRecord'], function (record, currentRecord) {

	/**
 	* @NApiVersion 2.x
 	* @NModuleScope SameAccount
	*/

	var exports = {};

 	function add_credit_card_fee() {
 		var amountTotal=0;
		
		var myRecord = currentRecord.get();

		var numLines = myRecord.getLineCount({
    		sublistId: 'item'
		});

		// Calculate credit card fee
		var amountTotal = myRecord.getValue({
			fieldId: 'total'
		});		
		var creditCardFee = (0.03 * amountTotal);

		// insert credit card fee
		myRecord.selectNewLine({sublistId:"item"});
		myRecord.setCurrentSublistValue({
			sublistId: "item",
			fieldId:"item",
			value: 14506
		});	
		myRecord.setCurrentSublistValue({
			sublistId: "item",
			fieldId:"rate",
			value: creditCardFee
		});
		myRecord.setCurrentSublistValue({
			sublistId: "item",
			fieldId:"amount",
			value: creditCardFee
		});
		myRecord.commitLine({sublistId:"item"});
 	}

 	exports.add_credit_card_fee = add_credit_card_fee;
 	return exports;
 })