Question: I have an array of US state values: cons...
# suitescript
m
Question: I have an array of US state values: const states = { AL: ‘Alabama’, AK: ‘Alaska’ , AS: ‘American Samoa’, AZ: ‘Arizona’, AR: ‘Arkansas’, CA: ‘California’ I need to set a custom list field of states on a customer. If I get the value of the customer shipping state, like AZ, how do I set the custom field text to ‘Arizona’ using the array to look it up?
e
Copy code
const states = {
 AL: ‘Alabama’,
 AK: ‘Alaska’ ,
 AS: ‘American Samoa’,
 AZ: ‘Arizona’,
 AR: ‘Arkansas’,
 CA: ‘California’,
  ...
};
is an Object, not an Array, and the distinction is important. If you have
'AZ'
, then you would get
'Arizona'
with
states['AZ']
.
m
Hey Eric! That would be what I want. So in: rec.setText({ How do I confugure this?
Is it: var shippingState = subrec.getValue({ fieldId: ‘state’ }); rec.setText({ fieldId: ‘someid’, value: states.shippingState }); I’m kinda stuck on this concept.
b
m
@battk Thanks! That worked like a charm after I got the hang of it: const state = {}; state[‘AL’] = ‘Alabama’; state[‘AK’] = ‘Alaska’ ; state[‘AS’] = ‘American Samoa’; state[‘AZ’] = ‘Arizona’; state[‘AR’] = ‘Arkansas’; state[‘CA’] = ‘California’; state[‘CO’] = ‘Colorado’; state[‘CT’] = ‘Connecticut’; state[‘DE’] = ‘Delaware’; state[‘DC’] = ‘District of Columbia’; state[‘FL’] = ‘Florida’; state[‘GA’] = ‘Georgia’; state[‘GU’] = ‘Guam’; state[‘HI’] = ‘Hawaii’; state[‘ID’] = ‘Idaho’; state[‘IL’] = ‘Illinois’; state[‘IN’] = ‘Indiana’; state[‘IA’] = ‘Iowa’; state[‘KS’] = ‘Kansas’; state[‘KY’] = ‘Kentucky’; state[‘LA’] = ‘Louisiana’; state[‘ME’] = ‘Maine’; state[‘MD’] = ‘Maryland’; state[‘MA’] = ‘Massachusetts’; state[‘MI’] = ‘Michigan’; state[‘MN’] = ‘Minnesota’; state[‘MS’] = ‘Mississippi’; state[‘MO’] = ‘Missouri’; state[‘MT’] = ‘Montana’; state[‘NE’] = ‘Nebraska’; state[‘NV’] = ‘Nevada’; state[‘NH’] = ‘New Hampshire’; state[‘NJ’] = ‘New Jersey’; state[‘NM’] = ‘New Mexico’; state[‘NY’] = ‘New York’; state[‘NC’] = ‘North Carolina’; state[‘ND’] = ‘North Dakota’; state[‘OH’] = ‘Ohio’; state[‘OK’] = ‘Oklahoma’; state[‘OR’] = ‘Oregon’; state[‘PA’] = ‘Pennsylvania’; state[‘PR’] = ‘Puerto Rico’; state[‘RI’] = ‘Rhode Island’; state[‘SC’] = ‘South Carolina’; state[‘SD’] = ‘South Dakota’; state[‘TN’] = ‘Tennessee’; state[‘TX’] = ‘Texas’; state[‘UT’] = ‘Utah’; state[‘VT’] = ‘Vermont’; state[‘VI’] = ‘Virgin Islands’; state[‘VA’] = ‘Virginia’; state[‘WA’] = ‘Washington’; state[‘WV’] = ‘West Virginia’; state[‘WI’] = ‘Wisconsin’; state[‘WY’] = ‘Wyoming’; var shippingState = subrec.getValue({ fieldId: ‘state’ }); var myState = state[shippingState]; log.debug({ title: shippingZip + ' ' + shippingState + ' ' + myState }); contactNewRecord.setText({ fieldId: ‘custentity_cust_us_state’, text: myState });
n
Curious, why didn't you just define your state constant directly rather than that verbose way? i.e.
Copy code
const states = {
 AL: 'Alabama',
 AK: 'Alaska' ,
etc
m
@NElliott I did that based on the article battk sent me.
🤭 1
b
it was the documentation on how to access properties, not how you should initialize it
the demo demonstrated both ways of setting up an object, so that you could see that both the different methods of access work
m
Thanks again @battk! The reason for this was the company wanted a custom search filter that eliminated all countries except the US for states and wanted to use the long state name. This script helped in accomplishing that.