Create Remote Site Setting through Visualforce Page

Hey Salesforce Ohana, I am back with new blog post. In this blog I will explain How to create a Remote Site Setting through coding.

Most of developer or admin usually create Remote Site Setting through Remote Sites Section [Administer -> Security Control -> Remote Site Settings] in your Salesforce organization.

If you do not know what is Remote Site Setting?

Why we use Remote site setting?

You can refer this blog post for complete details.

Now it’s time for coding.


Before starting , you should know about MetaData API .

A Secrete key to access, create your Metadata like Workflows, Remote Site, Validation Rules.

Let’s download MetaData WSDL File [Build -> API -> MetaData WSDL] and generate apex class using this WSDL File [Build -> Develop -> Apex Classes -> Generate from WSDL] or you can refer this MetadataService apex class.


Add your Salesforce Organization domain name and your visualforce page domain name in Remote site setting by following steps.

[Administer -> Security Control -> Remote Site Settings]

Now, Create a Visualforce Page for input Remote site details and apex controller for business logic.

Below is your apex controller code which will call MetaDataPort Method in the MetaDataService apex class for generating of session Id your salesforce organization and enter remote site details and call createMetaData() method.


You can refer the complete code here.

- This Visualforce Page is created for Remote Site Creation
- using MetaDataService apex class.
- @author Sagar Sindhi
- @since 10.03.2018
- @revision N/A
<apex:page docType="html-5.0" sidebar="false" controller="RemoteSiteCreationExt">
<!-- Page Message to display success or error message -->
<apex:PageMessages id="msg" />
<apex:pageBlock title="Remote Site Setting" id="RmtBlck">
<apex:pageBlockSection columns="2" id="RblckSec">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Name"/>
<apex:inputText id="rmtname" value="{!RemoteName}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="URL"/>
<apex:inputText id="rmturl" value="{!RemoteUrl}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Descripton"/>
<apex:inputTextArea id="rmtdesc" value="{!RemoteDescription}"/>
<apex:pageBlockSectionItem >
<apex:outputLabel value="Active"/>
<apex:inputCheckbox id="rmtact" value="{!RemoteActive}"/>
<div align="center">
<apex:commandButton value="Create Remote Site" action="{!RemoteSiteCreation}" reRender="msg,RmtBlck" status="actStatusId"/>
<apex:actionStatus id="actStatusId">
<apex:facet name="start">
<div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb;height: 100%;opacity:0.75;width:100%;">
<div class="waitingHolder" style="top:150px; width: 40px;">
<img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
* This Apex class is used for bussiness logic
* with RemoteSiteCreation Visualforce Page to
* call the MetaDataService class for Remote
* Site Creation.
* @author Sagar Sindhi
* @since 10.03.2018
* @revision N/A
public with sharing class RemoteSiteCreationExt {
public String RemoteName {get;set;}
public String RemoteUrl {get;set;}
public String RemoteDescription {get;set;}
public Boolean RemoteActive {get;set;}
public void RemoteSiteCreation()
String resultMessage;
if(String.isEmpty(RemoteName) && String.isEmpty(RemoteUrl))
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please Enter Name and URL'));
else if(String.isEmpty(RemoteName))
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please Enter Name'));
else if(String.isEmpty(RemoteUrl))
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please Enter URL'));
MetadataService.MetadataPort ServiceCreation = new MetadataService.MetadataPort();
ServiceCreation.SessionHeader = new MetadataService.SessionHeader_element();
ServiceCreation.SessionHeader.sessionId = UserInfo.getSessionId();
MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
remoteSiteSettings.fullName = RemoteName;
remoteSiteSettings.url = RemoteUrl;
remoteSiteSettings.description= RemoteDescription;
remoteSiteSettings.isActive= RemoteActive;
MetadataService.SaveResult[] results = ServiceCreation.createMetadata(new List<MetadataService.Metadata> { remoteSiteSettings });
for(MetadataService.SaveResult sr : results)
if(sr.errors != NULL)
for(MetadataService.Error er : sr.errors)
resultMessage = er.message;
if(results[0].success == true)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Remote Site Successfully created !!'));
RemoteName = '';
RemoteUrl = '';
RemoteDescription = '';
RemoteActive = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, resultMessage));
catch(Exception ex)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, ex.getMessage()));

Hope you find this helpful, in case of any queries/concerns, please post a reply or drop an email to me at .

Happy Coding 🙂