Activity Forums Salesforce® Discussions How to create custom fields of standard or custom object through apex code?

  • Surbhi

    Member
    August 30, 2016 at 12:44 pm

    Hi Tanu,

    You can refer to Metadata API document. Also please have a look on below code:

    CustomObject co = new CustomObject();
    co.setFullName(objectName+"__c");
    co.setDeploymentStatus(DeploymentStatus.Deployed);
    co.setDescription("Created by the WSC using the Metadata API");
    co.setLabel(displayName);
    co.setPluralLabel(displayName+"s");
    co.setSharingModel(SharingModel.ReadWrite);
    co.setEnableActivities(true);

    // create the text id field
    CustomField field = new CustomField();
    field.setType(FieldType.Text);
    field.setDescription("The custom object identifier field");
    field.setLabel(displayName);
    field.setFullName(objectName+"__c");
    // add the field to the custom object
    co.setNameField(field);

    AsyncResult[] ars = metadataConnection.create(new CustomObject[] { co });

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos