Activity Forums Salesforce® Discussions How to create a custom field on a standard Salesforce object?

  • pramod

    Member
    December 21, 2017 at 1:53 pm

    How to create a custom field on a standard object?

    I am creating a VF page in that I want to create a custom field using apex:

    <apex:inputField value=”{!account.lastname}”/>

  • Ankit

    Member
    December 28, 2017 at 10:25 am

    Hi Pramod,

    For creating custom fields of a Standard object follow these steps:

    1. Open setup
    2. Go to object manager and choose the object on which you wanted to create fields
    3. Then click on the fields and relationship of that object
    4. Click on the new , select custom field type click on next
    5. Now enter field name and label you can also add description and help text as per your use
    6. Now click Next, Next and Save.

    Finally you created custom field for standard object.

  • Ajay Prakash

    Member
    January 4, 2018 at 3:58 am

    Hi Pramod,

    Although your question is not clear, I guess you want to create the custom field on a standard object using apex from the user interface i.e. Visualforce.

    To do so you need to consume metadata API in your Salesforce Org.For more details about metadata, API follows the link  Understanding Metadata API. Once your metadata apex class is created you need to call below method from you visualforce page while passing the certain parameters that you want to take input from the user.Ex- Object Name, Field Name, Field Type, Field Size, etc.

    public static void createField(){
    MetadataService.MetadataPort service = createService();
    MetadataService.CustomField customField = new MetadataService.CustomField();
    customField.fullName = 'Test_Object__c.Test_Custom_Field__c';
    customField.label = 'Test Custom Field';
    customField.type_x = 'Text';
    customField.length = 22;
    List<MetadataService.SaveResult> results =
    service.createMetadata(
    new MetadataService.Metadata[] { customField });
    handleSaveResults(results[0]);
    }

    In the above method, I have passed the static values that you have to replace from the User inputs.In the whole process, you will require creating a remote site setting because to create a metadata above code will make a callout in the same org where you want to create the custom field.

    Let me know if this helps you.

    Thanks.

     

    • This reply was modified 3 years, 5 months ago by  Ajay Prakash.
Viewing 1 - 3 of 3 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos