Get all the fields in a Marketo instance, including their Descriptions

Time for another in my series of UI hacks. (These aren’t fit for cross-posting on the official Products Blog, as they’re too far off the supported path, but they’re very handy.)

Previously, I supplied JS code you can run in your browser’s F12 Console to get all roles and permissions in a single file, and similar code to get all channels and tags.

A Marketo Nation user recently noted that the REST API Describe Lead endpoint doesn’t include the Description field that admins can populate in Admin » Field Management.

I would say this is sensible behavior, since we grant Describe access to outside services (can’t turn that off) and our internal descriptions aren’t any of their business. Sort of like leaking internal lead notes back to leads!

Anyway, just open up Admin » Field Management and run this in the console to create a file fields.json that includes all the trimmings:

var commonFetchOptions = {
   method : "POST",
   credentials : "same-origin",
   headers : {
      "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
      "X-Requested-With" : "XMLHttpRequest"
   }
};

var fieldsTreeEp = "/leadDatabase/getFieldsAsTree?withSalesMapping=1&showHiddenFields=1&showOpportunityFields=1&excludeSystemFields=1&showAccountFields=1&showProgramMemberCustomFields=1";
var exportedProps = [
  "text",
  "children",
  "description",
  "id",
  "aid",
  "type",
  "fieldType",
  "category"
];

fetch(fieldsTreeEp, 
  Object.assign( 
    commonFetchOptions, 
    { body: "node=root&xsrfId=" + MktSecurity.getXsrfId() } 
  ) )
  .then( respJ => respJ.json() )
  .then( resp => { 
    const downloadable = JSON.stringify(resp, exportedProps, 2);
    const downloader = document.createElement("a");
    downloader.href = window.URL.createObjectURL( 
      new Blob( [downloadable],{ type:"application/json" } ) 
    );
    downloader.download = "fields.json";
    document.body.appendChild(downloader);
    downloader.click();
  });