[SalesForce] When and where should a connected app be created during DX managed package development

I'm developing a new managed package that will include a connected app. I created a connected app in a scratch org and committed it to version control. Then, when pushing the source to a new scratch org, I encountered the following error:

The consumer key is already taken.

I assume that this is because every time a connected app's source file is pushed to a new org, a new connected app is created and they obviously cannot share a consumer key.

My question is; in which org should I create the connected app if it needs to be used in all the scratch orgs that are used for development? Do I need to create an unmanaged package in a traditional development org and install it in each new scratch org after pushing the rest of the project's source?

I could create a new connected app in each scratch org but this wouldn't be viable for testing because the web server calling out to Salesforce would need to know the app credentials beforehand– it couldn't reference multiple new credentials for each new connected app.

Best Answer

Connected apps can't be created as an unmanaged package.

Technically you need the connected app only in your Dev Hub and all other orgs were you at deploying metadata.

Scratch orgs can be authenticated from the same machine or command line that created them.

I would recommend creating managed packages for the connected app as it eases deployment and installing in orgs where you want to connect like production org.

You will also need to assign the connected app to the profile that's used in deployment for the rest of the orgs.

Connected Apps Deployment in Scratch orgs

you can have the connected app included in the scratch org metadata. Just make sure you exclude the oauthConfig.consumerKey.

For deployment via Metadata API consider the guidelines outlined here.

Related Topic