function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ishaan UppalIshaan Uppal 

Salesforce Metadata API Remote Site Settings in Apex

Hi 

 

I am creating an application, where I need to use Metadata API from APEX code running in an org. to fetch and update objects within the same org (kind of loopback). Through my investigations, I came to know that it is possible to achieve this by using APEX SOAP Stubs generated from MetadataAPI WSDL. This WSDL is generated with instance URL specific to my organization.  e.g <soap:address location="https://na15-api.salesforce.com/services/Soap/m/27.0"/>

 

In order to use APEX SOAP stubs on my org, my application need to create Remote Site Setting for above *na15* url instance in WSDL. Correct ?. Next lets say I publish this app, then:

 

1. How will my application behave on a different organization which is running on a differnt instance url(e.g *na1*) than *na15*. It will receive unauthorized end point exception. So definitely hardcoding *na15* was never a right choice. 

 

2. Is there any generic instance URL for the Metadata API I can specify and bundle Remote Site Setting for that URL in my application, which can work seamlessly in any organization to connect to itself or some other org. 

 

 

One thing I am sure, using URL.getSalesforceBaseUrl would not do much help, since it would return Organization specific URLs.

Ishaan UppalIshaan Uppal

Salesforce Team  - Please help answer this issue. This is a road blocker for our application. 

nraghavannraghavan

Have a look at this great open source project that FinancialForce has put together for calling the Metadata API from Apex:

https://github.com/financialforcedev/apex-mdapi

AshwaniAshwani

HI, 

If I am understanding right. You should use URL.getSalesforceBaseUrl().getHost() method to run on organization on which application willl run. and generate soap address location by using URLMethods not by hardcode.

Becuase it returns the "instance+salesforce.com". So by this you can get instance of it before set remote site setting.

 

 

For any doubt please reply or mark it as solved if it solved your problem.

 

 

Thanks.

Ishaan UppalIshaan Uppal

Hi Ashwani,

 

My application will be running on different organizations, which will have different instance URLs. Using URL.getSalesforceBaseURL.getHost() will return instance URL for my org. It will be different for other orgs. Hence, I wont be able to create a single static entry for remote site setting.