Accessing Custom Record Fields in SuiteCommerce

Access custom record fields within SuiteCommerce templates efficiently, enhancing data interaction through JavaScript and TypeScript.

·4 min read·View Oracle Docs

The Accessing Custom Record Fields functionality allows developers to efficiently interface with custom records in SuiteCommerce, enabling dynamic data rendering and enhanced user experiences.

Accessing Data

Each custom content type (CCT) view can access SuiteCommerce Advanced (SCA) data regarding items, item lists, products, or categories. These data points are accessible to the Document Object Model (DOM) through various SCA modules. Furthermore, each view is capable of returning values for custom record fields associated with the CMS Content Type records.

Important:

To expose this data to your templates, each view must implement the getContext() method.

Accessing SCA Data

If a custom content type requires data not tied to a custom record, you can utilize the contextDataRequest array property to access certain SCA objects. For optimal functionality, ensure that your view is set up to utilize the information that the following contextData objects provide. This assumes the information is already supplied in the DOM at the relevant location where the CCT is placed.

Context Data ObjectView (JavaScript SCA 2019.1 and Earlier)View (TypeScript SCA 2019.2 and Later)Description
categoryFacets.Browse.View.jsFacets.Browse.View.tsReturns data about the category being navigated
itemProductDetails.Base.View.jsProductDetails.Base.View.tsReturns data about the item on the Product Details page
itemlistFacets.Browse.View.jsFacets.Browse.View.tsReturns the list of items in the search results
productProductDetails.Base.View.jsProductDetails.Base.View.tsReturns data for the product on the Product Details page

Note:

When adding SCA content in an area using SMT Admin, the application will automatically execute the validateContextDataRequest method to ensure that all requested contexts are valid. In scenarios where optional data is requested and not available, this method might fail. To prevent this, configure your view to override the validateContextDataRequest method so it always returns true.

Example Usage

The ImageViewer CCT requests an item’s name. A simplified implementation of the CCT could be:

javascript
1//...,
2contextDataRequest: ['item'],
3validateContextDataRequest: "text-purple-400">function() {
4 "text-purple-400">return true;
5},
6getContext: "text-purple-400">function getContext() {
7 //...;
8 "text-purple-400">if ("text-purple-400">this.contextData.item) {
9 "text-purple-400">var item = "text-purple-400">this.contextData.item();
10 //...;
11 }
12 //...;
13}
14//...

Accessing Custom Record Fields

By default, when a template renders, it receives a context object that contains each property defined in the linked custom record within a settings object, where you indicate the fields by Field ID.

Example Fields in ImageViewer CCT:

Field IDDescription
custrecord_sc_cct_iv_valignSets the vertical alignment of a text object
custrecord_sc_cct_iv_textDeclares the text to display
custrecord_sc_cct_iv_imageurlSets the URL for an image

In the getContext() method for the ImageViewer CCT, you specify which fields from your custom record correlate with the settings. If the custrecord_sc_cct_iv_valign field requires options from a custom list, your code may look like this:

javascript
1//...
2getContext: "text-purple-400">function() {
3 "text-purple-400">var texts = [],
4 imageUrl = '',
5 valign = "text-purple-400">this.valign["text-purple-400">this.settings.custrecord_sc_cct_iv_valign] || "text-purple-400">this.valign['3'];
6
7 "text-purple-400">var set_text = Utils.trim("text-purple-400">this.settings.custrecord_sc_cct_iv_text),
8 set_texts = set_text ? set_text.split('\n') : [],
9 set_imageUrl = Utils.trim("text-purple-400">this.settings.custrecord_sc_cct_iv_imageurl);
10
11 texts = set_texts.length ? set_texts : texts;
12 imageUrl = set_imageUrl ? set_imageUrl : imageUrl;
13//...

Steps to Create the View:

  1. Create a New JavaScript File: In your CCT module's JavaScript directory, create new JavaScript or TypeScript files named appropriately. For example: ../SC.CCT.ImageViewer@0.0.1/JavaScript/SC.CCT.ImageViewer.View.js.

  2. Define Dependencies: Integrate necessary dependencies in the new view file using either JavaScript or TypeScript syntax, as seen in the previous examples.

  3. Build the View: Customize the view in line with your CCT's specifications before saving the view file.

By carefully implementing these steps, developers can effectively access and utilize custom record fields within SuiteCommerce environments, enhancing user engagement through customized templates.

Key Takeaways

  • Each CCT view can access SCA data and custom record fields through the getContext() method.
  • Utilize the contextDataRequest property for data access beyond custom record fields.
  • Override the validateContextDataRequest method to ensure flexibility with optional data requests.
  • Define custom fields within a settings object to tailor user experiences.

Source: This article is based on Oracle's official NetSuite documentation.

Frequently Asked Questions (4)

What is the purpose of the getContext() method in SuiteCommerce templates?
The getContext() method is used to expose data from SuiteCommerce templates by returning values for custom record fields associated with CMS Content Type records. This allows developers to dynamically render data within their templates.
Do I need to override the validateContextDataRequest method?
Yes, if you want to ensure flexibility with optional data requests that may not be available. Overriding this method to always return true can prevent failures when optional data is requested.
Which method should I use to access data from SCA modules for a CCT?
You should use the contextDataRequest property to access certain SCA objects for a custom content type if the data is not tied to a custom record.
How are custom record fields defined in SuiteCommerce templates?
Custom record fields are defined within a settings object in the template. Field IDs are used to access these fields, which allows customization of user experiences through tailored data displayed in the templates.
Source: Accessing Custom Record Fields Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →