Reputation: 2595
We are building a data integration platform to connect to Salesforce.
Would like to know if there is any documentation to create a connected app programmatically using APIs. I see lot of documentation for creating connected apps via UI but not through API.
Upvotes: 1
Views: 2016
Reputation: 1651
If you don't want to use Metadata API library, you could use the following snippet of code
private static String getSoapBodyXml(String endpoint, String name) {
return ''
+ '<?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>' + userInfo.getSessionId() + '</urn:sessionId>'
+ '</urn:SessionHeader>'
+ '</env:Header>'
+ '<env:Body>'
+ '<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">'
+ '<metadata xsi:type="ConnectedApp">'
+ '<fullName>' + name + String.valueOf(DateTime.now().getTime()).right(4) + '</fullName>'
+ '<label>' + name + String.valueOf(DateTime.now().getTime()).right(4) + '</label>'
+ '<contactEmail>[email protected]</contactEmail>'
+ '<oauthConfig>'+
+ '<callbackUrl>' + endpoint + '</callbackUrl>'
+ '<scopes>Full</scopes>'
+ '<scopes>RefreshToken</scopes>'
+ '</oauthConfig>'
+ '</metadata>'
+ '</createMetadata>'
+ '</env:Body>'
+ '</env:Envelope>'
;
}
public static HttpResponse add(String endpoint, String name) {
HttpRequest req = new HttpRequest();
req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/m/50.0');
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setHeader('SOAPAction', '""');
req.setBody(getSoapBodyXml(endpoint, name));
HttpResponse r = new Http().send(req);
System.debug('add getBody: ' + r.getBody());
System.debug('add getStatus: ' + r.getStatus());
System.debug('add getStatusCode: ' + r.getStatusCode());
val(r);
return r;
}
private static void val(HttpResponse r) {
System.debug('success? ' + r.getBody().contains('<success>true</success>'));
if (!r.getBody().contains('<success>true</success>')) {
throw new UnexpectedException(r.getBody().substringBetween('<statusCode>', '</statusCode>') + ': ' + r.getBody().substringBetween('<message>', '</message>'));
}
}
public static HttpResponse deploy(Integer i) {
return add(
'https://google.com',
'DeployedApp' + i
);
}
Upvotes: 1
Reputation: 56
This code creates connected app.
MetadataService.MetadataPort service = createService();
MetadataService.ConnectedApp connectedApp = new MetadataService.ConnectedApp();
connectedApp.label = 'Test 005';
connectedApp.fullName = 'Test_005';
connectedApp.contactEmail = '[email protected]';
MetadataService.ConnectedAppOauthConfig oauthConfig = new
MetadataService.ConnectedAppOauthConfig();
oauthConfig.consumerKey = 'yourConsumerKey';
oauthConfig.consumerSecret = 'yourConsumerSecret';
oauthConfig.scopes = new List<String>{'Basic', 'Api', 'Web', 'Full'};
oauthConfig.callbackUrl = 'https://www.google.com/';
connectedApp.oauthConfig = oauthConfig;
List<MetadataService.SaveResult> results = service.createMetadata(new
MetadataService.Metadata[] { connectedApp });
Upvotes: 3
Reputation: 2759
You can create Connected Apps through the Metadata API, like any other Salesforce metadata.
The easiest way to do this is to build the Connected App in a Salesforce org and then extract it with a Metadata API client (SFDX, Workbench, CumulusCI, Ant...). Excise the Consumer Key from the metadata, and you'll then be able to deploy that Connected App cleanly into another org.
Note, though, that this is rarely necessary. Connected Apps are global metadata: you typically maintain your Connected App in a single org that you control, and it's then available everywhere. I've really only seen the need to deploy Connected Apps when a different one is needed in each subscriber org.
Upvotes: 5