I've looked around for this and there seem to be a couple of different ways on how to do this.

However, all of these seem to be old ways (I might be completely wrong here and those methods can be totally valid today) but I'd like to make sure I'm using the latest method for doing this and avoid making a request like it's suggested in one of the StackExchange answers in the link above (The tooling API method). I also don't want to implement this in a Visual Force or any UI pages, I'd like to create custom fields on an existing object based on a received post request.

A simple example for what I'm trying to do:

for (String fieldName : fieldNameList)
   Existing_Custom_Object__c.CreateNewCustomField(name = fieldName, type = checkbox, ...);
   // Upload/Push this change to the org
  • 4
    You cannot mutate the schema of your org without calling either the Metadata or Tooling APIs. The linked question remains valid. The OOTB Apex Metadata API does not support Custom Fields.
    – David Reed
    Apr 22 at 23:43
  • Sorry what's OOTB?
    – Bahman.A
    Apr 23 at 0:08
  • What's the difference between the two methods in this SE post? salesforce.stackexchange.com/questions/56421/… Please give some more information to understand how it's done or even a reference link to a the official docs? Thanks!
    – Bahman.A
    Apr 23 at 0:10
  • 1
    Tooling API requires substantially less code. I am not aware of any official documentation beyond the Tooling API CustomField reference.
    – David Reed
    Apr 23 at 0:28
  • 1
    @Bahman.A "OOTB" is short for "out of the box". This means something provided as standard. See wikipedia for more.
    – Phil W
    Apr 23 at 6:58