11

I have created a scratch org on my home machine and it is active and can be seen in the devhub. And I am now on my office computer.

However, when I use sfdx force:org:list I can't see that particular scratch org. And if I directly use sfdx force:org:open -u [userName] it comes back with an error message. And I am not quite sure what the password is for that particular scratch org.

What should I do in this case?

5 Answers 5

9
sfdx force:org:list

The above command is specific to the machine you used. So don't think your office laptop has any knowledge of what orgs you authenticated with and set as the default hub org and the default scratch org.

You will need to authorize all the orgs you have with your office laptop again, and create your scratch org again from the office CLI.

Follow the below steps

1.Again authorize your scratch org using the sfdx force:auth:web:login and entering the username and password

2.Use force:config:set to set the default scratch org

sfdx force:config:set [email protected]

Optioanlly you can use sfdx force:alias:set to set alias as well default scratch org and the hub org .

4
  • 1
    Thank you, Mohith. I will check them. But currently your link is pointing to about:blank. And I have noticed that I need to generate my password for my scratch org user before it has one. So does that mean I can't login to my scratch org before generating the pwd?
    – Lance Shi
    Commented Jan 25, 2018 at 5:53
  • I think you have to. But anyway, you shouldn’t worry too much about a specific scratch org. Everything should be in your repo, as Scratch orgs are ephemeral, so just git clone your repo again and create a new Scratch org from it. Commented Jan 25, 2018 at 8:00
  • well, I started work on my scratch org at home, and now you're telling me I can't pull that code into my project here at work - I have to wait until I go back home again and pull the changes out and commit to my VCS
    – bkwdesign
    Commented Mar 1, 2019 at 15:54
  • 🚫🔫📬 Don't shoot the messenger 🤷‍♂️ @bkwdesign For current functionality, in order to access the same org, you'll have to have set the password and authenticate to it from a different machine. That, or you can replicate the org with code checked in, but it won't have the data or uncommitted changes of your other org. Commented Jul 8, 2021 at 0:02
4

If you used your own connected app to authenticate with the DevHub which was used to create the scratch org, then you can use that connected app to connect to any scratch org created using the same connected app.

You'll need the username, your connected app's private key and your client id (consumer key)

e.g.

sfdx force:auth:jwt:grant -u [email protected] -f server.key -i <ConsumerKey> -a <MyAlias> -r https://test.salesforce.com

Full documentation here under Authorize a Scratch Org

6
  • so, if Machine 1 auth'd to DevHub using web flow and then created scratch org Foo; then machine 2, even if using a Connected App on the devhub configured for JWT, cannot authorize FOO? The whole chain of devhub auth/scratch org creation/scratch org auth has to be done for all machines using the JWT flow with the same consumer_key and server.key?
    – cropredy
    Commented Jul 9, 2020 at 1:02
  • You can always use the username and password with sfdx force:auth:web:login. But if you use the JWT flow with your own connected app, when the org is created that Connected App is automatically preauthed for the creating user (you can see the connected app consumer_key in the ScratchOrgInfos object, alongside a short lived auth_code used to grab a refresh_token on creation, and your connected app listed in Connected Apps Oauth Usage).
    – James
    Commented Jul 21, 2020 at 21:43
  • It looks like the same process used with Trialforce orgs to allow the ISV to access the provisioned org without knowing the password set by the new org admin. If you could get the cert for the Global Connected App then you could access any scratch org, which seems like a bad idea!
    – James
    Commented Jul 21, 2020 at 21:43
  • Clarification: (you can see the connected app consumer_key in the ScratchOrgInfos object in your devhub, alongside a short lived auth_code used to grab a refresh_token on creation, and your connected app listed in Connected Apps Oauth Usage in the scratch org)
    – James
    Commented Jul 21, 2020 at 21:55
  • I understand how username/pw can be used; but I was trying to get clarification that if machine 2 uses JWT to access machine1's scratch org, machine 1 needs to have created the scratch org using JWT
    – cropredy
    Commented Jul 21, 2020 at 22:19
3

Here are the commands I used to reconnect to a scratch org that was initiated on another machine. Please note, I had my .sfdx/ folder included in my git repo, so the org definition existed when I pulled my code down to my other machine.

It was not necessary to reauth to the scratch org in this case (which would be difficult since the password is not known).

shanekenyon@Shanes-MacBook-Pro ~/Documents/git/csvupload (master) $ sfdx force:config:set  defaultusername=dev
=== Set Config
NAME             VALUE
───────────────  ─────
defaultusername  dev
shanekenyon@Shanes-MacBook-Pro ~/Documents/git/csvupload (master) $ sfdx force:org:list
=== Orgs
  ALIAS      USERNAME                     ORG ID              CONNECTED STATUS
  ─────────  ───────────────────────────  ──────────────────  ────────────────
  devhub     [email protected]            ****000001Uj0****   Connected


     ALIAS  SCRATCH ORG NAME     USERNAME                       ORG ID              EXPIRATION DATE
───  ─────  ───────────────────  ─────────────────────────────  ──────────────────  ───────────────
(U)  dev    shanekenyon Company  [email protected]  ****000000982****   2018-10-23
3
  • 1
    Just seeing this now .Storing .sfdx folder on git can mean potential security risk . Commented Feb 19, 2019 at 14:16
  • 1
    Yeah its not a good idea to have the .sfdx folder in the repo. We stopped doing it a while ago because it simply causes problems and doesn't work all that well. There is no reason to share scratch org creds, and you rightly point out that keeping sandbox org auth there is a security risk and violation of the principle to keep "secrets" out of source control.
    – Shanerk
    Commented Feb 19, 2019 at 17:30
  • 1
    Another option is to create another user with System Administrator access in the scratch org and do a password reset on that user to get access for multiple developers.
    – Shanerk
    Commented Apr 14, 2020 at 15:33
1

I appreciate all the answers from experts. But here's another one:

In Source Machine

You can check the list of all ORGs that you have already used and their status too (i.e. Org is expired or not) using this code in the VSCode terminal.

> sfdx force:org:list

The above command is specific to the machine you used. So don't think your office laptop has any knowledge of what orgs you authenticated with and set as the default hub org and the default scratch org.

You don't need this above-mentioned step

  1. Access your source machine that has the scratch ORG.
  2. Open that org using VSCode

How to open org in VSCode

  1. After opening ORG in the browser, get Session-Id (by following these steps)

One fast and easy way to get SId is: Add this extension in your browser ad you will get the URL and sid at the URL's end. (Easy way) https://chrome.google.com/webstore/detail/sfdl-salesforce-direct-lo/hbenaapjmjekjignjanojegkiphoclma Copy and save (You will use it later)

If you don't want to install. Open browser > right click > Inspect > Application > Cookies > click on second link > find SID > Copy and save (You will use it later)

For Target Machine

The next step is to open that Screch ORG in our machine.

Follow the below steps

First you have to clone everything from the source machine to the target machine.

We already have URL and SID, So Press: Crtl+Shift+P

Select> sfdx:Authorize an org using session Ids

Put> Source ORG URL> That we copied from the above steps (i.e. HTTPS://"ORG Name".my.salesforce.com)

Put> Name for your ORG!

Put> SID (i.e. from extension > after sid="This is your session id") or what you copied form Inspect

wait for seconds (Congratulations you are done) now the ORg is successfully authorized!

You can open the scratch org as mentioned in the previews step.

How to open org in VSCode

If you have any concerns, In the above steps, Let me know I will be happy to help.

1
  • Welcome to SFSE and thank you for your contribution. Kindly note that contributions to this site is voluntary and it's not well received to 'request' votes
    – Shamina
    Commented May 4, 2022 at 5:22
0

If possible on your home machine use

Sfdx Force:user:password:generate -u orgalias

The use the username and password displayed to authenticate from your work machine

If you need the password again use

Sfdx force:user:display

Not sure if the work machine would recognize it as a scratch org but you would then have it connected

You must log in to answer this question.

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