Skip to content

Instantly share code, notes, and snippets.

@Patlatus
Last active May 13, 2021 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patlatus/880d29f0ee83119b4538bc5348970493 to your computer and use it in GitHub Desktop.
Save Patlatus/880d29f0ee83119b4538bc5348970493 to your computer and use it in GitHub Desktop.
Metadata Api Custom Object Create
public without sharing class MetadataApiCustomObjectCreate {
private static String getSoapBodyXml(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="CustomObject">'
+ '<label>' + name + '</label>'
+ '<pluralLabel>' + name +'s' + '</pluralLabel>'
+ '<fullName>' + name + '__c' + '</fullName>'
+ '<nameField><label>Name</label><type>Text</type></nameField>'
+ '<deploymentStatus>Deployed</deploymentStatus>'
+ '<sharingModel>ReadWrite</sharingModel>'
+ '</metadata>'
+ '</createMetadata>'
+ '</env:Body>'
+ '</env:Envelope>'
;
}
public static HttpResponse add(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(name));
return new Http().send(req);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment