0

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?

2

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(); 
field.setType(FieldType.Text); 
field.setDescription(description); 
field.setLabel("answerid"); 
field.setFullName("answerid__c"); 
field.setLength(lenght);

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">
  <fieldPermissions>
        <editable>true</editable>
        <field>ObjectName__c.Field__c</field>
        <readable>true</readable>
    </fieldPermissions>
</Profile>

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? – Hemant Keni Jan 10 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 – Hemant Keni Jan 10 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" : '; – Hemant Keni Jan 10 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()); – Hemant Keni Jan 10 at 7:39
  • 1
    @HK As I mentioned I done this in past for app exchange project. So you can also do this. – Tushar Sharma Jan 10 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.