-1

Recently I am building a side project myself to allow user mass update custom fields label. Currently, you can only do that one at a time. The POC looks good, I am able to achieve that with text and email fields. enter image description here

However, the metadataSerivceExample class doesn't offer an example for number/picklist/formula/Lookup...etc. Reference: Github Link

At first, I thought only to simply change the type_x to corresponding value with the updated label, but no luck.

Just wondering anyone with similiar experince could give any suggestions or ideas? thanks.

enter image description here

1 Answer 1

-1

You have not shared the JS. I am sharing the apex code to update the multiple fields using MetadataApi.

MetadataService.MetadataPort service = MetadataServiceExamples.createService();
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Contact.Change__c';
        customField.label='Change__c Changed';
        customField.type_x = 'Text';
        customField.length = 52;
MetadataService.CustomField customField1 = new MetadataService.CustomField();
        customField1.fullName = 'Contact.LookupField__c';
        customField1.label='LookupField Changed';
        customField1.type_x = 'Lookup';
        customField1.referenceTo = 'Account';
        customField1.relationshipLabel = 'Account';
        customField1.relationshipName = 'Account';

        List<MetadataService.SaveResult> results =
            service.updateMetadata(
                new MetadataService.Metadata[] { customField,customField1 });

You can not change the Label of the Standard field it'll throw Field_integrity_exception

Only custom fields label will be changed.

To update the picklist you need to do some additional changes in MetadataService class -

Replace

public virtual class CustomValue extends Metadata {
    public String color;
    public Boolean default_x;
    public String description;
    public Boolean isActive;
    public String label;
}

With -

public virtual class CustomValue extends Metadata {
    public String type = 'CustomValue';
    public String fullName;
    private String[] fullName_type_info = new String[]{'fullName',SOAP_M_URI,null,'0','1','false'};
    public String color;
    public Boolean default_x;
    public String description;
    public Boolean isActive;
    public String label;
    private String[] color_type_info = new String[]{'color',SOAP_M_URI,null,'0','1','false'};
    private String[] default_x_type_info = new String[]{'default',SOAP_M_URI,null,'1','1','false'};
    private String[] description_type_info = new String[]{'description',SOAP_M_URI,null,'0','1','false'};
    private String[] isActive_type_info = new String[]{'isActive',SOAP_M_URI,null,'0','1','false'};
    private String[] label_type_info = new String[]{'label',SOAP_M_URI,null,'0','1','false'};
    private String[] apex_schema_type_info = new String[]{SOAP_M_URI,'true','false'};
    private String[] field_order_type_info = new String[]{'fullName','color','default_x','description','isActive','label'};
}
MetadataService.MetadataPort service = MetadataServiceExamples.createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Contact.Level__c';
customField.label = 'picklist 1';
customField.type_x = 'Picklist';

// Define the Value Set and Value Set Definitions
MetadataService.ValueSet vs = new MetadataService.ValueSet();
MetadataService.ValueSetValuesDefinition vd = new MetadataService.ValueSetValuesDefinition();

// Define the picklist values
List<MetadataService.CustomValue> customValues = new List<MetadataService.CustomValue>();
MetadataService.CustomValue cv1 = new MetadataService.CustomValue();
cv1.label = 'One';
cv1.fullName = 'One';
cv1.isActive = true;
cv1.default_x = false;
customValues.add(cv1);
MetadataService.CustomValue cv2 = new MetadataService.CustomValue();
cv2.label = 'Two';
cv2.fullName = 'Two';
cv2.isActive = true;
cv2.default_x = true;
customValues.add(cv2);


vd.value = customValues;
vd.sorted = true;

vs.valueSetDefinition = vd;

// Use valueSet instead of Picklist
customField.valueSet  = vs;

List<MetadataService.SaveResult> results =
    service.updateMetadata(
        new MetadataService.Metadata[] { customField });

So you only need to add the customFields variables in the MetadataService.Metadata[].

I hope this will help you out.

4
  • Please read the question, I managed to get the text and email field working, what about other field type like number/picklist/formula/Lookup. BTW, you can update those 2 fields in 1 go rather than 2.
    – Wayne Ni
    Mar 17 at 7:15
  • Yes I'm able to update the fields in 1 go, you just need change the type__x to the field type like lookup, picklist. Search by lookup in the GitHub link you shared. Mar 18 at 9:11
  • Please read the question again, I tried that, and It doesn't work, I will share the error message later on
    – Wayne Ni
    Mar 19 at 5:44
  • @WayneNi, please check the updated answer Mar 20 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .