Tag Archives: MetadataService

Create Remote Site Settings Using Apex (Metadata API)

Sample Code:

//Metadata Service
MetadataService.MetadataPort service = new MetadataService.MetadataPort();
ServiceCreation.SessionHeader = new MetadataService.SessionHeader_element();
ServiceCreation.SessionHeader.sessionId = UserInfo.getSessionId();

//Metadata for remote site settings
MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
remoteSiteSettings.fullName = 'Google';
remoteSiteSettings.url = 'https://www.google.com';
remoteSiteSettings.description = 'Remote site settings created from apex';
remoteSiteSettings.isActive = true;
remoteSiteSettings.disableProtocolSecurity = false;
MetadataService.SaveResult[] results = service.createMetadata(new List<MetadataService.Metadata> { remoteSiteSettings });

for(MetadataService.SaveResult sr : results){
    if(result.success){
        //Success
        system.debug('Sucess - ' + result);
    }else{
        //Failed
        system.debug('Failed - '+ result.errors[0].message);
    }
}

Note: You need to import all the metadata classes from Metadata WSDL to get this working.