Custom Value Formatting

Intro

3B Docs allows you to build a library of “Value Formatters” – these are special functions built with JavaScript that let you apply a specific formatting to a merge tag that are stored in the Value Formatter object table.

Usage

You can use Value Formatters in order to adjust, change, mutate and transform values returned by merge tags. Here are some examples of how you can employ the power of Value Formatters:

-       Depending on the currently viewing user’s browser locale settings, change a #{Contact.Id:CreatedDate} merge tag to return a date in dd/mm/yyyy or mm/dd/yyyy

-       Change a decimal with higher resolution to a lower resolution (round a decimal)

-       Conditionally display a value, for example if the currently viewing user’s browser declares their location to be outside of your country, you may want to not show the return value of the merge field.

-       Translations and internationalisation – you could build maps of translations for specific strings, say if a field from salesforce returns “Hello”, you may want to transform this to “Hola”, “Здрасти“, “Pronto”, etc. again, depending on the currently viewing user’s browser locale.

-       Offset Dates/Times to the timezone of the currently viewing user’s browser reported timezone

How To

In order to add a custom value formatter, you need to create a Value Formatter record, which you can access from the 3B Docs Application (or simply search for “Value Formatters” in the app launcher drop down).

When you have navigated to the object list view, you can click on the “New” button which will display the new record form. What is important here is that you give the Value Formatter a unique name that has no spaces or special characters. Don’t worry about enforcing this rule yourself, we have built a salesforce validation rule to make sure that the name of the Value Formatter is compliant with 3B Docs.

To use the newly created Value Formatter with your template, you need to navigate to a Template record, load the editor, add a merge tag and click on the merge tag.

 
Adding Value Formatter


From the options list, select the value formatter that you want to apply to the merge tag. The merge tag will be transformed from say {Contact.Id:CreatedDate}to #{Contact.Id:CreatedDate>EU_Date_Format} . This arrow right chevron indicates that the merge tag is to be formatted by the Value Formatter with the same name immediately after the chevron.

Syntax

When you create a value formatter, you are basically creating a JavaScript function that accepts a single parameter “value”. This JavaScript function has access to the global context and thus to the user’s navigator object, the application variables & collections and much more. This parameter “value” is the value returned by the merge tag embedded on the template.

Note: You must always return a string value – this return value will be your “transformation” of the merge tag.

Examples

Here are some examples to get you started with the basic syntax used by 3B Docs for Value Formatters:

Format Date Time to EU Format Date

This example is useful for formatting “CreatedDate” to a dd/mm/yyyy format. By default, salesforce returns a date-time value in the format YYYY-MM-DDTHH:MM:SS.ZZZ. This formatter will return the date-time in date only formatted to  dd/mm/yyyy:

Version < 1.16

value => new Date(value).toLocaleDateString(en-GB);

Version > 1.16

//Option 1
(args) => args[0].toLocalDateString('en-GB')
//Option 2
(args) => {
    const value = args[0];
    const callout = args[1];
    return value..toLocalDateString('en-GB')
}

Tips & Tricks

Here are some tips and tricks for using Value Formatters:

Locale, Timezone & Language

Always remember that you have access to the currently viewing user’s navigator which can help you determine their locale, timezone and language. Use this in order to internationalize a template!

//Get Timezone
Intl.DateTimeFormat().resolvedOptions().timeZone;

//Get Locale/Language
navigator.language

Geolocation

You can request the currently viewing user to grant you access to their location via using the native JavaScript APIs for geolocation requesting.

//Get User Location (request it)
if (window.navigator.geolocation) {
    window.navigator.geolocation.getCurrentPosition(console.log, console.log);
}

API Callouts

You can use Value Formatters just like Function Expressions to connect to external systems (or APEX). In the example below, we are calling the apex class b3d.StandardFunctionsRouter and its method getVersionData, the response is parsed to return an embed element with the image. This has been applied to a ContentVersion.Id field as a value formatter.

async (args) => {
	console.log('args', args)
	const value = args[0];
	const callout = args[1];
	
	try{
		await callout({
			endp: 'getVersionData',
			contentVersionId: value
		},
		false, 
		'b3d.StandardFunctionsRouter').then(response => {
			console.log('callout response from function expression: ', response);
			let type = '';
			if(response.responseObject.contentVersion.FileType == 'JPEG'){
				type = 'image/jpeg';
			}
			return `
				<embed type="${type}" src="${response.responseObject.data}"></embed>
			`;
		});
	}catch(err){
		console.error(err);
	}
}
 
Full Calendar JS library