Tweet |
Introduction
As regular readers of this blog know, I'm a huge fan of the Salesforce CLI. I reckon getting on for half of the talks/blogs that I've done in the last couple of years have been around using or extending it. I've even wrapped it in a GUI that I use multiple times every day, to open orgs, create scratch orgs or work with packages. I'm always looking for new ways to leverage it, and a year or so ago I was asked to create a document with details of the objects and fields from one of my Salesforce orgs. While I could do this manually, that's a lot of effort for something that will almost certainly be wrong in a couple of days. A better idea would be to automatically generate the document from the metadata.
BP (Before Plug-Ins)
AP (After Plug-Ins)
The Metadata
- Book - details of a specific book
- Author - an individual who has written one or more books
- Publisher - an organisation that publishes books from multiple authors
- Book Signing - an event where an author signs copies of their book that customers have purchased
- Author Reading - an event where an author reads from their book to an invited audience
I want to create an HTML document that pulls information from the metadata and generates an HTML document, separating the objects into two groups - Events (Book Signing and Author Reading) and the rest.
Creating a Plug-In
Creating a plug-in is as simple as executing the plugins:generate command and answering some questions. The following example creates a plug-in named 'sample'. Note that the plug-in is generated in the current working directory, so I create the sample directory first, or everything ends up in the root of my drive!kbowden@Keirs-MacBook-Pro ~ *$ mkdir sample
kbowden@Keirs-MacBook-Pro ~ *$ cd sample
kbowden@Keirs-MacBook-Pro sample *$ sfdx plugins:generate
_-----_ ╭──────────────────────────╮
| | │ Time to build an │
|--(o)--| │ sfdx-cli plugin! │
`---------´ │ Version: 1.1.2 │
( _´U`_ ) ╰──────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
? npm package name sample
? description Sample plug-in for blog
? author Keir Bowden @keirbowden
? version 0.0.0
? license MIT
? Who is the GitHub owner of repository (https://github.com/OWNER/repo) keirbowden
? What is the GitHub name of repository (https://github.com/owner/REPO) sample
Once I've answered the questions, the command creates some files in the directory and installs a bunch of packages. You don't need to worry about the details, just make sure that the following is output when it completes to indicate the command was successful:
Created sample in /Users/kbowden/sample
(replacing sample and the directory with your specific details, obviously!)
Executing a Plug-In Command
kbowden@Keirs-MacBook-Pro sample *$ bin/run hello:org -u MENTZLIVE
Hello world! This is org: Bob Buzzard
My hub org id is: 00D30000000J2G8EAK
Creating a New Command
The easiest way to create a new command is to copy the example and change it to your requirements. I'd very much advise taking baby steps when you are doing this for the first time, so try to keep the command working and re-run it when making changes.
The example command lives in the src/commands folder:
the hello folder defines the topic, and org.ts contains the command source code. Copying this to src/commands/bbdoc/doc.ts adds the bbdoc:doc command:
kbowden@Keirs-MacBook-Pro sample *$ bin/run bbdoc:doc -u MENTZLIVE
Hello world! This is org: Bob Buzzard
My hub org id is: 00D30000000J2G8EAK
So running a couple of commands, answering a few questions and copying a file gives a whole new (if familiar) plug-in and command. At this point the plug-in is complete and can be published to npm. It's unlikely to see much uptake, but there's nothing else that needs to be done to make it distributable.
In the next instalment, I'll start the customisation of the bbdoc:doc command. If you can't wait, you can see the plug-in source on Github or install it yourself from npm.
Related Posts
- Parallel Apex Unit Tests and Salesforce CLI Plug-Ins
- Going GUI over the Salesforce CLI Part 2
- Going GUI over the Salesforce CLI
- Mentz - the Mentee Workflow
- Offline mobile app template plug-in Dreamforce 18 session
- Salesforce CLI Play-by-Play
- Salesforce CLI Cheat Sheet
- SFDX and the Metadata API Part 4 - VSCode Integration
- SFDX and the Metadata API Part 3 - Destructive Changes
- SFDX and the Metadata API Part 2 - Scripting
- SFDX and the Metadata API
For anyone having issues with the hello:org command, you need to change the package.json file to point to src/commands, otherwise the command will not work
ReplyDeleteSee here: https://salesforce.stackexchange.com/questions/318491/unable-to-run-custom-hello-world-sfdx-plugin-locally