Is it allowed to create a custom field dynamically(Tooling API) through managed package?

Will it create any issue from the salesforce review perspective?

What will be the best way to give access to the fields created dynamically?

1 Answer 1


Yes, you can easily create field using Metadata API or tooling API. In past, I have created fields dynamically using Metadata API.

CustomField field = new CustomField(); 

customObject.setFields(new CustomField[] {field});
UpsertResult[] results = metadataConnection.upsertMetadata(new Metadata[]{ customObject });

To assign the permission you need to update the profile metadata.

<?xml version="1.0" encoding="UTF-8"?>
<Profile xmlns="http://soap.sforce.com/2006/04/metadata">

You can find Metadata API wrapper here

  • @ Tushar Shrama, Thanks for your response. I know that We can create a custom field using metadata API as well as tooling API. But, I wanted to know the salesforce security aspect for creating dynamic fields through manage pacakge. Whether it is allowed or not? Jan 10, 2019 at 7:37
  • Tooling API approach to create custom field dynamically : String objectapiname = 'Contact';//replace with your object name String fieldapiname = 'CustomFieldUsingToolingAPI';//replace with your field name String fieldlabel = 'CustomFieldUsingToolingAPI';//replace with your field label String fielddescription = 'CustomFieldUsingToolingAPI';//replace with your field label Jan 10, 2019 at 7:39
  • HttpRequest requestinside = new HttpRequest(); requestinside.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID()); requestinside.setHeader('Content-Type', 'application/json'); requestinside.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+'/services/data/v41.0/tooling/sobjects/CustomField/'); requestinside.setMethod('POST'); String fieldDef = '{"Metadata" : '; Jan 10, 2019 at 7:39
  • String metadef = '"type" : "Text","description" : "'+fielddescription+'", "inlineHelpText" : "","precision" : null,"label" : "'+fieldlabel+'","length" : 255,"required" : false'; fieldDef += '{'+metadef+'},'; fieldDef += '"FullName" : "'+objectapiname+'.'+fieldapiname+'__c"}'; system.debug(fieldDef); requestinside.setBody(fieldDef); Http http = new Http(); HTTPResponse res = http.send(requestinside); System.debug(res.getBody()); Jan 10, 2019 at 7:39
  • 1
    @HK As I mentioned I done this in past for app exchange project. So you can also do this. Jan 10, 2019 at 8:27

You must log in to answer this question.

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