0

Here is my some piece of code where I am creating custom field using metadata API,by default is not visible,can i make this using apex,thanks in advance

global with sharing class dynamicfieldsCreation {
    static String F = '';
    public List < SelectOption > loadSelectedFields {
        get;
        set;
    }
    static list < String > reen = new list < String > ();
    static list < Schema.SObjectField > toStore = new list < Schema.SObjectField > ();
    static list < Boolean > required = new list < Boolean > ();
    static Schema.SObjectField field1;
    list < Schema.SObjectField > listogg = new List < Schema.SObjectField > ();
    static list < String > addtoElements = new list < String > ();
    //public static list<String> fieldList1=new list<String>();

global static void dynamicFieldsCreation() {


    SObjectType objectToken = Schema.getGlobalDescribe()
        .get('Lead');

    String response = '';
    Schema.DescribeSObjectResult r = objectToken.getDescribe();
    Map &lt; String, Schema.SObjectField > M = r.fields.getMap();
    for (String fieldName: M.keySet()) {


        field1 = M.get(fieldName);

        Schema.DescribeFieldResult Fig = field1.getDescribe();
        Boolean iscreate = Fig.isCreateable();
        Boolean iscreate1 = Fig.isCustom();


        system.debug('**** check is creatable *************' + iscreate);

        system.debug('*** get field names***' + String.ValueOf(field1));

        Boolean isFieldreq = Fig.isNillable();
        Boolean isdefault = Fig.isDefaultedOnCreate();

        if ((isFieldreq == false || iscreate == true || iscreate1 == true))
            toStore.add(field1);
        system.debug('***** getting list toStore******' + toStore);
    }



    system.debug('****** toStore.size() *************' + toStore.size());

    CreateCheckboxFields('Lead', toStore);

    //ClassTOCreateEmailFields.CreateEmailFields('Account', toStore);
    //ClassTOCreatePhoneFields.CreatePhoneFields('Account', toStore);
    //ClassTOCreatePICKLISTFields.CreatePICKLISTFields('Account', toStore);
    //ClassTOCreateAddressFields.CreateAddressFields('Account', toStore);
    //ClassTOCreateTextFields.CreateTextFields('Account', toStore);
    //ClassTOCreateUrlFields.CreateUrlFields('Account', toStore);
}

global static void CreateCheckboxFields(String selectedobje, list &lt; Schema.SObjectField > selectedFields) {
    system.debug('*** This is to create custom fields using metadata ****');
    system.debug('*** selectedobject ***' + selectedobje);
    system.debug('*** selectedFields ***' + selectedFields);
    List &lt; MetadataService.Metadata > createFieldList = new List &lt; MetadataService.Metadata > (); // Added By AnkaRao
    String stringDynamicObject = 'Dynamicobject__c';
    system.debug('*** The Object name is ***' + stringDynamicObject);
    String dynamicObjectLable = 'Dynamicobject';

    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();

    List &lt; String > dynamicfields = new List &lt; String > ();


    for (Schema.SObjectField field: selectedFields) {
        system.debug('the current field is 111====>' + field);
        Schema.DescribeFieldResult fieldResult = field.getDescribe();
        system.debug('**** check iscustom Field or not ****' + fieldResult.isCustom());
        system.debug('*** get the Name of fields***' + fieldResult.getName());
        system.debug('*** get the datatype ***' + fieldResult.getType());
        Boolean customResult = fieldResult.getName()
            .contains('__c');
        system.debug(customResult + '**** check custom or not ***');
        if (customResult) {
            system.debug('** will execute for custom field*****');
            system.debug('String.Valueof(fieldResult.getType())' + String.Valueof(fieldResult.getType()) == 'Checkbox' + String.Valueof(fieldResult.getType()));
            if (String.Valueof(fieldResult.getType()) == 'BOOLEAN') {
                system.debug('id callled');
                MetadataService.CustomField customField = new MetadataService.CustomField();
                                    String s = fieldResult.getName();
                                    system.debug('**field name**'+s );

if (s.endsWith('__c')) customField.fullName = stringDynamicObject + '.' + fieldResult.getName(); else customField.fullName = stringDynamicObject + '.' + fieldResult.getName() + '_Emp__c';
customField.label = fieldResult.getLabel(); customField.type_x = 'Checkbox'; customField.defaultvalue = 'false'; //if(createFieldList.size() < 10){

createFieldList.add(customField); //} //MetadataService.AsyncResult[] results2 = service.deleteMetadata(new List&lt;MetadataService.Metadata> { customField }); MetadataService.AsyncResult[] asyncResults = service.create(new List &lt; MetadataService.Metadata > { customField });
  • No. You have to update the profiles as well. – Eric Feb 16 '17 at 5:53
  • can u explain bit more\ – Satya Feb 16 '17 at 5:59
  • 1
    When fields are added by default they are invisible until the profiles or permission sets are also updated. So you will need to use MetadataService calls to also update those objects which is hard to do because your code can't know what the purpose of the various profiles or permission sets is in an org (in general). – Keith C Feb 16 '17 at 9:11

Your Answer

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

Browse other questions tagged or ask your own question.