You need to sign in to do that
Don't have an account?
Nethra Raghupathy
create remote site settings through apex
Hi All,
I have developed a lightning application which requires adding of remote site settings dynamically via apex metadata service (Andrew Fawcett approach).
It throws following error to authorize my SOAP API. But it works fine in anonymous window.
"IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://xxxx.xxx.visual.force.com/services/Soap/m/44.0"
Is there a way to do it without manually adding the SOAP API in remote site settings?
Apex:
Component Controller:
I have developed a lightning application which requires adding of remote site settings dynamically via apex metadata service (Andrew Fawcett approach).
It throws following error to authorize my SOAP API. But it works fine in anonymous window.
"IO Exception: Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://xxxx.xxx.visual.force.com/services/Soap/m/44.0"
Is there a way to do it without manually adding the SOAP API in remote site settings?
Apex:
@AuraEnabled public static void getCreateRemoteSiteSetting() { MetadataService.MetadataPort service = createService(); MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting(); remoteSiteSettings.fullName = 'hello123'; remoteSiteSettings.url = 'http://www.hello123.com'; remoteSiteSettings.isActive=true; remoteSiteSettings.disableProtocolSecurity=false; system.debug(remoteSiteSettings); service.createMetadata(new List<MetadataService.Metadata> { remoteSiteSettings }); } public static MetadataService.MetadataPort createService() { MetadataService.MetadataPort service = new MetadataService.MetadataPort(); service.SessionHeader = new MetadataService.SessionHeader_element(); service.SessionHeader.sessionId = UserInfo.getSessionId(); return service; }
Component Controller:
function(component, event, helper){ var remoteSite = component.get("c.getCreateRemoteSiteSetting"); remoteSite.setCallback(this, function(data) { console.log(data); }); $A.enqueueAction(remoteSite); }
Please checkout the link to create Remote Site Settings throug apex.
Link : http://www.exceptionlesscode.com/how-to-create-remote-site-settings-through-apex/
Cheers!!!
In the above method we have to manually add my endpoint in my remote site settings (https://xxxxx.salesforce.com) which I can't. Is there any other way to automate it as well?
The link shared helps to create remote site settings through apex.You can pass the value from your component to apex class and then create the same.If there is any understanding gap could you please share the process which you are trying to acheive.Then I can help you out.
In first place my concern is to make remote site settings dynamic.
If you want to retrieve the Instance Url for you org then you can use :
URL.getSalesforceBaseUrl()
When you get this value insert it as a remote site setting,if not added previously.
Cheers!!!
Using URL.getSalesforceBaseUrl() I will get my instance, but I can't add it manually to remote site settings, as when a client downloads the app from AppExchange I will get his/her instance but how to add it to remote site settings?. I have to use metadata API only to achieve it right?. Please guide me if I'm wrong.
Only Metadata API is the solution to insert the remote site setting using BaseURL().Nothing else can be done.
Cheers!!!!