Salesforce DX error output will go to stdout starting with v45

Now this is very good news for us that are doing scripting with Salesforce DX. Currently the Salesforce DX CLI (sfdx) will send its output to stdout (standard out) when the command succeeds and to stderr (standard error) if the command fails. Why would a command fail? Well if you try and describe an org with a wrong or non-existing alias that’s one reason.

Now this is really the expected behaviour for a CLI but when doing scripting using the –json flag to always have the response returned as JSON it just makes it more complicated. For one the response already have a “status”-flag to indicate whether the invocation was successful or not. Secondly having to deal with stdout and stderr is not difficult but leads to boilerplate scripting code as what you really want to do is capture the response and check that “status”-flag in the JSON. Also it makes it harder to adopt for people new to CLI scripting and the way it’s done across platforms differ.

Now to the good news.

As of v44, there is an environment variable to have all JSON output go to stdout. Set SFDX_JSON_TO_STDOUT=true to get this new behaviour. This will become the default behaviour in v45. Now it’s not listed on the environment variables list for Salesforce DX yet but I trust it will be soon.

Yay!! 🙏


XPages JavaScript utility function of the day

Yesterday and today I’ve been spending some time doing some XPages coding for a customer project and after spending quite some time doing Java plugin development I’m amazed of how easy XPages are. Don’t get me wrong – there is still work to do for the XPages team but it’s a joy to work with XPages and coding in JavaScript is just – well – flexible and fun.

One of the real joys of JavaScript is it’s dynamic nature and that it allows one to really cut down on the boilerplate and repeated code. For one I spent a lot of key pressed getting field values from backend documents in server side JavaScript. Instead of repeating the same ol’ Document.getItemValueString(String) over and over again I did a neat little shortcut. Since I needed all items from a document I just created a utility function to JSONify a document to make it easier to access. The method is below.

var jsonifyDocument = function(doc) {
   var result = {};
   if (!doc) return result;
   var item;
   for (item in doc.getItems().toArray()) {
      result[item.getName().toLowerCase()] = item.getText();
   return result;

So now instead of writing doc.getItemValueString all the time I can now just do property-like access to field values (“Heading” and “Description” are fields on the document).

var jsonDoc = jsonifyDocument(doc);
jsonDoc.heading + " (" + jsonDoc.description + ")";

I also wrote a nice little Dojo-like mixin function to make my XAgents easy to configure. But that’s for another day.

Messing with JSON?

Working with JSON can be a little tricky since it normally comes in as one long string which means it can be difficult to discover the data structure. I ran into this while pasing the JSON API and needed a JSON viewer to help me out. A quick Google search later I found JsonViewer which is a very nice small utility for perusing JSON data. No install necessary.

If you’re working with JSON check it out.

JSONMap class

I’m starting off with converting some inhouse developed AJAX stuff to using DOJO and for that reason I needed to convert my XML responses to JSON. Using the JSON Java classes this was pretty straight forward but I really needed an easier way to convert a java.util.Map instance to JSON. Extending JSONArray this was easy. Provided here for those who might need similar code…

package org.json;

import java.util.Iterator;
import java.util.Map;

public class JSONMap extends JSONArray {
   public JSONMap(Map m) {
      for (Iterator ite=m.keySet().iterator(); ite.hasNext(); ) {
         String key = (String);
         String value = m.get(key).toString();
         JSONArray a = new JSONArray();

Code like this

Map m = new HashMap();
m.put("AL", "Alabama");
m.put("AK", "Alaska");
m.put("AR", "Arkansas");
JSONMap jm = new JSONMap(m);

will output the following JSON

    [ "Alabama", "AL" ],
    [ "Alaska", "AK" ],
    [ "Arkansas", "AR" ]