From ea8e7056e8b4414d4e464fc4ce3c611ca81ebd26 Mon Sep 17 00:00:00 2001 From: Yousuf Zaman Date: Wed, 29 Jan 2020 16:52:13 +0600 Subject: [PATCH] Example: Add value in picklist using customValue Earlier Piklist has been deprecated. Needs to add some example to add value in picklist using valueSet. --- .../src/classes/MetadataServiceExamples.cls | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/apex-mdapi/src/classes/MetadataServiceExamples.cls b/apex-mdapi/src/classes/MetadataServiceExamples.cls index 0d51b12..d07e9bc 100644 --- a/apex-mdapi/src/classes/MetadataServiceExamples.cls +++ b/apex-mdapi/src/classes/MetadataServiceExamples.cls @@ -293,6 +293,40 @@ public with sharing class MetadataServiceExamples System.debug(fileProperty.fullName); } + public static void addValueInPicklistField() + { + MetadataService.MetadataPort service = createService('sss'); + String picklistapiname = 'Lead.LeadSource'; + + MetadataService.CustomField customField = (MetadataService.CustomField) service.readMetadata('CustomField', new String[] { picklistapiname}).getRecords()[0]; + + List newValues = new List(); + + MetadataService.CustomValue customValue1 = new MetadataService.CustomValue(); + customValue1.fullName = 'Lead Source 1'; //API name of picklist value + customValue1.description = 'Lead Source 1'; + customValue1.label = 'Lead Label 1'; + customValue1.default_x = false; + customValue1.isActive = true; + newValues.add(customValue1); + + customField.valueSet.valueSetDefinition.value.addAll(newValues); + + // Update + List lstResults = service.updateMetadata( new MetadataService.Metadata[] { customField }); + + for (MetadataService.SaveResult objResult : lstResults) { + if (objResult.success) { + System.debug('Successfully updated'); + } + else { + if(objResult.errors.size() > 0){ + System.debug('eror : ' + objResult.errors[0].message); + } + } + } + } + /* * Piclkist in CustomField has been deprecated use valueSet instead public static void createPicklistField() @@ -1713,4 +1747,4 @@ public with sharing class MetadataServiceExamples if(!upsertResult.success) throw new MetadataServiceExamplesException('Request failed with no specified error.'); } -} \ No newline at end of file +}