You need to sign in to do that
Don't have an account?
How to create remote site settings dynamically though apex ?
My first question, Is it possible to add a remote site dynamically through apex ?
Regardless of the answer, I still tried to achieve this by using Metadata API within Salesforce. Below is my code.
public static void createremoteSiteSettings()
{
MetadataService.MetadataPort service = createService();
MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
//remoteSiteSettings.fullName = 'Test';
remoteSiteSettings.url = 'http://www.google.com';
MetadataService.AsyncResult[] results = service.create(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;
}
I am getting this error : System.CalloutException: Web service callout failed: WebService returned a SOAP Fault: Must specify a {http://www.w3.org/2001/XMLSchema-instance}type attribute value for the {http://soap.sforce.com/2006/04/metadata}metadata element faultcode=soapenv:Client faultactor=
Any help would be appreciated !
You can also view the same post on my blog
http://www.exceptionlesscode.com/post/2013/05/06/How-to-create-remote-site-settings-through-apex.aspx
All Answers
Answering my own question. Yes, it is possible to create remote site settings dynamically through apex.
Got over through many errors but finally achieved what i wanted.
I can provide the solution if anyone needs it. :)
You can also view the same post on my blog
http://www.exceptionlesscode.com/post/2013/05/06/How-to-create-remote-site-settings-through-apex.aspx
your code helped a lot to solve a nagging problem I was encountering, thanks indeed!
All i'm trying to do is call MD CRUD from apex, and actually the WSDL2APEX utility is not foolproof at all, because the code generated doesnt have some very imp. attributes of the classes, in my case, the "Type" attribute was missing in CustomObject class... and I found the hint in your post!
Now i'll try once again to get rid of this error Web service callout failed: WebService returned a SOAP Fault: Must specify a {http://www.w3.org/2001/XMLSchema-instance}type attribute value for the {http://soap.sforce.com/2006/04/metadata}metadata element ...................
Could you please let me know how exactlly you created the remote site settings dynamically?
Thanks
You can see my post here
http://exceptionlesscode.com/index.php/how-to-create-remote-site-settings-through-apex/
Let me know if you need any other help
Thanks.
Your link does not work. Anyways, so the solution is good, but it will not work unless somebody manually enters remote site the first time. In this case https://c.ap1.visual.force.com and https://ap1.salesforce.com
I was looking for solution that actually enters the remote site with the host the very first time the app is installed. I would ideally like to do that in Post-Install.
I found this link http://andyinthecloud.com/2014/07/29/post-install-apex-metadata-api-configuration-solved/ which is great, but it does depend on user to click "Create Remote Site" button. At this stage, I am not sure if there is anyway of creating them via apex when the app is installed and automatically.
Unfortunately that link is down for maintainence. You are absolutley right, my solution only works when mentioned URL's are already added in remote site setting.
Would certainly share if I find something useful in regards to Post-Install.
I was also looking for exactly same thing, found useful link, may be usefull for other.
http://andyinthecloud.com/2014/07/29/post-install-apex-metadata-api-configuration-solved/
I have also bookmarked it as it has such rich information :)
Can you please explain me that where does the remote site settings value changes as I am unable to get the change when I am using your code.?
Hi Aforank/Mitesh,
I have a requirement where I need to update remote site setting using apex, I am able to insert/query remote site setting through apex but not able to update it, Is there any solution for this?