Anybody know a solution for: Cannot Add Component ...
# suitescript
a
Anybody know a solution for: Cannot Add Component Item? as described in Answer Id: 78672? The Script creates a new Bill of Materials(BOM) and set the Subsidiary subsidiary but when creating the BOM Revision this record is not getting the right subsidiary from the Bill of Materials and therefore the script is unable to add the components.
b
not enough code to guess what you are doing
a
Already tried: • Set the defaultValue of the
subsidiary
when creating the BOM(not possible). • Set the include children checkbox after setting the Subsidiary. • Set the defaultValue of
billofmaterials
when creating the BOM Revision(not possible). Both records are being created in Dynamic mode.
Untitled.js
b
meh, works for me with alterations to mandatory fields
Copy code
require(["N/record"], function (record) {
  const oBOMRec = record.create({
    type: record.Type.BOM,
    isDynamic: true,
  });

  oBOMRec.setValue({
    fieldId: "name",
    value: "BOM 1",
  });
  oBOMRec.setValue({
    fieldId: "subsidiary",
    value: "1",
  });
  oBOMRec.setValue({
    fieldId: "includechildren",
    value: true,
  });
  oBOMRec.setValue({
    fieldId: "usecomponentyield",
    value: true,
  });
  oBOMRec.setValue({
    fieldId: "availableforalllocations",
    value: true,
  });

  const sBOMRecId = oBOMRec.save({
    enableSourcing: true,
    ignoreMandatoryFields: true,
  });
  var pEngBOMData = [{ ITEM: "5", BOM_QTY: "2" }];

  /* Creating the BOM Rev */
  const oBOMRevRec = record.create({
    type: record.Type.BOM_REVISION,
    isDynamic: true,
  });

  oBOMRevRec.setValue({
    fieldId: "name",
    value: "BOM Revision 1",
  });

  oBOMRevRec.setValue({
    fieldId: "effectivestartdate",
    value: new Date(),
  });

  oBOMRevRec.setValue({
    fieldId: "billofmaterials",
    value: sBOMRecId,
  });

  /* Inserting New Lines */
  pEngBOMData.forEach((oBOMLine) => {
    oBOMRevRec.insertLine({
      sublistId: "component",
      line: 0,
    });

    oBOMRevRec.setCurrentSublistValue({
      sublistId: "component",
      fieldId: "item",
      value: oBOMLine.ITEM,
    });

    oBOMRevRec.setCurrentSublistValue({
      sublistId: "component",
      fieldId: "bomquantity",
      value: Number(oBOMLine.BOM_QTY),
    });

    oBOMRevRec.commitLine({
      sublistId: "component",
    });
  });

  oBOMRevRec.save({
    enableSourcing: true,
    ignoreMandatoryFields: true,
  });
});
you can try using the bom default value, but I wouldnt expect that to give very different results
a
The item/component you are using is only in the subsidiary you are setting in the BOM?
My code is working all the way to adding that component/item to the BOM Revision
component
sublist, if my component is only available in Subsidiary A(Not the top most/parent subsidiary) and that subsidiary is being set when creating the BOM, then I can't select that Item in the BOM Revision...
If your Item [{ ITEM: "5", BOM_QTY: "2" }] is available in your Subsidiary '1' it will work, I don't have any problem with that scenario...
b
thats not how your code is setup
includechildren
set to true means that the children of your subsidiary are also included
and the components must be available in each child
a
@battk When you create a BOM you don't even need to set the subsidiary is not a mandatory field it will be set by default to the top-most/parent subsidiary. Then when you create a BOM Revision in the UI the BOM Revision inherit that Subsidiary from the BOM. When you do that in code (creating the BOM) and you do set the subsidiary to a CHILD subsidiary and then you create the BOM Revision and the BOM revision does not inherit the CHILD subsidiary you set or any at all(not sure because the Subsidiary is not even present in the BOM Revision record), then when you try to add an Item from the CHILD subsidiary is not there, not available, only Items from the top-most subsidiary are available to be selected in the BOM Revision Component sublist...
It does not matter what subsidiary you set at the BOM or which setting, the BOM Revision is not inheriting those values.
b
share code on how subsidiary 8 is created
a
Another crazy behavior (steps to reproduce): • Manually create a BOM and set the top most subsidiary. • Do not create any BOM Revision yet. • Edit the BOM, change/update the Subsidiary to a CHILD subsidiary. • Try to create a BOM Revision clicking
Create New Revision
from the BOM record and you will see how the BOM Revision only show the items from the top-most subsidiary.
Subsidiary 8 is just a Child subsidiary of the top most subsidiary.
b
Copy code
require(["N/record"], function (record) {
  const oBOMRec = record.create({
    type: record.Type.BOM,
    isDynamic: true,
  });

  oBOMRec.setValue({
    fieldId: "name",
    value: "BOM " + Date.now(),
  });
  oBOMRec.setValue({
    fieldId: "subsidiary",
    value: "10",
  });
  oBOMRec.setValue({
    fieldId: "includechildren",
    value: true,
  });
  oBOMRec.setValue({
    fieldId: "usecomponentyield",
    value: true,
  });
  oBOMRec.setValue({
    fieldId: "availableforalllocations",
    value: true,
  });

  const sBOMRecId = oBOMRec.save({
    enableSourcing: true,
    ignoreMandatoryFields: true,
  });
  var pEngBOMData = [{ ITEM: "306", BOM_QTY: "2" }];

  /* Creating the BOM Rev */
  const oBOMRevRec = record.create({
    type: record.Type.BOM_REVISION,
    isDynamic: true,
  });

  oBOMRevRec.setValue({
    fieldId: "name",
    value: "BOM Revision 1",
  });

  oBOMRevRec.setValue({
    fieldId: "effectivestartdate",
    value: new Date(),
  });

  oBOMRevRec.setValue({
    fieldId: "billofmaterials",
    value: sBOMRecId,
  });

  /* Inserting New Lines */
  pEngBOMData.forEach((oBOMLine) => {
    oBOMRevRec.insertLine({
      sublistId: "component",
      line: 0,
    });

    oBOMRevRec.setCurrentSublistValue({
      sublistId: "component",
      fieldId: "item",
      value: oBOMLine.ITEM,
    });

    oBOMRevRec.setCurrentSublistValue({
      sublistId: "component",
      fieldId: "bomquantity",
      value: Number(oBOMLine.BOM_QTY),
    });

    oBOMRevRec.commitLine({
      sublistId: "component",
    });
  });

  oBOMRevRec.save({
    enableSourcing: true,
    ignoreMandatoryFields: true,
  });
});
still works for me
though i made a change to the name since i got tired of the uniqueness warning
a
What is the subsidiary setting of this item: [{ ITEM: "306", BOM_QTY: "2" }];
???
b
only set to subsidiary with internal id 10
a
At the Item record level what is the subsidiary setting/value on that Item?
@battk Are you running this code from the console or server side?
b
server
it works client too