EONE BLOG

Tech Tuesday: Useful Javascript Commands


With the launch of SmartConnect.com, another scripting language has been introduced for users to create new calculated columns and scripts. We’re going to take a quick look at a couple useful scripts and commands that will assist in building out integrations.

For these examples I’ll describe what each script should help with, then list out what the source field names would be for each example, and finally I’ll show the actual syntax that would be used to build each.

A quick note if you are copying the scripts out from this article: Double-check any of the single quotes in the scripts, word editors and blog sites like to format the single quotes to make them easier to read but it technically makes it a different character in the script compiler. I would copy these scripts out to notepad and re-type the single quotes to ensure they show up as a basic single quote without formatting.

 

Let’s start with a straightforward scenario. We have a first name and a last name in the source data and we need to concatenate it into 1 field for the destination. I will be assuming our column names are FirstName and LastName for this script. The syntax would be as follows:

return this._FirstName.trim() + ‘ ‘ + this._LastName.trim();

The code will take the first name and last name columns and apply a trim statement to each. The trim statement removes any extra whitespace characters before or after the actual data. The single quotes in the middle of the statement are there to ensure a space is added between the first and last name when building the new string.

 

Another good example is needing to come up with the current date and/or time value. A common scenario this is used for it to create a batch ID that has a datetime stamp as part of the value. In this case, I won’t be using anything from the source data, instead I want the beginning of the batch to have the value “EONE_” and then the date to follow in year-month-day format (ie: EONE_2019-02-19):

var today = new Date();

var day = today.getDate();

var month = today.getMonth();

var year = today.getFullYear();

 

month = month + 1;

 

if (day < 10) {

     day = ‘0’ + day;

};

 

if (month < 10) {

     month = ‘0’ + month;

};

 

var myBatch = ‘EONE_’ + year + ‘-‘ + month + ‘-‘ + day;

 

return myBatch;

There is a bit going on with this script so we’ll walk through it. The first 4 lines are declaring the variable for the current date, and then getting each individual component of it (day, month, year). After that, the month has to be increased by 1, as the Javascript getMonth() function returns a 0-11 for the month – which isn’t how humans number their months.

After that, both the month and day values are checked to see if they are less than 10. If they are, the value needs to have a 0 padded to the front of it to keep it at 2 characters so that our batch ID is formatted correctly (ie, padding a 0 to 7, to make the value 07).

Finally, all the components are concatenated together to build the value that will be returned and mapped through to the destination.

 

Each of those examples and the code included can be taken and adjusted as needed for real world integrations and problems that need to be solved. The Javascript used within the scripting windows in SmartConnect.com will match standard syntax that can be built in any editors. Along that point, using online Javascript editors to test out different statements and code snippets is extremely helpful. There are multiple options out there to try, but one that I personally make use of is: https://playcode.io/online-javascript-editor

 

If you have other scripting scenarios you’d like to cover or have questions related to the examples above, comment below and we’ll reply and help out – either through the comments below or in another Tech Tuesday. Thanks!

 

 

 

Leave a Comment





RECENT POSTS


Free Samples (ie Sample Data!) with Popdock
The Pick Popdock Papers, Part III: Know Your Audience
Employee Spotlight: Jess Green, Partner Development Manager
Get Creative With Your Dynamics GP Data
Directions EMEA - SmartConnect Bootcamp in Vienna, Austria - Don't Miss It!

POPULAR POSTS


2016 SmartConnect Integration Bootcamps
Tech Tues: Automatically refreshing Pivot Tables in Excel Refreshable Reports
Happy Thanksgiving from the eOne team!
New Releases of Extender and SmartList Builder
2017 SmartConnect Integration Bootcamps

CATEGORIES

ARCHIVES

open all | close all

TAGS

Business Central CRM Dynamics 365 dynamics crm Dynamics GP Dynamics NAV Econnect Employee Spotlight eone eOne News eOne Spotlight Events Excel Excel Report Builder Extender Extender Enterprise Flexicoder GP GPUG integration integration manager Meet the Team Microsoft dynamics crm Microsoft Dynamics GP Office Relationships partners Popdock release Reporting SalesForce SalesForce.com scripting SmartConnect SmartConnect.com SmartConnect Office Hours SmartList SmartList Builder SmartList Designer SmartView SQL Support Tech Tuesday Templates training Zendesk

Integrate & Automate without Any Code.

SmartList Data has Never Been Faster.

The Easiest Way to Report on GP Data.