This topic contains 3 replies, has 0 voices, and was last updated by Aaron@MIT 8 years ago.
-
AuthorPosts
-
November 15, 2016 at 3:04 pm #1540
Aaron@MITHello, everyone.
I am trying to write a script that removes the Component list for an Assembly item and then adds items back on as I’ve specified. The functionality is happening in the BeforeSubmit function of a user event on Assembly Items, but I keep encountering issues and errors.
I thought that the correct code would be
Code:
var newRec = scriptContext.newRecord;util.each( itemsArray, function( value, index ) {
var itemID = value[ itemID ];
var quantity = value[ quantity ];newRec.selectNewLine({ sublistId: “member” });
newRec.setCurrentSublistValue({ sublistId: “member”, fieldId: “item”, value: itemID });
newRec.setCurrentSublistValue({ sublistId: “member”, fieldId: “quantity”, value: quantity });
newRec.commitLine({ sublistId: “member” });
});
However, when I try this code, I get the following error:TypeError: Cannot find function selectNewLine in object DeferredDynamicRecord.
The same happens for the setCurrentSublistValue and commitLine functions. Why can’t I use these functions on a DeferredDynamicRecord?
Another problem is that I need to remove all the current Component items before adding new ones, since the new list may have fewer items than the old one did. But when I add this code to my script:
Code:
do {
newRec.removeLine({ sublistId: “member”, line: 0 });
} while( newRec.getLineCount({ sublistId: “member” }) > 0 );
I start getting Unexpected Errors on that line! Gah!Similarly, this thread mentioned a possible workaround, but the last post makes a good point. If the workaround isn’t supported, then it’s only a matter of time before it breaks, and I definitely don’t want to have come in to find that one day. Also, since the workaround doesn’t remove old entries, it doesn’t do everything I need. Also, I really don’t want to have to load the record that’s already loaded using record.load, especially since I would then have to handle the user event script not getting stuck in an infinite loop when I submit that loaded record.
Has anyone experienced this and found a solution? Am I missing something obvious?
Thanks in advance.
This is a cached copy. Click here to see the original post. -
November 16, 2016 at 5:04 am #1541
Olivier Gagnon NCHave you tried switching from .setCurrentSublistValue to .setSublistValue, without the selectLine? Not saying that’ll necessarerily work, but when one method doesn’t work, usually the other does.
-
November 16, 2016 at 7:58 am #1542
Aaron@MITI have, and that does work for adding new lines to the Component List. Unfortunately, I need to also remove lines if they are no longer meant to be on the list, and .removeLine() throws an error when using the scriptContext.newRecord record. Is there an alternative for that?
-
November 16, 2016 at 8:23 am #1543
Aaron@MITNevermind, I found the cause of the unexpected error. I modified my code and now it works as planned.
For anyone that finds this thread in the future, here’s what I ended up doing:
As for removing all the lines first, the code that threw the error was this:
Code:
do {
newRec.removeLine({ sublistId: “member”, line: 0 });
} while( newRec.getLineCount({ sublistId: “member” }) > 0 );
I’m not sure why this doesn’t work, but I guess that’s just the nature of unexpected errors. I was able to fix it by using the following code instead:Code:
var existingRows = newRec.getLineCount({ sublistId: “member” });
for( var i = 0 ; i < existingRows ; i++ )
newRec.removeLine({ sublistId: "member", line: 0 });
As for adding the new lines, I was able to do so using Olivier's suggestion. The final code is now something likeCode:
var newRec = scriptContext.newRecord;util.each( itemsArray, function(value, index) {
var itemID = value.itemID;
var quantity = value.quantity;newRec.setSublistValue({ sublistId: "member", line: index, fieldId: "item", value: itemID });
newRec.setSublistValue({ sublistId: "member", line: index, fieldId: "quantity", value: quantity });
});
Note that I'm able to incrementally specify the line number of the component that I'm adding, even though I haven't specifically created that line. Apparently this is just handled in SuiteScript (though I'm afraid that it will break someday). I'll just have to watch out for it.Thanks for your help, Olivier!
-
AuthorPosts
You must be logged in to reply to this topic.