This topic contains 14 replies, has 0 voices, and was last updated by NateS 6 years, 8 months ago.

  • Author
    Posts
  • #18320

    NateS

    I’m writing a script that takes a sales order with a kit, and adds the components of the kit to the sales order and then removes the individual kit itself. I have no problem adding an item to the sales order outside of a loop, but as soon as I put it inside of a for-loop I get UNEXPECTED_ERROR.

    Code:
    /**
    * @NApiVersion 2.x
    * @NScriptType UserEventScript
    * @NModuleScope SameAccount
    */
    define([‘N/record’, ‘N/search’, ‘N/email’], function(record, search, email) {
    function afterSubmit(scriptContext) {
    // var salesOrderId = scriptContext.newRecord.id;
    var salesOrderId = 536658;
    // Used to store kit components item IDs
    var componentsOfKit = [];
    // Use to store quantity of each component
    var componentsQTY = [];

    var componentsPrices = [];

    //Loads Sales Order
    salesOrder = record.load({
    type: record.Type.SALES_ORDER,
    id: salesOrderId,
    isDynamic: true
    });

    itemsInSalesOrder = [];
    itemsCount = salesOrder.getLineCount({
    sublistId: ‘item’
    });

    //Creates array of items in sales order to be passed as parameter into searchForKit function
    for(var i=0; iThis is a cached copy. Click here to see the original post.

  • #18321

    david.smith

    Have you inspected your componentsOfKit array? I would log that and see what it’s doing as a first step.

  • #18322

    NateS

    Originally posted by david.smith

    View Post

    Have you inspected your componentsOfKit array? I would log that and see what it’s doing as a first step.

    It’s returning the ids of the components of the kit.

    Here is the output:

    Code:
    componentsOfKit: 82875,77864,77899
    I still get the error if I just put in the item’s internal id manually. It’s really weird, like I said, if I add them individually outside of the loop it works, but I need it to be dynamic since not all kits are the same.

  • #18323

    david.smith

    Well there are quite a few things I would structure differently in this script. However, I’m not sure if there are mandatory column fields you’re not setting on the form or not. Have you tried setting the isDynamic mode of the sales order to false?

  • #18324

    NateS

    Originally posted by david.smith

    View Post

    Well there are quite a few things I would structure differently in this script. However, I’m not sure if there are mandatory column fields you’re not setting on the form or not. Have you tried setting the isDynamic mode of the sales order to false?

    No, I’ll give that a try

  • #18325

    NateS

    Originally posted by david.smith

    View Post

    Well there are quite a few things I would structure differently in this script. However, I’m not sure if there are mandatory column fields you’re not setting on the form or not. Have you tried setting the isDynamic mode of the sales order to false?

    I feel like there is something wrong here. Even after I turned off dynamic mode for the sales order I’m getting a can’t find function selectLine in DeferredDynamicRecord

  • #18326

    david.smith

    Oh, I think I see something. You are trying to save the SO after each line which causes the record to be changed and would though an error on the second line your adding.

    Code:

    salesOrder.save({});

  • #18327

    NateS

    Originally posted by david.smith

    View Post

    Oh, I think I see something. You are trying to save the SO after each line which causes the record to be changed and would though an error on the second line your adding.

    Code:
    salesOrder.save({});

    I’ve tried

    Code:
    salesOrder.save({})
    outside of the loop as well and was getting the same error.


    david.smith replied on 07/31/2017, 01:53 PM: It should always be the last thing you do.

  • #18328

    NateS

    david.smith I’ve even tried completely removing the record.save({}) function just to see if that atleast gets rid of the error and I still get a unexpected_error message on the same line. I’m completely stumped.

  • #18329

    Mercedes Lerena RST

    The error comes up the second time you select a new line with selectNewLine(), setCurrentSublistValue() and commitLine(). I tried to run the script in standard mode using insertLine(), setSublistValue() and save() but now I get the same error on save(). I suggest to involve Support or use SuiteScript 1.0.

  • #18330

    NateS

    Originally posted by Mercedes Lerena RST

    View Post

    The error comes up the second time you select a new line with selectNewLine(), setCurrentSublistValue() and commitLine(). I tried to run the script in standard mode using insertLine(), setSublistValue() and save() but now I get the same error on save(). I suggest to involve Support or use SuiteScript 1.0.

    Thanks for confirming my problem. I’ll see what support has to offer.

  • #18331

    zackb22

    Originally posted by NateS

    View Post

    Thanks for confirming my problem. I’ll see what support has to offer.

    Code:
    salesOrder.setCurrentSublistValue({
    sublistId: ‘item’,
    fieldId: ‘item’,
    value: componentsOfKit[i],
    ignoreFieldChange: false,
    fireSlavingSync: true });
    Try adding these parameters to your setCurrentSublistValues. I’ve also had to do window.setTimeout(FUNCTION(), 10) to slow down the adding of lines as well.

  • #18332

    NateS

    Originally posted by zackb22

    View Post

    Code:
    salesOrder.setCurrentSublistValue({
    sublistId: ‘item’,
    fieldId: ‘item’,
    value: componentsOfKit[i],
    ignoreFieldChange: false,
    fireSlavingSync: true });
    Try adding these parameters to your setCurrentSublistValues. I’ve also had to do window.setTimeout(FUNCTION(), 10) to slow down the adding of lines as well.

    Nope, still didn’t work. I’ve opened a case with support. Once I get this figured out I’ll post the solution here in case anybody else runs into this problem.

  • #18333

    mary.thomas

    Is there a solution to it. am running into the similar problem.

  • #18334

    NateS

    Originally posted by mary.thomas

    View Post

    Is there a solution to it. am running into the similar problem.

    Sorry mary, I didn’t see this reply until just now, but the issue resolved itself with the 2017.2 release, for me anyway.

You must be logged in to reply to this topic.