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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// const METADATA_NAMESPACE: &str = "http://soap.sforce.com/2006/04/metadata";
// const ENVELOPE_NAMESPACE: &str = "http://schemas.xmlsoap.org/soap/envelope/";
/*
The endpoint /services/data/vXX.0/metadata/deployRequest is specifically designed for deploying metadata to a Salesforce org, not for retrieving it.
To retrieve metadata, you should use the /services/data/vXX.0/metadata/retrieveRequest endpoint.
Below is an example of how to retrieve metadata using the correct retrieveRequest endpoint via Apex:
```apex
public class MetadataRetrieveExample {
public static void retrieveMetadata() {
// 1. Define the package.xml content specifying what to retrieve
String packageXml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<Package xmlns="http://soap.sforce.com/2006/04/metadata">\n' +
' <types>\n' +
' <members>*</members>\n' +
' <name>ApexClass</name>\n' +
' </types>\n' +
' <version>58.0</version>\n' +
'</Package>';
// 2. Prepare the REST request
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data/v58.0/metadata/retrieveRequest');
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
// 3. Set the body with the package.xml encoded as base64
// The retrieve API expects a JSON object with a "unpackaged" field containing the base64 encoded package.xml
String body = '{"unpackaged" : "' + EncodingUtil.base64Encode(Blob.valueOf(packageXml)) + '"}';
req.setBody(body);
// 4. Send the request
Http http = new Http();
HttpResponse res = http.send(req);
System.debug('Retrieve Request Status: ' + res.getStatusCode());
System.debug('Retrieve Response: ' + res.getBody());
// 5. Parse the response to get the asyncResultId
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
String asyncResultId = (String) responseMap.get('id');
// 6. Poll for status using the asyncResultId
checkRetrieveStatus(asyncResultId);
}
public static void checkRetrieveStatus(String asyncResultId) {
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data/v58.0/metadata/retrieveRequest/' + asyncResultId);
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
Http http = new Http();
HttpResponse res = http.send(req);
Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
String status = (String) responseMap.get('status');
if (status == 'In Progress') {
// Poll again after a delay
System.debug('Still retrieving...');
} else if (status == 'Success') {
// The response will contain the zip file encoded in base64
String zipFile = (String) responseMap.get('zipFile');
System.debug('Retrieved zip file size: ' + zipFile.length());
// Decode the zipFile to retrieve the metadata components
Blob zipBlob = EncodingUtil.base64Decode(zipFile);
// Process the zipBlob as needed
} else {
System.debug('Retrieve Failed: ' + res.getBody());
}
}
}
```
Key Differences:
DeployRequest: Uses POST to /metadata/deployRequest with a body containing zipFile (the metadata to deploy) and deployOptions.
RetrieveRequest: Uses POST to /metadata/retrieveRequest with a body containing unpackaged (the package.xml defining what to retrieve). The response provides an id to poll for the result, which includes the zipFile of the retrieved metadata.
*/
use HashMap;
/*
fn escape_xml(value: &str) -> String {
value
.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
.replace('"', """)
.replace('\'', "'")
}
*/