22

I am using Visual Studio Code to develop new functionality.

However, after completing the testing in the sandbox, I want to push the code to production. When I choose "SFDX: Deploy Code to org", it fails with errors -

INVALID_OPERATION: rollbackOnError option must be true on a production org

and

INVALID_OPERATION: testLevel of NoTestRun cannot be used in production organizations

The code is in the new "source format", but I'm not developing in scratch orgs (I started the project with "SFDX: Create Project with Manifest").

Question 1: Do I need to convert the source to Metadata API format before I can deploy to production? This is not how the instructions in release notes are written:

To deploy source to an org without source tracking (to an org that’s not a scratch org), right-click a manifest, a source file, or a directory in the Visual Studio Code explorer. Select SFDX: Deploy Source to Org.

https://releasenotes.docs.salesforce.com/en-us/winter19/release-notes/rn_vscode_any_org.htm

However, the Salesforce DX Developer Guide seems to imply it:

Release Your App to Production

After you convert from source format to metadata format, and package metadata from one org, you can release your app in a different org.

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_build_mdapi_production.htm

Question 2: If I don't need to convert the source format and can push to production directly, how do I set up Visual Studio Code / CLI to avoid the two errors?

1

3 Answers 3

14

You have two choices. First, you can use force:package commands to create an unlocked package. You can install this in production with force:package:install; it is similar to installing an AppExchange package. Otherwise, you would need to use force:source:convert to get a mdapi style format, then use force:mdapi:deploy with the relevant test level (e.g. RunLocalTests) and set rollbackOnError to true. Check out the Salesforce CLI Reference for specific details, or sfdx help force:<command> for command-level help direct from the console. I do not believe that you're currently allowed to create unlocked packages directly via the GUI, but you can open a terminal by using the Terminal > Create New Terminal option, which will open a sfdx environment by default.

7
  • Thank you for making the 2 options explicit. I think my disconnect was that I was expecting the entire development workflow to go through VS Code menus.
    – t0tl
    Commented Dec 28, 2018 at 21:09
  • 2
    @t0tl I expect it will some day, but today isn't that day. Right now, it's focused more on development, but there's a few places that are either unclear or not implemented yet.
    – sfdcfox
    Commented Dec 28, 2018 at 21:10
  • 2
    @sfdcfox Sorry, I wasn't knocking your answer, I was hoping you had some insight because the trailhead is misleading. Just realized the trailhead is referring to a beta command that is failing cause there is no way I see to set rollbackOnError to true for production. To confirm, converting and then using mdapi:deploy works without test for LWCs Commented Feb 8, 2019 at 23:19
  • 1
    @Xtremefaith Thanks for the confirmation. I think they'll get around to it some day, but for now, the recommended path would be unlocked packages if you can afford to use them.
    – sfdcfox
    Commented Feb 8, 2019 at 23:43
  • 3
    As of v46.0, you can now deploy to production with force:source:deploy Commented Jul 16, 2019 at 10:16
20

As of Summer '19 (v46.0), the INVALID_OPERATION errors you were hitting are no longer an issue!

force:source:deploy now works when deploying to production. See an example below:

sfdx force:source:deploy -m ApexClass:MyApexClass -l RunSpecifiedTests -r MyApexClass_Test -w 3 -u MyProductionOrg
6
  • 1
    That is unbelievable!
    – hgolov
    Commented Jul 17, 2019 at 7:20
  • wish it can be done by VS code but not outside of it someday...
    – Cray Kao
    Commented Mar 8, 2020 at 13:05
  • How do you do this but just 1 LWC bundle? I normally just do this on VScode via right click the lwc i want to deploy then click "SFDX: Deploy Source to org".
    – compski
    Commented Mar 23, 2020 at 5:40
  • 2
    @compski sfdx force:source:deploy -m LightningComponentBundle:myLwcComp -u MyDestinationOrg You can see a list of all the different Metadata types here Commented Mar 23, 2020 at 7:19
  • It doesn't work for me in version 48.
    – picardo
    Commented Jun 28, 2020 at 19:25
3

The "SFDX: Deploy Code to org" command uses the sfdx force:source:deploy command, which according to the documentation only deploys to non-production orgs (at least as of Spring '19).

A nifty way to deploy to production with these new commands (as inspired by the documentation above) is as follows:

  1. Set the SFDX_MDAPI_TEMP_DIR environment variable to a place that's easily accessible (like the metadata folder within your current project):
SFDX_MDAPI_TEMP_DIR=/path/to/mydxproject/metadata
  1. Retrieve the exact metadata you want to deploy to production, like as follows:
sfdx force:source:retrieve -m ApexClass:MyUtilClass,ApexClass:MyUtilClass_Test -u MySandboxOrg
  1. This will create a folder within your metadata folder with the metadata .zip file or package.xml file that you're looking for. Deploy from the zip file as follows (and feel free to add any other helpful options as specified in the mdapi Commands):
sfdx force:mdapi:deploy -f ./metadata/sdx_sourceRetrieve_1554058974901/unpackaged.zip -u MyProductionOrg -w 3
  1. If the deployment takes longer than three minutes (as specified in the above -w 3 flag), then check your deployment status in the production org itself or via:
sfdx force:mdapi:deploy:report -u MyProductionOrg

This allows you to use the new force:source:retrieve command and have it auto-convert into metadata, without having to call force:source:convert manually

You must log in to answer this question.

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