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.

https://c.ap1.visual.force.com

https://ap1.salesforce.com

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 = ‘http://www.google.com’;
    remoteSiteSettings.description=’ewf’;
    remoteSiteSettings.isActive=true;
    remoteSiteSettings.disableProtocolSecurity=false;
    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.

  2. julfy@i.ua'
    Patlatus
    says:
    Reply

    Hi,
    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 https://github.com/financialforcedev/apex-mdapi/blob/master/apex-mdapi/src/classes/MetadataService.cls

  3. Ankit Sharma
    says:
    Reply

    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 nagarajuu.c@gmail.com

  5. Reply

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

    nagarajuu.c@gmail.com

  6. narang.ashish@gmail.com'
    Ashish Narang
    says:
    Reply

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

    1. renu.arora1210@gmail.com'
      Renu Arora
      says:
      Reply

      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
      says:
      Reply

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

  8. bhatia.paritosh@gmail.com'
    Paritosh
    says:
    Reply

    Thanks you it was very helpful 🙂

  9. Reply

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

  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 https://c.ap1.visual.force.com here. (Please Suggest)
    I replaced my domain name in the place of ap1 in this url “https://ap1.salesforce.com”

Leave a Reply to Patlatus

Cancel reply

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