0

Is it possible to run SFDX commands on some UI events?

If so, like we have used a Node.js application for it as SFDX commands are Node.js based. Are there any packages which we can install and use?

sfdx force:data:soql:query -q "SELECT Id, Name, Account.Name FROM Contact LIMIT 10" -u [email protected] --perflog --json
5
  • In what context are these UI events? In the Salesforce UI?
    – Phil W
    Commented Apr 22, 2021 at 11:24
  • no any button click ... Commented Apr 22, 2021 at 11:24
  • 1
    "any button click" doesn't give context. Is the button in a web UI? Is it in a desktop app? This will drive what you can do and how you do it.
    – Phil W
    Commented Apr 22, 2021 at 11:36
  • Sorry Phil.. yes from Web UI.. what i was trying to do is make this sfdx call from a LWC component.. but didnt know how to do it.. Commented Apr 22, 2021 at 11:42
  • I don't think you'll be able to. See my comment on Oleh Berehovski's answer.
    – Phil W
    Commented Apr 22, 2021 at 12:03

1 Answer 1

3

Locally (or in a container) for Node.js applications there are numerous libraries that enable executing Bash command/scripts. Some of them:

  1. ShellJS - Unix shell commands for Node.js.

    ShellJS is a portable (Windows/Linux/macOS) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands. You can also install it globally so you can run it from outside Node projects - say goodbye to those gnarly Bash scripts!

    Example:

    var shell = require('shelljs');
    // Of course you should log in first
    shell.exec('sfdx force:data:soql:query -q "SELECT Id, Name, Account.Name FROM Contact LIMIT 10" -u [email protected] --perflog --json');
    
  2. The child_process module provides the ability to spawn subprocesses.

    Example (asynchronous):

    const { exec } = require("child_process");
    
    const query = 'sfdx force:data:soql:query -q "SELECT Id, Name, Account.Name FROM Contact LIMIT 10" -u [email protected] --perflog --json';
    exec(query, (error, stdout, stderr) => {
        if (error) {
            console.log(`error: ${error.message}`);
            return;
        }
        if (stderr) {
            console.log(`stderr: ${stderr}`);
            return;
        }
        console.log(`stdout: ${stdout}`); // Result
    });
    

    The stdout and stderr arguments passed to the callback will contain the stdout and stderr output of the child process. The error object is not null when the child_process module fails to execute a command.

As soon as Salesforce Functions are available it will be possible to integrate Salesforce with Node.js, but for now, I can't say too much about its possibilities/limitations.

3
  • How is NodeJS to run in the context of a browser? You'll want to reference something like this and its related github repo, but then my worry is how Salesforce will let you use this since the Lightning Locker Services will probably get in the way. I'm not sure this is the answer.
    – Phil W
    Commented Apr 22, 2021 at 12:02
  • Salesforce Functions sounds plausible, but there will be a compute charge (per function execution and/or by CPU time) for this service, I'm quite sure, just like with AWS etc.
    – Phil W
    Commented Apr 22, 2021 at 12:05
  • 1
    Good points. There are numerous approaches of rendering html files with Node.js. I didn't deep dive into it but rather focused on how to execute sh/bash commands on UI events from the Node.js application. Generally speaking, I'd advise using standard Salesforce features and APIs whenewer it's possible. Commented Apr 22, 2021 at 12:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .