Chuck Grimmett

November 2017

Index

  • Adding Months to a Calculated Date in Salesforce

    Adding Months to a Calculated Date in Salesforce

    Adding dates is tricky. Months have different numbers of days, so you can’t rely on just adding 30 days to get an extra month. You also can’t just add a certain number of months because formulas in Salesforce don’t auto increment the year. The solution is modular arithmetic and conditionals.

    The goal here was to make a set of fields to send out emails on the first day of each month for 6 months, given a specific month to start with.

    What I’m doing here is:

    • Year: Figuring out the month number, adding one less than the number of months over all, dividing it by 12, and rounding down to add either a 0 or 1 to the year. You have to subtract one from the month because 12/12 = 1 and you don’t want December adding an extra year.
    • Month: If the resulting month is December, return 12. Otherwise return the month number modulo 12. (12 mod 12 is 0, hence the conditional).
    • Day: Always returning 01, the first day of the month.
    DATE(   YEAR( date ) + FLOOR( ( MONTH ( date ) + number_of_months - 1 ) / 12 ),   IF( MONTH ( date ) + number_of_months = 12, 12, MOD( MONTH ( date ) + number_of_months, 12 )),   01 ) 

    How to use this: the date variable should be the date field you are starting with. You should replace number_of_months with the number of months you want to add to the original date. If the date is 07/01/2017 I want this to go out on 08/01/2017, I’d set number_of_months to 1. If 09/01/2017, I’d set it to 2, etc.

    Note: This only works for the first of each month. If you need it to work on any day of the month, use this more complicated solution to account for months having different lengths.

  • Automating MySQL Backups with Gulp

    Automating MySQL Backups with Gulp

    As I mentioned a few days ago, I’m using Gulp on a new WordPress project. I like to back up my work every night, and since a lot of WordPress config and customization happens in the WordPress editor and widgets, that means backing up the mysql database as well as the code.

    Why not use this newfound tool? Let’s do it.

    I did some searching and found Gulp WordPress Backup, but it was overkill for what I wanted. But I saw that it used an npm package named mysqldump, for the export, so I grabbed that and started setting up a new task in gulpfile.js:

    // add mysqldump as a dependency var mysqlDump = require('mysqldump');  // dumpDatabase gulp.task('dumpDatabase', () => { return new Promise((resolve, reject) => { mysqlDump({ host: 'localhost', user: 'user', password: 'pass', database: 'wp_database', dest: 'backup.sql' }, (err) => { if (err !== null) return reject(err); }); }) .catch((err) => { console.log(err); }); });

    Next step: Defining the filename. I just wanted to use today’s date because I intend on running this at the end of each work day. Since gulp is all javascript, this is easy:

    var today = new Date(), dd = today.getDate(), mm = today.getMonth()+1 //January is 0! yyyy = today.getFullYear(); if(dd<10) { dd = '0'+dd } if(mm<10) { mm = '0'+mm } today = mm + '-' + dd + '-' + yyyy;

    Add this to the gulp task and you are good to go!

    gulp.task('dumpDatabase', () => { var today = new Date(), dd = today.getDate(), mm = today.getMonth()+1 //January is 0! yyyy = today.getFullYear(); if(dd<10) { dd = '0'+dd } if(mm<10) { mm = '0'+mm } today = mm + '-' + dd + '-' + yyyy;      return new Promise((resolve, reject) => {         mysqlDump({             host: 'localhost',             user: 'user',             password: 'pass',             database: 'wp_database',             dest: 'SQLBackups/' + today + '.sql' // Outputs to the folder named SQLBackups and uses today's date as the filename.         }, (err) => {             if (err !== null) return reject(err);         });     })     .catch((err) => {         console.log(err);     });  });

    Make sure you add mysqldump to your project’s package.json, or at least run npm install mysqldump before using!

  • Fixing s3_website Java 9 Error with jEnv

    Fixing s3_website Java 9 Error with jEnv

    When I updated to macOS High Sierra, a bunch of necessary stuff broke: Jekyll, Homebrew, Node.js, and a bunch of gems. s3_website, the tool I use to deploy my Jekyll site to S3, was one of the gems that just completely disappeared. When I went to reinstall it, I got an error that I didn’t have Java installed. Against my better judgment, I went to the URL listed and installed it. Then I ran s3_website push.

    After about 30 seconds, I got an error saying that s3_website doesn’t work with Java 9, which was the most recent version at the link. And also the version you get with brew cask install java. Well, shit.

    Exception in thread "main" java.lang.ExceptionInInitializerError 	at org.jruby.Ruby.newInstance(Ruby.java:266) 	at s3.website.Ruby$.rubyRuntime$lzycompute(Ruby.scala:4) 	at s3.website.Ruby$.rubyRuntime(Ruby.scala:4) 	at s3.website.model.Config$$anonfun$15.apply(Config.scala:229) 	at s3.website.model.Config$$anonfun$15.apply(Config.scala:227) 	at scala.util.Try$.apply(Try.scala:192) 	at s3.website.model.Config$.erbEval(Config.scala:227) 	at s3.website.model.Site$$anonfun$2.apply(Site.scala:28) 	at s3.website.model.Site$$anonfun$2.apply(Site.scala:27) 	at scala.util.Success.flatMap(Try.scala:231) 	at s3.website.model.Site$.parseConfig(Site.scala:27) 	at s3.website.model.Site$.loadSite(Site.scala:100) 	at s3.website.Push$.push(Push.scala:62) 	at s3.website.Push$.main(Push.scala:40) 	at s3.website.Push.main(Push.scala) Caused by: java.lang.RuntimeException: unsupported Java version: 9 	at org.jruby.RubyInstanceConfig.initGlobalJavaVersion(RubyInstanceConfig.java:1878) 	at org.jruby.RubyInstanceConfig.(RubyInstanceConfig.java:1585) 	... 15 more

    After lots of searching, I came across a kind soul on Github suggesting that we use jEnv to define which java environment to use in the directory.

    When I first installed jenv, I couldn’t add versions to the tool. I kept getting this error:

    ln: /usr/local/opt/jenv/versions/oracle64-9.0.1: No such file or directory

    The fix is described here: https://github.com/gcuisinier/jenv/wiki/Trouble-Shooting. Once I added version 8 as well, I switched to version 8 locally with this:

    jenv local oracle64-1.8.0.151

    Then I opened a fresh Terminal window and ran s3_website again and everything pushed up to s3 without an issue.

  • Making line height work with spans

    Making line height work with spans

    The line-height attribute doesn’t work on span by default. If you need to use line-height on a span, you’ll need to set display:block; on the span element to make it display as a block element (like

    ).

  • d3.nest

    d3.nest

    While working on the 51 Line Charts for my Opioid Crisis data visualization, I ran into an issue with generating all 51 charts at the same time: The data was either in 51 rows stacked and I couldn’t access the dates effectively, or 306 rows unstacked and I had 6x as many objects as I needed when I did a join.

    The solution was to use D3.nest. Nesting allows elements in an array to be grouped into a hierarchical tree structure; think of it like the GROUP BY operator in SQL, except you can have multiple levels of grouping, and the resulting output is a tree rather than a flat table. The levels in the tree are specified by key functions.

    When I used the state as a key, it grouped each state’s data together in a tree.

    Input:

    state,code,year,deaths,adjrate Alabama,1,2010,547,11.8 Alabama,1,2011,552,11.8 Alabama,1,2012,562,12.1 Alabama,1,2013,598,12.7 Alabama,1,2014,723,15.2 Alabama,1,2015,736,15.7 Alaska,2,2010,83,11.6 Alaska,2,2011,107,14.2 Alaska,2,2012,129,17.4 Alaska,2,2013,105,14.4 Alaska,2,2014,124,16.8 Alaska,2,2015,122,16 Arizona,4,2010,1098,17.5 Arizona,4,2011,1071,16.9 Arizona,4,2012,1134,17.7 Arizona,4,2013,1222,18.7 Arizona,4,2014,1211,18.2 Arizona,4,2015,1274,19 Arkansas,5,2010,350,12.5 Arkansas,5,2011,355,12.6 Arkansas,5,2012,373,13.1 Arkansas,5,2013,319,11.1 Arkansas,5,2014,356,12.6 Arkansas,5,2015,392,13.8

    D3 code:

    d3.nest()   .key(function(d) { return d.state; })   .entries(data);

    Output:

    [   {     "key": "Alabama",     "values": [       {         "state": "Alabama",         "code": "1",         "year": "2010",         "deaths": "547",         "adjrate": "11.8"       },       {         "state": "Alabama",         "code": "1",         "year": "2011",         "deaths": "552",         "adjrate": "11.8"       },       {         "state": "Alabama",         "code": "1",         "year": "2012",         "deaths": "562",         "adjrate": "12.1"       },       {         "state": "Alabama",         "code": "1",         "year": "2013",         "deaths": "598",         "adjrate": "12.7"       },       {         "state": "Alabama",         "code": "1",         "year": "2014",         "deaths": "723",         "adjrate": "15.2"       },       {         "state": "Alabama",         "code": "1",         "year": "2015",         "deaths": "736",         "adjrate": "15.7"       }     ]   },   {     "key": "Alaska",     "values": [       {         "state": "Alaska",         "code": "2",         "year": "2010",         "deaths": "83",         "adjrate": "11.6"       },       {         "state": "Alaska",         "code": "2",         "year": "2011",         "deaths": "107",         "adjrate": "14.2"       },       {         "state": "Alaska",         "code": "2",         "year": "2012",         "deaths": "129",         "adjrate": "17.4"       },       {         "state": "Alaska",         "code": "2",         "year": "2013",         "deaths": "105",         "adjrate": "14.4"       },       {         "state": "Alaska",         "code": "2",         "year": "2014",         "deaths": "124",         "adjrate": "16.8"       },       {         "state": "Alaska",         "code": "2",         "year": "2015",         "deaths": "122",         "adjrate": "16"       }     ]   },   {     "key": "Arizona",     "values": [       {         "state": "Arizona",         "code": "4",         "year": "2010",         "deaths": "1098",         "adjrate": "17.5"       },       {         "state": "Arizona",         "code": "4",         "year": "2011",         "deaths": "1071",         "adjrate": "16.9"       },       {         "state": "Arizona",         "code": "4",         "year": "2012",         "deaths": "1134",         "adjrate": "17.7"       },       {         "state": "Arizona",         "code": "4",         "year": "2013",         "deaths": "1222",         "adjrate": "18.7"       },       {         "state": "Arizona",         "code": "4",         "year": "2014",         "deaths": "1211",         "adjrate": "18.2"       },       {         "state": "Arizona",         "code": "4",         "year": "2015",         "deaths": "1274",         "adjrate": "19"       }     ]   } ]

    A few useful tools and resources for understanding d3.nest:

  • Giving Atom a try

    Giving Atom a try

    I’ve used Coda by Panic as my IDE of choice for 10 years now, but I think it is starting to fall behind other tools. It isn’t updated often and there aren’t nearly as many community-built packages as other tools have. So I’m giving both Atom and Sublime a try. I’m trying Atom first.

    In my first few days of use, here are some things I love:

    • d3-snippets package – The D3 is super powerful, but unless you use it every day, it is easy to forget the syntax for its huge array of features. I usually need to keep the documentation open in another window. With this package I just need to start typing what I need and I get a list of options with useful snippets.
    • language-liquid package – Finally, highlighting liquid syntax! Coda didn’t have this.
    • platformio-ide-terminal package – Yeah, opening terminal.app in another window is easy. But having everything in one place is great.
    • It is so easy to find and install new packages for Atom. And there are so many!
    • Git integration – I usually use the command line for git, but I love being able to see which files in the sidebar are new, modified, and staged. Then with the click of a button I can commit and push them.
    • Easy customization – Within seconds I was able to switch themes and default fonts.
    • Multi line editing. Protip: On a Mac go to System Preferences > Mission Control and turn off the Control + Shift + Up Arrow and Down Arrow keyboard commands, which you can then use in Atom for multi line selection and editing.
  • Gulp and Sketch first use notes

    Gulp and Sketch first use notes

    I’m working on a new WordPress theme development project and using Gulp and Sketch for the first time. Here are my first use notes:

    Gulp

    • Toolkit for automating tasks. Tons of packages available for things like minifying JS, compiling Sass, linting, packaging into zip files, pushing content to S3 and external servers, watching and automatically rendering changes in the browser, etc.
    • Pain to install. Dependencies all the way down.
    • Everything important goes in gulpfile.js

    Here are the tasks I’m using:

    • gulp styles — Compile, autoprefix and minify Sass files.
    • gulp scripts — Minify javascript files.
    • gulp images — Compress and optimize images.
    • gulp watch — Compile assets when file changes are made, start BrowserSync
    • gulp — Default task – runs all of the above tasks.
    • gulp zip — Package theme into zip file for distribution, ignoring node_modules.

    Sketch

    • I love being able to take layers, merge them, and export them as different image formats. This makes exporting background content a breeze.
    • No longer do I need to toil with eyeballing buttons and trying to figure out their padding, background gradients, and border radius. Code export is a gift from above.
    • I like their price model. Use the app for life, free updates for a year, resubscribe when you need more updates.
  • TLDR pages – Simplified man pages with practical examples. Probably covers 80% of your daily use cases. Looks super cool.

  • Great email from Paul Jarvis’s Sunday Dispatches this week. The relationship doesn’t end once you make the sale. That is just the beginning. Don’t be the hot tub guy.