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
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;
}
John
says:Thanks! Needed this code.
Patlatus
says:Hi,
Cool snippet for creation of remote site setting.
Do you know, however, how to retrieve existing site settings (all or by name)?
Pooja
says: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
Ankit Sharma
says:Thanks Patlatus.
However, it’s been a long time since I coded on salesforce platform. So I can’t recall at the moment.
RAJU
says:Cna u plz share vf page code .my mail id is nagarajuu.c@gmail.com
RAJU
says: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
Ashish Narang
says: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
Renu Arora
says:I had the same query. I have to add the Salesforce url itself to the RemoteSiteSettings. Did you find the answer to your query?
ruban
says:Hi Ankit,
Nice snippet.i need this,
But i need test class for the above two classes.
can u please help me?
Ankit Sharma
says:Sorry. I don’t have the test class with me.
Paritosh
says:Thanks you it was very helpful 🙂
winlaeaye
says:Can you give me VF page code? Thanks in advance….
Pls send code to this email gccuser201709@gmail.com
Shiva
says: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”