How to create remote site settings through apex.

How to create remote site settings through apex.

Step 1:

Create a new class for Metadata Service, and copy the contents of attached Metadata Service file. MetadataService

Step 2:

Add the following endpoints to your remote site settings, It is required to callout the web service.

Note:- Change sub domains of the endpoints according to your org.

Step 3:

Create a visual force page along with its controller class. And use the following code.

public with sharing class MetaController {

public static void createRemoteSiteSettings()
    MetadataService.MetadataPort service = createService();
    MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
    remoteSiteSettings.fullName = ‘Test12345’;
    remoteSiteSettings.url = ‘’;
    MetadataService.AsyncResult[] results = service.create(new List<MetadataService.Metadata> { remoteSiteSettings });
    MetadataService.AsyncResult[] checkResults = service.checkStatus(new List<string> {string.ValueOf(results[0].Id)});
    system.debug(‘chk’ + checkResults );

 public static MetadataService.MetadataPort createService()
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    return service;

13 thoughts on “How to create remote site settings through apex.

  1. Reply

    Thanks! Needed this code.


    Cool snippet for creation of remote site setting.
    Do you know, however, how to retrieve existing site settings (all or by name)?

    1. Reply

      You may use financial force MetadataService API its a wrapper over salesforce meta data api. Checkout this link

  3. Ankit Sharma

    Thanks Patlatus.

    However, it’s been a long time since I coded on salesforce platform. So I can’t recall at the moment.

  4. Reply

    Cna u plz share vf page code .my mail id is

  5. Reply

    I dont know how to create remote site settings through apex dyanamically i am new in soap and rest plz help me

    Ashish Narang

    No need to have this code, you can package it directly.
    Actual thing I was looking for was – How to add salesforce URL itself as remote site settings through code

      Renu Arora

      I had the same query. I have to add the Salesforce url itself to the RemoteSiteSettings. Did you find the answer to your query?

  7. Reply

    Hi Ankit,

    Nice snippet.i need this,

    But i need test class for the above two classes.
    can u please help me?

    1. Ankit Sharma

      Sorry. I don’t have the test class with me.


    Thanks you it was very helpful 🙂

  9. Reply

    Can you give me VF page code? Thanks in advance….
    Pls send code to this email

  10. Reply

    I tried to put this code in apex controller and calling it from the lightning and it is throwing me this error.
    “Web service callout failed: WebService returned a SOAP Fault: INVALID_SESSION_ID: This session is not valid for use with the API faultcode=sf:INVALID_SESSION_ID faultactor=”
    What is c.ap1 in here. (Please Suggest)
    I replaced my domain name in the place of ap1 in this url “”

Leave a reply

Your email address will not be published. Required fields are marked *