3

How to pass a powershell variable as parameter to apex execute

$paramstring = 'long json string from a local file';
sfdx force:apex:execute
apexclass ac = new apexclass($paramstring);
ac.mainmethod();

1 Answer 1

5

sfdx force:apex:execute has two modes of operation. You can either provide it with a file via -f:

sfdx force:apex:execute -f myApexScript.apex

or you can run in a quasi-interactive or piped mode with

sfdx force:apex:execute

In the latter case, you can either enter your script at the prompt, interactively, or you can pipe a script into standard input. In a Bash script, for example (I'm not sure what the PowerShell equivalent might be), you'd do

cat somescript.apex | sfdx force:apex:execute

or

echo "System.debug('Test');" | sfdx force:apex:execute

You can apply any of those paradigms using the appropriate PowerShell idioms. SFDX doesn't have any special parameter-passing syntax; you just have to construct a text script and supply it to the command.

You must log in to answer this question.

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