0

How Bulk Creation of Custom Objects can be implemented?

Assuming we need to replicate and debug a case where there are 3000 custom objects in the customer org. To replicate this situation, we could create as many custom objects as we could. However, it takes a lot of time to create single object.

Is there any way to automate this or create many objects at once?

1 Answer 1

1

There could be several ways to automate sobject creation, if we need to create multiple custom objects.

If you use SFDX, it is possible to use shane plugin to create custom object metadata file and then deploy those files or create a package from them and install the package.

upperlim=2000

for ((i=0; i<=upperlim; i++)); do
   echo "$i"
   sfdx shane:object:create -t custom -l "CustomObject$i" -p "CustomObjects$i" -a "CustomObject${i}__c" 
done

Another option is to use Metadata API and create them in Apex

for (Integer i = 0; i < 100; i++) {
    HttpResponse res = MetadataApiCustomObjectCreate.add('CustomObject' + i);
    System.debug(res.getBody());
}

with use of the following class

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);
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .