Reference a remote site setting URL in Apex class?
Asked Answered
A

1

6

I have a webservice class that will be in a managed package and distributed to multiple clients. The class currently has a variable with the hardcoded value of the server it's hitting.

The problem: the server will be different for each client, so a hardcoded value will not work.

I figured since each client will have to add their server to their remote site settings, the easiest way might be to grab the correct URL from their setting. Is this possible? Or is there another "right" way to accomplish this? Thanks

Audrit answered 26/7, 2011 at 15:39 Comment(0)
V
10

The best way I've found to save configuration values is to use Apex Custom Settings. To set the Remote Site settings programmatically you could use the Metadata API.

Viscountcy answered 26/7, 2011 at 21:21 Comment(4)
How about if all I want to do is test to see if the remote-site setting exists?Wolford
I just want to test (inside a unit test) to see if the remote site is properly configured. After deploying to production (or integration) I want to test multiple setup parameters, one of them being if the remote site setting has been created correctly.Wolford
I'm not aware of any way to access remote site settings setup from within Apex code. <hack-alert>You could write a method which attempts a callout to a given URL, then if Salesforce suppresses the callout (catch the exception) you'll know that the Remote site setting is not setup correctly</hack-alert>Viscountcy
But Metadta APi by itself requires an endpoint set in Remote Site Setting . How will you overcome that requirement ? @AdamTractor

© 2022 - 2024 — McMap. All rights reserved.