trying to add ship and bill subrecord but getting ...
# suitescript
r
trying to add ship and bill subrecord but getting this error - userevent - aftersubmit - above is the code that is not working
e
Scroll further to the right on your error message and look for the specific line number from your code module.
r
record.save() is the line number
I am saving the record at that line that they have mentioned
e
that means that the data you're trying to save is not being accepted or you're missing a value that it needs.
paste that error message as text here
r
Copy code
{
   type: "error.SuiteScriptError",
   name: "UNEXPECTED_ERROR",
   message: "An unexpected SuiteScript error has occurred",
   stack: [
      "Error\n    at RecordInvoker.save (suitescript/resources/javascript/record/serverRecordService.js:371:13)\n    at NetSuiteObject.thenableFunction() (suitescript/resources/javascript/record/proxy.js:115:24)\n    at Object.afterSubmit (/SuiteScripts/GBS_UE_setCustomerAndPriceLevel.js:271:20)"
   ],
   cause: {
      type: "internal error",
      code: "UNEXPECTED_ERROR",
      details: "An unexpected SuiteScript error has occurred",
      userEvent: null,
      stackTrace: [
         "Error\n    at RecordInvoker.save (suitescript/resources/javascript/record/serverRecordService.js:371:13)\n    at NetSuiteObject.thenableFunction() (suitescript/resources/javascript/record/proxy.js:115:24)\n    at Object.afterSubmit (/SuiteScripts/GBS_UE_setCustomerAndPriceLevel.js:271:20)"
      ],
      notifyOff: false
   },
   id: "",
   notifyOff: false,
   userFacing: true
}
what it could be?
i am doing everything
b
too much code, remove everything
only set the country
hardcode that country
code like
Copy code
billaddrSubrecord.setValue({
        fieldId: "country",
        value: billCountry || ""
      });
doesnt mean much to us
you could be getting an unexpected error because you are unsetting the country
or perhaps because you are setting it an invalid value
its too wide a range to guess
r
I did
message has been deleted
still it persists
message has been deleted
even removed set Billing address
setting only shipping that too country
b
Afghanistan should be a valid country
does your script do anything else?
or does it only load the record, set the country of the shipping address, and then save
r
no.. it does alot of other things
but when i introduce this specific code it fails
dont know why
same error
message has been deleted
message has been deleted
same to same
b
same thing
start simpler
load a record, set the shipping addresses country
save it
r
okay
wow
this is crazy
I removed everything!
still the same error
b
what does the code look like now?
r
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */

define(["N/record", "N/search"], function (record, search) {
  function afterSubmit(context) {
    try {
      var recordLoadsoid = context.newRecord;

      var recordId = recordLoadsoid.id;

      log.debug({
        title: "recordId",
        details: recordId
      });

      var recordLoadso = record.load({
        type: record.Type.SALES_ORDER,
        id: recordId
      });

      var {
        itemLineCount,
        getShopifyCustomer,
        getCustomerShipAddress,
        getCustomerShipTo,
        getCustomerBillTo,
        getCustomerBillAddress
      } = getExistingRecordData(recordLoadso);

      if (
        getShopifyCustomer == 1793051 ||
        getShopifyCustomer == "1793051" ||
        getShopifyCustomer == 1775805 ||
        getShopifyCustomer == "1775805"
      ) {
        var {
          shipCountry,
          shipisresidential,
          shipattention,
          shipaddressee,
          shipaddrphone,
          shipaddr1,
          shipaddr2,
          shipcity,
          shipstate,
          shipzip
        } = getShipData(recordLoadso);

        if (getShopifyCustomer == 1793051 || getShopifyCustomer == "1793051") {
          setShip(
            recordLoadso,
            shipCountry,
            shipisresidential,
            shipattention,
            shipaddressee,
            shipaddrphone,
            shipaddr1,
            shipaddr2,
            shipcity,
            shipstate,
            shipzip,
            getCustomerShipTo
          );
        }

        recordLoadso.save();
      }
    } catch (e) {
      log.debug({
        title: "e",
        details: e
      });
    }
  }

  function setShip(
    recordLoadso,
    shipCountry,
    shipisresidential,
    shipattention,
    shipaddressee,
    shipaddrphone,
    shipaddr1,
    shipaddr2,
    shipcity,
    shipstate,
    shipzip,
    getCustomerShipTo
  ) {
    try {
      recordLoadso.setValue({
        fieldId: "shipaddresslist",
        value: -2
      });

      var shipaddrSubrecord = recordLoadso.getSubrecord({
        fieldId: "shippingaddress"
      });

      log.debug("shipCountry", shipCountry);
      shipaddrSubrecord.setValue({
        fieldId: "country",
        value: shipCountry
      });

      log.debug(
        "shipCountry",
        shipaddrSubrecord.getValue({
          fieldId: "country"
        })
      );
    } catch (err) {
      log.debug({
        title: "errr in setship",
        details: err
      });
    }
  }

  function getShipData(recordLoadso) {
    try {
      var shipaddrSubrecord = recordLoadso.getSubrecord({
        fieldId: "shippingaddress"
      });
      return {
        shipCountry: shipaddrSubrecord.getValue({
          fieldId: "country"
        }),
        shipisresidential: shipaddrSubrecord.getValue({
          fieldId: "isresidential"
        }),
        shipattention: shipaddrSubrecord.getValue({
          fieldId: "attention"
        }),
        shipaddressee: shipaddrSubrecord.getValue({
          fieldId: "addressee"
        }),
        shipaddrphone: shipaddrSubrecord.getValue({
          fieldId: "addrphone"
        }),
        shipaddr1: shipaddrSubrecord.getValue({
          fieldId: "addr1"
        }),
        shipaddr2: shipaddrSubrecord.getValue({
          fieldId: "addr2"
        }),
        shipcity: shipaddrSubrecord.getValue({
          fieldId: "city"
        }),
        shipstate: shipaddrSubrecord.getValue({
          fieldId: "state"
        }),
        shipzip: shipaddrSubrecord.getValue({
          fieldId: "zip"
        })
      };
    } catch (err) {
      log.debug({
        title: "err in get ship data",
        details: err
      });
    }
  }

  return {
    afterSubmit: afterSubmit
  };
});
nothing, i do
b
thats still a lot of code to set a field
r
Copy code
/**
 *@NApiVersion 2.1
 *@NScriptType UserEventScript
 */

define(["N/record", "N/search"], function (record, search) {
  function afterSubmit(context) {
    try {
      var recordLoadsoid = context.newRecord;

      var recordId = recordLoadsoid.id;

      log.debug({
        title: "recordId",
        details: recordId
      });

      var recordLoadso = record.load({
        type: record.Type.SALES_ORDER,
        id: recordId
      });

      setShip(recordLoadso);

      recordLoadso.save();
    } catch (e) {
      log.debug({
        title: "e",
        details: e
      });
    }
  }

  function setShip(recordLoadso) {
    try {
      var shipaddrSubrecord = recordLoadso.getSubrecord({
        fieldId: "shippingaddress"
      });

      shipaddrSubrecord.setValue({
        fieldId: "country",
        value: "US"
      });

      log.debug(
        "shipCountry",
        shipaddrSubrecord.getValue({
          fieldId: "country"
        })
      );
    } catch (err) {
      log.debug({
        title: "errr in setship",
        details: err
      });
    }
  }

  return {
    afterSubmit: afterSubmit
  };
});
nothing, nada
still the same
Oh my god!
I dont understand
its such a simple process
b
the code itself looks fine, it works in my account
r
it sets the field to custom but address does not change
it stays the same
there is no error now
but address is not changing
b
thats normal
its extra common for the ship to address text to not update from updating the ship address subrecord
the subrecord itself should have your fields set
r
why is it setting here and not there
i dont understand
thats wierd
weird*
will check
I am chainging the entity and doing this
shouldnt be an issue