sFiBmc

Templating

Templating of scripts

Template scripting offers several advantages in Salesforce Marketing Cloud (SFMC) development. Firstly, it allows developers to conceal sensitive information, such as API keys or credentials, when sharing code, like via Git. By templating scripts, these secrets can be stored separately and inserted only during deployment, minimizing the risk of exposing them unintentionally.

Additionally, templating facilitates the management of environment-specific configurations, enabling seamless transitions between development, testing, and production environments. Moreover, templating empowers developers to script reusable components or functionalities, akin to libraries, within their SFMC projects. This promotes code reusability, reduces redundancy, and enhances overall development efficiency. In essence, leveraging templating in SFMC development streamlines the development process, enhances security, and promotes code maintainability and scalability.

How to start?

Set dev-tokens and prod-tokens in .vscode/ssjs-setup.json, in case you need to fill anything in your scripts differently on the context. E.g.: Dev vs Production or in order not to commit to your Git repository.

Any key you set here can be used in your .ssjs file using {{ token-name }} syntax. It will be automatically assigned whenever you deploy your file. Mustache syntax is currently used.

Example:

<script runat=server>
	// ...
	var IS_PROD = {{IS_PROD}};
	var MC_SUBDOMAIN = "{{SUBDOMAIN}}";
	var CLIENT_ID = "{{CLIENT_ID}}";
	var CLIENT_SECRET = "{{CLIENT_SECRET}}";
	// ...
	if (IS_PROD) {
		var sfmc = new sfmcApi(MC_SUBDOMAIN, CLIENT_ID, CLIENT_SECRET);
	}
	// ...
</script>

.vscode/ssjs-setup.json

{
	//...
	"dev-tokens": {
		"IS_PROD": false,
		"MC_SUBDOMAIN": "mc-dummy-subdomain",
		"CLIENT_ID": "cl-id-12345",
		"CLIENT_ID": "cl-secret-98765"
	},
	"prod-tokens": {
	},
	//...
}

Library templating

This feature lets you easily include other file in your project into the script. So, if you want to share a piece of code - let’s call it a… library. ;)

But how?

  1. put your piece of code into a new file:
// file: ./myLib.js
function helloWorld() {
	Write("Hello World!");
}

Now, set a new templating token in the .vscode/ssjs-setup.json. It needs to reference your file using the: file://./path/to/file syntax.

{
	//...
	"dev-tokens": {
		"IS_PROD": false,
		"MY_LIB": "file://./myLib.js"
	},
	//...
}

Let’s include the library in your script file, like:

<script runat="server">
	Platform.Load("1.1.1");

	{{MY_LIB}}

	helloWorld();
</script>

And run as usual! No changes there!

But you might be asking, how about deploying to production? I’ve got you covered with a new command in the toolset: SSJS: Deploy to Production. At the moment, it gives you compiled code to Clipboard, but more advanced deployment is coming soon!