Environment variable as custom metadata type in Salesforce

0 votes
I stumbled across Custom Metadata Types when trying to represent environment variables in the Salesforce codebase. So, depending on whatever Sandbox I'm in, I'd like to change the baseURL of an external service to hit my apex class calls. I don't want to hard code anything in the class, so I'm looking for an environment variable-like approach.

As a custom metadata type, how would you express the URL? Also, how can I get to it in class? When a QA sandbox is refreshed from production, what happens? Is it possible for custom metadata type records to be overridden?
Mar 29, 2022 in SalesForce by surbhi
• 3,810 points
709 views

1 answer to this question.

0 votes
What's the name of the external service you're referring to? If it's actually a base url, "named credential" might be a better option. It'll abstract the base url for you, and if you need to offer authentication or a certificate, it'll do so...

If that fails, custom metadata could be a bad idea. You can add more (but not from apex) but if you deploy using Git/Ant/SFDX CLI instead of changesets, it'll be a nuisance because you'll require distinct custom metadata values for sandbox and production. It sort of ruins the purpose.

You might be better off utilising custom settings (hierarchy is enabled by default, but list requires a checkbox to be flipped in setup).

You can also change them using Apex. In an ideal world, you'd have a "postcopy" class that runs as soon as the sandbox is refreshed, overwriting the custom setting with the non-prod value. I don't think you can do it with named credentials; you'd need a tiny deployment or a manual step.
answered Apr 1, 2022 by CoolCoder
• 4,400 points

Related Questions In SalesForce

0 votes
0 answers

Apex (Salesforce) read json from response and store it in a string variable

I am trying to hit a API ...READ MORE

Mar 4, 2022 in SalesForce by surbhi
• 3,810 points
1,077 views
0 votes
1 answer

Upsert fields must be defined as unique External ID fields in Salesforce - Heroku Connect

You can use either the Salesforce record ...READ MORE

answered Apr 6, 2022 in SalesForce by CoolCoder
• 4,400 points
952 views
0 votes
0 answers

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

In logic app i'm using salesforce connector after deployment ...READ MORE

Mar 1, 2022 in SalesForce by surbhi
• 3,810 points
823 views
0 votes
1 answer

How to take test.salesforce.com while deployment as salesforce login url for api connection logic app?

you are not passing the parameterValues, use ...READ MORE

answered Mar 2, 2022 in SalesForce by surbhi
• 3,810 points
759 views
+2 votes
2 answers

Salesforce Interview questions

Here are some questions very important for ...READ MORE

answered Jan 11, 2019 in Career Counselling by Suresh
• 720 points
2,766 views
0 votes
1 answer

How to connect to salesforce from tableau?

Hi, follow these steps to connect to Salesforce: 1. ...READ MORE

answered Mar 25, 2019 in Tableau by Cherukuri
• 33,030 points
744 views
0 votes
1 answer

Power BI - Salesforce

Hi, Follow below steps: 1. Go to Data source. 2. ...READ MORE

answered Mar 25, 2019 in Power BI by Cherukuri
• 33,030 points
485 views
0 votes
2 answers

What is the best training for Salesforce ADM-201 Exam?

Hi @Vardhan, I took Edureka's Salesforce Online Training, ...READ MORE

answered Jun 3, 2021 in Others by Jaya
• 140 points

edited Dec 22, 2021 by Soumya 543 views
0 votes
1 answer

Apex (Salesforce) read json from response and store it in a string variable

String jsonStr = '{ "oktaToken":"eyJraWQiOiIyNlN1NHFMNnVVZTVJX2M5X2Z3WmZvX09ON0dNUHRtQzlEeHFsTGplLS00IiwiYWxnIjoiUlMyNTYifQ.eyJ2ZXIiOjEsImp0aSI6IkFULlVINmdIOF9BcWJ" }'; Map<String, String> ...READ MORE

answered Mar 3, 2022 in SalesForce by CoolCoder
• 4,400 points

edited Jun 27, 2023 by Khan Sarfaraz 9,117 views
0 votes
1 answer

how to create a custom login page in salesforce.com?

Here are a few things to consider: Is ...READ MORE

answered Apr 6, 2022 in SalesForce by CoolCoder
• 4,400 points
1,204 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP