18 thoughts on “Apex Code to Create Custom Field Programmatically”

  1. Getting INVALID_SESSION_ID error

    [{“message”:”This session is not valid for use with the REST API”,”errorCode”:”INVALID_SESSION_ID”}]

  2. May be you are doing something wrong in your implementation. Just verified below piece of code in developer console and it worked:

    HttpRequest request = new HttpRequest();
    request.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionID());
    request.setHeader(‘Content-Type’, ‘application/json’);
    request.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+’/services/data/v41.0/tooling/sobjects/CustomField/’);
    request.setMethod(‘POST’);

    request.setBody(‘{“Metadata” : {“type” : “Text”,”description” : “ayub”, “inlineHelpText” : “”,”precision” : null,”label” : “Success”,”length” : 255,”required” : false}, “FullName” : “Case.Success__c”}’);

    Http http = new Http();
    HTTPResponse res = http.send(request);
    system.debug( res.getBody());

  3. Am also getting same error
    [{“message”:”This session is not valid for use with the REST API”,”errorCode”:”INVALID_SESSION_ID”}]

  4. I have Gone through the above mentioned code i have a query that text fields and formula fields are being created but if i want to create Date,Number,Email,Checkbox etc fields then how can i create that methods please explain or give me some code so that i can also use and see

  5. I have Gone through the above mentioned code i have a query that text fields and formula fields are being created but if i want to create Date,Number,Email,Checkbox etc fields then how can i create that methods please explain or give me some code so that i can also use it and see

  6. So this creates a field but it appears as hidden in the page layout and profile, please share some sample code to change visibility settings via apex?

  7. Trulife Distribution – Nutrition Distribution helps our clients achieve success in a complex, competitive retail environment. Our team of nutrition industry experts takes care of everything from importation compliance to marketing, sales and distribution at the ground level. There is no need to navigate the complicated intricacies of the American market when we have already done the work. Let us use our experience to expand your brand and put your product into the hands of American consumers. https://trulifedist.com/

  8. Simply want to say your article is as amazing.

    The clearness in your post is simply great and i could assume you are an expert
    on this subject. Fine with your permission let me to grab your RSS
    feed to keep up to date with forthcoming post.
    Thanks a million and please keep up the enjoyable work.
    0mniartist asmr

Comments are closed.