I have a map reduce script that creates and sets t...
# suitescript
v
I have a map reduce script that creates and sets the fields on an address subrecord for a customer This is working mostly correctly other than the 'State' field.
Copy code
// If a new line was successfully selected
if (addressSublistLine !== -1) {
  // Get the subrecord for the selected sublist line
  var addressSubrecord = customerRecord.getCurrentSublistSubrecord({
    sublistId: 'addressbook',
    fieldId: 'addressbookaddress'
  });

  // Set the address details in the subrecord
  addressSubrecord.setValue({
    fieldId: 'country',
    value: 'AU'
  });
  addressSubrecord.setValue({
    fieldId: 'addr1',
    value: address1
  });
  addressSubrecord.setValue({
    fieldId: 'addr2',
    value: address2
  });
  addressSubrecord.setValue({
    fieldId: 'city',
    value: city
  });
   addressSubrecord.setValue({
    fieldId: 'zip',
    value: postCode
  });
  addressSubrecord.setValue({
    fieldId: 'state',
    value: state
  });

   addressSubrecord.setValue({
    fieldId: 'label',
    value: 'Default Shipping'
  });

//addressSubrecord.setCurrentSublistValue({
// sublistId: 'addressbook',
 //  fieldId: 'label',
 //  value: 'Shipping address'
 // });


  

  // Commit the sublist line
  customerRecord.commitLine({
    sublistId: 'addressbook'
  });


// Get the line number of the newly created line
var lineCount = customerRecord.getLineCount({ sublistId: 'addressbook' });
var lastLine = lineCount - 1;

// Select the last line in the addressbook sublist
customerRecord.selectLine({
  sublistId: 'addressbook',
  line: lastLine
});

// Set the label for the current line of the addressbook sublist
customerRecord.setCurrentSublistValue({
  sublistId: 'addressbook',
  fieldId: 'label',
  value: 'Shipping address'
});

 log.debug('lastLine',lastLine)

  // Commit the sublist line
  customerRecord.commitLine({
    sublistId: 'addressbook'
  });


  // Save the customer record
  customerRecord.save();
The image shows what the script returns. However, if you try to edit the address subrecord, it shows the State as blank NetSuite have filed this as a defect. I suspect this is more likely an issue with the script. Can anyone see what is wrong in the script?
b
what values are in your variables
v
I just ran it again right now. The debug log has: Lead ID: 8753, Address 1: 123 Wallaby Crescent, Address 2: , City: Sydney, State: New South Wales, Post Code: 4569 I have tried using the internal ID for the state variable instead of text and this is what it displays:
b
the internal id should be what you use to set a select field, share the id you are using
id also advise using a real address while testing
the address you showed doesnt look like it set 4569 as the zip code
s
Can depend on your account settings for how states are displayed (as NSW or New South Wales). I use a function to get AU states/territories, their internal id, abbreviation, and full name. Then it's just a matter of working out your preferences. But save to make sure you are using setValue({fieldId:"xxx",value:xxx) if using internal id, or setText({fieldId:"xxx", text:"xxx"}) if setting to the text value
Copy code
function getStates(countryCode) {
            var filters = [
                ["inactive", "is", "F"]
            ];

            if (countryCode) {
                filters.push("AND")
                filters.push(["country","anyof",countryCode])
            }

            var searchObject = search.create({
                type: "state",
                filters: filters,
                columns:
                    [
                        search.createColumn({name: "id", label: "Id"}),
                        search.createColumn({
                            name: "fullname",
                            sort: search.Sort.ASC,
                            label: "Full Name"
                        }),
                        search.createColumn({name: "shortname", label: "Short Name"}),
                        search.createColumn({
                            name: "country",
                            sort: search.Sort.ASC,
                            label: "Country"
                        }),
                        search.createColumn({name: "inactive", label: "Inactive"})
                    ]
            });

            return searchObject.run();
        }
👍 1
Check your general prefs:
Disallow free-form for safety and consistency
v
Thanks. These are the current settings
Sorry Battk. this was the address created from the script
The other one (with state of 401) was just to show that is what I get if I try to use State internal id instead of State name
I will try your suggestion Stefan thanks for that!
b
states arent set with internal id numbers
set the state on a record in the ui and then get its value to see what it should look like
v
Thank you Stefan! That worked I will need to test with the function to retrieve the abbreviation (for now, I just hard coded the value of the state to 'NSW') though I can see where my script was going wrong Thanks again