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
Nethra RaghupathyNethra Raghupathy 

create remote site settings through apex

Hi All,
 
I have developed a lightning application which requires adding of remote site settings dynamically via apex metadata service (Andrew Fawcett approach).
It throws following error to authorize my SOAP API. But it works fine in anonymous window. 
 
"IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://xxxx.xxx.visual.force.com/services/Soap/m/44.0" 
 
Is there a way to do it without manually adding the SOAP API in remote site settings? 
 
Apex
@AuraEnabled 
    public static void getCreateRemoteSiteSetting()
    {
        MetadataService.MetadataPort service = createService();
        MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
        remoteSiteSettings.fullName = 'hello123';
        remoteSiteSettings.url = 'http://www.hello123.com';
        remoteSiteSettings.isActive=true;
        remoteSiteSettings.disableProtocolSecurity=false;
        system.debug(remoteSiteSettings);
        service.createMetadata(new List<MetadataService.Metadata> { remoteSiteSettings });
        
    }
    
    public static MetadataService.MetadataPort createService()
    {
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;
    }

Component Controller:
function(component, event, helper){
        var remoteSite = component.get("c.getCreateRemoteSiteSetting");
        remoteSite.setCallback(this, function(data) {
            console.log(data);
        });
        $A.enqueueAction(remoteSite);
    }

 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Nethra

Please checkout the link to create Remote Site Settings throug apex.

Link : http://www.exceptionlesscode.com/how-to-create-remote-site-settings-through-apex/

Cheers!!!
Nethra RaghupathyNethra Raghupathy
Hi Syed,

In the above method we have to manually add my endpoint in my remote site settings (https://xxxxx.salesforce.com) which I can't. Is there any other way to automate it as well?
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Nethra

The link shared helps to create remote site settings through apex.You can pass the value from your component to apex class and then create the same.If there is any understanding gap could you please share the process which you are trying to acheive.Then I can help you out.
Nethra RaghupathyNethra Raghupathy
Yes I got that but my concern is, that link expects me to have my endpoint (https://xxxx.xxx.visual.force.com/services/Soap/m/44.0) manually added to the remote site settings. Which isn't possible, as I wont know what the instance will be.
 
In first place my concern is to make remote site settings dynamic. 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Nethra

If you want to retrieve the Instance Url for you org then you can use : 
URL.getSalesforceBaseUrl() 
When you get this value insert it as a remote site setting,if not added previously.

Cheers!!!
Nethra RaghupathyNethra Raghupathy
Syed,

Using URL.getSalesforceBaseUrl() I will get my instance, but I can't add it manually to remote site settings, as when a client downloads the app from AppExchange I will get his/her instance but how to add it to remote site settings?. I have to use metadata API only to achieve it right?. Please guide me if I'm wrong. 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Nethra

Only Metadata API is the solution to insert the remote site setting using BaseURL().Nothing else can be done.

Cheers!!!!