fbpx

One of the projects I am working on right now involves building solutions in Salesforce Commerce Cloud. Salesforce Commerce Cloud can be a little intimidating at first glance, however once you some time with it, it’s not as difficult as it first seems, especially for someone with a Java background. In this post, I will shed some light on Salesforce Commerce Cloud, talk about its data structures and finally give an example of sort a list Salesforce Commerce Cloud.

Let’s get started with a little background…

Salesforce Commerce Cloud (SFCC)

SFCC is huge and I can easily write a few lengthy posts on it, however for sake of brevity, I will limit the SFCC description in this post.

SFCC or Salesforce B2C Commerce provides a set of tools and resources for your ecommerce sites which they define as a storefront. It exposes a set of API’s that you can program to customise your storefront. Storefront in the SFCC terminology is your e-commerce site. Using SFCC you can build one or more ecommerce sites in you realm.

Sounds good but what’s a realm?

If you read the SFCC official docs, a realm consists of instances that you can use to develop, test and deploy your online storefront. A realm is composed of three layers namely, Primary Instance group, Secondary Instance group and Commerce Cloud. Primary instance group consists of the development, staging and production environments. Secondary instance group has development sandboxes i.e. environments for developers to test their code. Finally, Commerce Cloud is the infrastructure composed of Application Servers, Web Servers, Database servers, Storage. Commerce Cloud can be thought of as Google Cloud Platform, Amazon Web Services or Microsoft Azure. It is similar but not quite, I will talk about it in more detail in another post.

SFCC API

The underlying backend that SFCC is built with is Java however developers can extend it or program it using Javascript.

Regarding the backend being built in Java, I realised that as soon as I started working with it. After seeing some of the error messages, stack trace, documentation etc I figured this has to have been done in Java. My theory is, the reason they used Javascript as the language to extend it is due to Javascript’s popularity. I mean, Javascript may seem less intimidating than Java, hence web developers are likely to feel very comfortable with it.

Data types & Data structures

SFCC data structures are the other aspect of the language that should seem familiar if you come from a Java background.

  • Array: An array to store a list of items
  • Collection: The Super class with a number of implementations e.g.
    • ArrayList
    • List
    • HashSet
    • LinkedHashSet
    • SortedMap
    • etc
  • Number: self-explanatory data type
  • String: self-explanatory data type
  • StringWriter: This one’s like the Java StringBuilder or StringBuffer, why use it? because Strings are immutable and you can build a large string by appending to an instance of a StringWriter.
  • Utils classes: Utility classes such as StringUtils provide helper functions which provide shortcuts to accomplish various tasks

If you need to know more about SFCC, please check out their official documentation here. Please remember, their document is a jsp so you cannot bookmark a specific class name or such. Hence you will just need to search for what you are looking by typing into your search text box.

How to create list in Salesforce Commerce Cloud

Before sorting a list of strings let’s create a list of strings.

//import the relevant

var ArrayList = require("dw/util/ArrayList");

//create a list with some data

var unsortedList = new ArrayList();

unsortedList.add1({name: "Jack"});

unsortedList.add1({name: "Ben"});

unsortedList.add1({name: "Rajesh"});

In the above code,

  • We first import an ArrayList similar to import java.util.ArrayList in Java – var ArrayList = require(“dw/util/ArrayList”)
  • Then create a new variable of type ArrayList – var unsortedList = new ArrayList() – this is similar to earlier Java versions e.g. pre Java 5 or 1.5
  • Then we add a bunch of values (objects) to it – unsortedList.add1({name: “c”}) – this is something unique to Javascript.

How to sort a list in Salesforce Commerce Cloud

To sort a collection of strings, we will use the PropertyComparator class. We can sort it in a similar way

//sort the Collection in place by name in
//ascending order

var PropertyComparator = require("dw/util/PropertyComparator");

var comparator = new PropertyComparator("name", true);

unsortedList.sort(comparator);

In the code above

  • we import the PropertyComparator class – var PropertyComparator = require(“dw/util/PropertyComparator”) – see? it’s similar to how we would do this in Java using java.util.Comparator
  • declare a property comparator variable to sort the list by the name property – var comparator = new PropertyComparator(“name”, true); – but default it sorts in ascending order by default. However if you create a new PropertyComparator like this var comparator = new PropertyComparator(“-name”, true);, then it would sort it in descending order
  • then we call the sort method of the ArrayList and pass it the Comparator – unsortedList.sort(comparator); – Sort (PropertyComparator) method is defined in the Collections superclass

Summary

This post served an introduction to SFCC and it’s underlying infrastructure followed by a quick look at some of the data structures. Then we move onto the main goal of this post; show code samples of how to create and sort a list in in ascending and descending order in SFCC.

This was an introductory post on Salesforce Commerce Cloud (SFCC)

Like the blog? Subscribe for updates

As usual, if you find any of my posts useful support me by  buying or even trying one of my apps on the App Store. 

https://mydaytodo.com/apps/

Also, if you can leave a review on the App Store or Google Play Store, that would help too.

Categories: Algorithms

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *