Hi I am trying to write a script that accepts a JS...
# suitescript
c
Hi I am trying to write a script that accepts a JSON Request to create a vendor and an address. I have successfully got the vendor, when I try to create an address I get "..........................error.SuiteScriptError\",\"name\":\"USER_ERROR\",\"message\":\"Please enter value(s) for: Address\",\"stack\":[\"Error\n.........." In the script below I am hardcoding the string values because I just want to get an address successfully created. Eventually it will be passed along in the JSON Object. I am racking my brain since every stack overflow article I have read is very similar.
Copy code
Here is the specific code within the POST Request Function Below.     Without the address lines the script works just fine.

    // Set Address Fields
    rec.selectNewLine({ sublistId: 'addressbook' });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'defaultbilling', value: true });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'attention', value: "data.attention" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'addressee', value: "data.addressee" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'addr1', value: "data.addr1" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'city', value: "data.city" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'state', value: "data.state" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'country', value: "US" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'zip', value: "12345" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'label', value: "Test Address" });
    rec.commitLine({ sublistId: 'addressbook' });

    let recordId = rec.save();
Copy code
Entire Function


// (POST method - create a NetSuite record)
  function post(context) {
    doValidation([context.recordtype], ["recordtype"], "POST");

    // Define a mapping of context field names to NetSuite field names
    const fieldMap = {
      paymentType: "custentity9",
      vendorClassification: "custentity_vendor_classification",
      vendorSmallBusinessSublist: "custentity_sb_sublist",
      naicsCode: "custentity_naics_code",
      paymentEmail: "custentity_2663_email_address_notif",
    };

    let rec = record.create({
      type: context.recordtype,
      isDynamic: true,
    });

    for (let fldName in context) {
      if (context.hasOwnProperty(fldName)) {
        // Use the mapped field name if available, otherwise use the original field name
        const fieldId = fieldMap[fldName] || fldName;
        if (fieldId !== "recordtype" && fieldId !== 'addressbook') {
          let field = rec.getField({ fieldId: fieldId }); //gets field information to determine if it's a select field (dropdown)
          if (!isNullOrEmpty(context[fldName]) && field.type === "select") {
            let selectOptions = field.getSelectOptions({
              filter: context[fldName],
              operator: "is",
            });
            let selectValue =
              selectOptions.length > 0
                ? selectOptions[0].value
                : context[fldName];
            rec.setValue({
              fieldId: fieldId,
              value: selectValue,
            });
          } else {
            rec.setValue({
              fieldId: fieldId,
              value: context[fldName],
              isDynamic: fieldId.indexOf("cust") === 0,
            });
          }
        }
      }
    }
    
    // Set Address Fields
    rec.selectNewLine({ sublistId: 'addressbook' });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'defaultbilling', value: true });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'attention', value: "data.attention" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'addressee', value: "data.addressee" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'addr1', value: "data.addr1" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'city', value: "data.city" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'state', value: "data.state" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'country', value: "US" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'zip', value: "12345" });
    rec.setCurrentSublistValue({ sublistId: 'addressbook', fieldId: 'label', value: "Test Address" });
    rec.commitLine({ sublistId: 'addressbook' });
  
    let recordId = rec.save();

    let entityId = search.lookupFields({
      type: search.Type.VENDOR,
      id: recordId,
      columns: ["entityid"],
    }).entityid;

    //returning the response
    return {
      success: true,
      nsInternalId: recordId,
      entityId: entityId,
      errorMessage: "",
    };
  }
h
I'd try "selectLine" instead of "selectNewLine". And we need a line index as well.
e
c
ok thanks I'll try that. I thought I tried this at one point but I never tried it while hardcoding the values so the error could have been with the JSON and how I was processing it
I think it may have to do with the country
since i wasn't setting it first
🙌 1
THANK YOU
I got it to work. I probably even looked over this documentation but didn't read those first comments where it says you need to set the country first
👍 1
Adding to this does anyone have a trick to return the internal id of the address being created? Currently I have a search that is returning the Summary MAX of {addressinternalid}, but the problem is that NetSuite isn't assigning the highest Internal ID to the most recent record. It makes big jumps and then goes back and fills in the gaps. I also checked a save search of Address Fields {address.internalid} and that is also out of order. Label 2 Address - 319263 Label 3 Address - 319463 <---Highest Internal ID Label 4 Address - 319264 <---Most recently created My solution right now is to have the entry system assign an external ID when it sends over the address
The external ID solution proved to be more difficult since the save search would show multiple "addressinternalids" even if the external ID wasn't assigned to it. My current solution is to do search on the vendor record for all of the {addressinternalids}, push them to an array and then return the last one added. So far this has been working with no issues even when the internalid is out of order.
s
NS makes no guarantees about the order of internal ids AFAIK, so seeing slightly 'out of order' ids is as designed.
Also, writing a function to cleverly set other fields is clever but harder to maintain, and in my experience usually not even much of a code savings over simple assigments e.g. record.foo = input.foo, etc.