I am trying to copy line values from one item record to another. In the destination record which is client-side (UI), I load the source item record.
I want to load two lists (vendor and site category lists). It seems when I perform a loop, the first list will work correctly but oddly the second list the indexing is off and nothing seems to work correctly. Here's the code (shortened to only the problem areas).
var src_recd = nlapiLoadRecord('inventoryitem',xx); var lists = ['itemvendor','sitecategory']; // loop through the lists and if there are any contents in the current UI record, remove those line items first for(var i=0; i<lists.length; i++) { var cnt = nlapiGetLineItemCount(lists[i]); if ( cnt > 0 ) { // remove the items from the list for(var line=cnt; line>0; line--) { nlapiSelectLineItem(lists[i],line); // if list is the first list, the current index show correct. // if list is second or more, the current index for the first loop shows the // next index of the previous list that just looped through alert('Current Index: '+nlapiGetCurrentLineItemIndex(lists[i])); nlapiRemoveLineItem(lists[i],line); // according to help, I don't need line in UI // the above does not work for the second or more lists } } } // end loop // now try and add lines - different function call for me for(var i=0; i<lists.length; i++) { var cnt = src_recd.getLineItemCount(lists[i]); if ( cnt <= 0 ) { continue; } // get field IDs (this is a custom list of fields I have but could use getAllLineItemFields... var flds = GetMyFields(lists[i]); // returns an array of fields for the list for(var line=1; line<=cnt; line++) { nlapiInsertLineItem(lists[i],line); // doesn't seem to work after the first insert of a list. // when you check nlapiGetCurrentLineItemIndex at this point, it is always 1 // get the value from the source list in the source field for(var x=0; x<flds.length; x++) { var fld = flds[x]; var src_val = src_recd.getLineItemValue(lists[i],fld,line); // put in the new UI list at the new line nlapiSetCurrentLineItemValue(lists[i],fld,src_val); // note: nlapiSetLineItemValue(lists[i],fld,line,src_val) doesn't work! } } } Anyone seeing buggy action from the line item APIs or is it just me. Another issue I found was with price level matrix. If I want to get the column count from a loaded record (src_recd.getMatrixCount('price','price') ) which should return my quantity columns, I get null returned. But, if I do nlapiGetMatrixCount(...) I get the correct number of columns returned. I'm trying to sort out the right behavior here. The documentation seems to be somewhat mixed on when to use setlineitem versus setcurrentlineitem, when to use commitlineitem and when not necessary, and so on. And the removelineitem seems to only work with the current line item (according to the help) but I would think the proper API for that (to be consistent) would be nlapiRemoveCurrentLineItem... Anyone see an error in my ways in the above code or had better luck using a different approach? Really could use some community insight on this one -- burning too many hours doing trial and error on something that seems pretty trivial. Thanks.-Steve