Legacy Approach
Previously, I used to use Apex Metadata Service (https://andyinthecloud.com/2013/10/27/introduction-to-calling-the-metadata-api-from-apex/); but it requires to import all code for even a small requirement. After, release of Tooling API, I find it lifesaver for such implementation.
Below given approach is with help of Tooling api.
Here is one Salesforce stackexchange question: https://salesforce.stackexchange.com/questions/162390/how-to-update-picklist-values-along-with-record-types-using-metadataservice
New Approach
- This protoype is achieved using tooling api and workbench
- Need to refine – remove unwanted metadata in request. If you want to remove then its ok as well.
- If you just want to update/create picklist field then you will have to just make first HTTP request given below.
- First http call to add/remove/update picklist value.
- Second http call to add new value in record type.
First HTTP Call to Update Picklist Field
PATCH
/services/data/v41.0/tooling/sobjects/CustomField/00N540000071QJG
Here is JSON Body that has to be sent as request body in this callout: https://github.com/ayub-ansari/Create-Picklist-Field-Using-Apex/blob/master/FirstHTTPRequestToUpdatePicklistField
Note:
- Replace Custom field Id with your custom field in above given endpoint.
- Please copy the request body carefully and replace the picklist field metadata. In the given example, we are updating a picklist field “Competitor__c” on Object “Opportunity”. You will have to replace all values, labels and api names.
- Optionally, there is a trick to get picklist field metadata as well. You can get that using workbench –> Rest Explorer and hit
/services/data/v48.0/tooling/sobjects/CustomField/00N9000000DHfrM where ‘00N9000000DHfrM
is existing field with same data type. this call will return json response where you can extract metadata and manipulate as per your requirement. - An important note that, if you are just updating picklist value and there is no controlling field then you may face an error – “Cannot deserialize instance of complexvalue from VALUE_NULL value null or request may be missing a required field”. This is coming because attribute “valueSettings” is set to null but actually it should be []. So replace null with []. You will be able to save it.
2nd HTTP Call to Update Record Type to Include New Picklist Values
PATCH
/services/data/v41.0/tooling/sobjects/RecordType/012f20000009x5X
Please find here example of metadata JSON Request Body: https://github.com/ayub-ansari/Create-Picklist-Field-Using-Apex/blob/master/SecondHTTPRequestToUpdateRecordTypes
Note:
- Replace the record type id with your id in given endpoint.
- Please copy the request body carefully and replace the picklist field metadata. In the given example and replace the values with your metadata.
- Optionally, there is a trick to get picklist field metadata as well. You can get that using workbench –> Rest Explorer and hit
/services/data/v48.0/tooling/sobjects/RecordType/012f20000009x5X
is existing record type. This call will return JSON response where you can extract metadata and manipulate as per your requirement.
I visited many web pages however the audio feature for audio songs
present at this site is genuinely superb. asmr 0mniartist