-
Notifications
You must be signed in to change notification settings - Fork 973
/
remotesitehelper.component
80 lines (74 loc) · 4.85 KB
/
remotesitehelper.component
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<apex:component controller="RemoteSiteHelperController" allowDML="true" access="global">
<apex:attribute name="rssName" type="String" required="true" access="global" assignTo="{!rssNameString}" default="mdapi" description="The name of the remote site setting you want to create." />
<apex:attribute name="rssDescription" type="String" required="true" access="global" default="mdapi" description="The description of the remote site setting you want to create." />
<apex:attribute name="pageOverview" type="String" required="false" default="This tool does lots of fun things with the Metadata API." description="The text to be displayed to the user that will give an overview of the tool using the metadata api." />
<apex:attribute name="pageOverviewLinkURL" type="String" required="false" default="http://google.com" description="Some link you want to point the user to."/>
<apex:attribute name="pageOverviewLinkText" type="String" required="false" default="Use Google" description="The link text."/>
<apex:attribute name="pageSectionTitle" type="String" required="true" access="global" default="Tool using Metadata API" description="This is the section Title for the page." />
<apex:attribute name="pageSectionSubTitle" type="String" required="true" access="global" default="Welcome" description="This is the section sub title for the page." />
<apex:attribute name="pageImageURL" type="String" required="false" access="global" default="http://andrewfawcett.files.wordpress.com/2013/07/blog_chart1.jpg" description="This is the url of the image to be displayed." />
<script>
function createRemoteSite()
{
// Disable button
document.getElementById('createremotesitebtn').disabled = true;
// Calls the Metdata API from JavaScript to create the Remote Site Setting to permit Apex callouts
var binding = new XMLHttpRequest();
var request =
'<?xml version="1.0" encoding="utf-8"?>' +
'<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+
'<env:Header>' +
'<urn:SessionHeader xmlns:urn="http://soap.sforce.com/2006/04/metadata">' +
'<urn:sessionId>{!$Api.Session_ID}</urn:sessionId>' +
'</urn:SessionHeader>' +
'</env:Header>' +
'<env:Body>' +
'<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">' +
'<metadata xsi:type="RemoteSiteSetting">' +
'<fullName>{!rssName}</fullName>' +
'<description>{!rssDescription}</description>' +
'<disableProtocolSecurity>false</disableProtocolSecurity>' +
'<isActive>true</isActive>' +
'<url>https://{!Host}</url>' +
'</metadata>' +
'</createMetadata>' +
'</env:Body>' +
'</env:Envelope>';
binding.open('POST', 'https://{!Host}/services/Soap/m/31.0');
binding.setRequestHeader('SOAPAction','""');
binding.setRequestHeader('Content-Type', 'text/xml');
binding.onreadystatechange =
function() {
if(this.readyState==4) {
var parser = new DOMParser();
var doc = parser.parseFromString(this.response, 'application/xml');
var errors = doc.getElementsByTagName('errors');
var messageText = '';
for(var errorIdx = 0; errorIdx < errors.length; errorIdx++)
messageText+= errors.item(errorIdx).getElementsByTagName('message').item(0).innerHTML + '\n';
displayMetadataResponse(messageText);
}
}
binding.send(request);
}
</script>
<apex:form id="myForm">
<apex:actionFunction name="displayMetadataResponse" action="{!displayMetadataResponse}" rerender="myForm">
<apex:param name="metadataResponse" assignTo="{!metadataResponse}" value="{!metadataResponse}"/>
</apex:actionFunction>
<apex:sectionHeader title="{!pageSectionTitle}" subtitle="{!pageSectionSubTitle}"/>
<apex:pageMessages />
<img src="{!pageImageURL}" width="200" height="200" align="left"/>
<apex:outputPanel rendered="{!MetadataConnectionWarning}">
<h1>Important Post Install Step:</h1>
<p>In order to utilise the features of this tool, you need to permit the tool access to the Salesforce Metadata API. This can be accomplished by adding a <a target="_new" href="http://na1.salesforce.com/help/doc/en/configuring_remoteproxy.htm">Remote Site Setting</a> with this Salesforce Server URL <b>https://{!Host}</b></p>
<p> Click the <b>Create Remote Site Setting</b> button to have the tool perform this step for you.</p>
<input id="createremotesitebtn" type="button" onclick="createRemoteSite();" value="Create Remote Site Setting"/>
</apex:outputPanel>
<p><b>Overview</b>: {!pageOverview}</p>
<p><b>Link</b>: <a href="{!pageOverviewLinkURL}" target="_new">{!pageOverviewLinkText}</a></p>
<apex:outputPanel rendered="{!MetadataConnectionWarning}">
<p style="color:gray">DEBUG USE ONLY: {!metadataApiException}</p>
</apex:outputPanel>
</apex:form>
</apex:component>