Post Featured Image

Add Custom Entity Field Data to the Profile Model

This blog post is appropriate for sites running versions of SuiteCommerce Advanced older than 2019.1. It is unnecessary for SuiteCommerce sites, and newer SuiteCommerce Advanced sites.

In the 2019.1 release of SuiteCommerce, we introduced the user profile component to the extensibility API. The prime use is to return an object of data about the current user; one of its big uses is to grab any custom field data associated with the user, which it surfaces automatically. If you're interested, you can read my blog post on taking advantage of it.

However, for you happy folk unable to use this component because of your code version, you may still be faced with the problem: how do I get all of the user's custom field data from NetSuite to the frontend?

The good news is that it is relatively easy. By taking a look at how the more modern code does this, you can replicate it in your code — and that's exactly what I'm going to do.

The Very Short Answer

At its absolute shortest, you can fetch customer entity custom fields by using the commerce API method getCustomFields() on the customer object.

After adding SC.Models.Init to your SuiteScript file, you can call it by doing something like this:

ModelsInit.customer.getCustomFields()

It will return an array of the custom fields associated with the user, each one an object with the name, value, and type of the field. It also includes some pre-generated HTML, but this is legacy and not very useful for you.

Note that you can also use the similarly named getCustomFieldValues() method instead. Materially speaking, there is little difference between them: they both have the important properties of the fields' names and values. However, the crucial benefit of using getCustomFields() is that it also returns the type of the field. If it is important to know whether you are returning a value from a text field or checkbox, for example, it is preferable to use getCustomFields().

The Slightly Longer Answer (Automatically Adding the Values to the Profile Model)

Most of the time I hear this request, the developer is working with the user's profile model and wants to access the custom fields in the same way that they access the standard fields. The trick, therefore, is to use this commerce API method and automatically add the field data to the profile model.

The good news is that this is also straightforward.

To illustrate this, I'm going to create a new customization folder like this:

UserCustomFields@1.0.0

  • SuiteScript
  • ns.package.json

In the SuiteScript folder, create UserCustomFields.js and in it put:

define('UserCustomFields'
, [
    'Profile.Model'
  , 'SC.Models.Init'
  ]
, function
  (
    ProfileModel
  , ModelsInit
  )
{
  'use strict';

  ProfileModel.get = _.wrap(ProfileModel.get, function (fn)
  {
    var profile = fn.apply(this, _.toArray(arguments).slice(1));
    profile.customfields = ModelsInit.customer.getCustomFields();

    return profile;
  });
});

What we're doing is modifying the get() call the profile model relies on to get its data. We're using the useful _.wrap() method. it works by calling the initial function and then modifying the result. For us, we're telling it to get the profile model object, and then add a new property for the custom fields. It then returns the freshly updated profile object with our new data. Nice and neat.

In order to get this on your site, you'll need to update the module's ns.package.json file:

{
  "gulp":
  {
    "ssp-libraries": ["SuiteScript/*.js"]
  }
}

And then update the distro.json so that the new module is included in the compilation process.

Update the modules object to include a path to your new module (eg "customizations/UserCustomFields": "1.0.0"), and also update the ssp-libraries : dependencies array to include the new file ("UserCustomFields").

Save and deploy.

The custom fields are automatically sent to the frontend profile model on a successful get(), so there is no additional work you need to do there.

You can test it by trying to access the fields in your customization, but you can quickly test it by typing the following into your browser's developer console:

require('Profile.Model').getInstance()

This will return the profile model singleton, which you can then inspect. In the attributes object, you should see your new customfields object.

Remember, you will need to be logged in or recognized in order to see values here.

Security

Just remember that when you do this, you are automatically adding the names and values of all your user custom fields to the profile model, which is accessible by the user. If you store any data about the user that you consider private, you will need to take steps to prevent this from happening. For example, before modifying/returning the profile object, you could check the customfields array for any you don't want to include and delete them (ie build a blacklist). Alternatively, after getting the custom fields back, you could process them and only include certain ones in the profile object (ie build a whitelist).

Troubleshooting

As a reminder, if you want to surface a custom entity field onto the frontend, you must make sure that it is configured to appear in the web store.

To do this, edit the field and ensure that the Applies > Web Store and Applies > Customer checkboxes are checked.