5

Would anyone have an example on how to update a picklist?

I have a System Custom Field that I want to append add new items to as it comes up.

The details of the field are the following:

  • Field Label: Device Status
  • Field Name: Device_Status
  • API Name: Device_Status__c
  • Object Name: System Data
  • Type: Picklist

How can I append one picklist value using REST API?

2
  • 2
    By the way, if this is not a Restricted Picklist, you can insert (via the API) any value you wish in the picklist.
    – sfdcfox
    Commented May 8, 2018 at 15:47
  • 1
    Thank you, you are exactly right about this. I unchecked the box "Restrict picklist to the values defined in the value set". After that I was able to send other values. Then when I checked the custom field, the values were listed in two catergories "Values" and "Inactive Values".
    – Kris
    Commented May 8, 2018 at 22:09

2 Answers 2

7

As the other answer says, you can do this via the Tooling API if you want to use a REST call. You can find the details on their answer. The other option is to use the Metadata API which is a SOAP API. This stackexchange post has some example code about updating picklists using the Metadata API. The first link to the Metadata API docs should get you started on how to use it. If you are trying to do this in Apex, there is an apex wrapper for the Metadata API on Github.

There is an idea for implementing the Metadata API in Apex natively here, and it appears that the latest update on that idea indicates that as of October 2019 they are not planning on updating that API in the near future.

1
  • Thank you, you are right. I was able to find a different way to accomplish this.
    – Kris
    Commented May 8, 2018 at 22:10
3

Now, It can be done using REST calls with help of Tooling API.

The above given solutions works perfectly using 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 without importing and managing too much code. We just need to make a callout using Tooling API.

Full detailed article is here: https://sfdcian.com/update-picklist-value-and-add-it-in-record-type-using-apex-salesforce/

PATCH
/services/data/v41.0/tooling/sobjects/CustomField/00N540000071QJG

Set metadata as body

{"Metadata" : {
    "businessOwnerGroup" : null,
    "businessOwnerUser" : null,
    "businessStatus" : null,
    "caseSensitive" : null,
    "complianceGroup" : null,
    "customDataType" : null,
    "defaultValue" : null,
    "deleteConstraint" : null,
    "deprecated" : null,
    "description" : null,
    "displayFormat" : null,
    "displayLocationInDecimal" : null,
    "encryptionScheme" : null,
    "escapeMarkup" : null,
    "externalDeveloperName" : null,
    "externalId" : false,
    "formula" : null,
    "formulaTreatBlanksAs" : null,
    "inlineHelpText" : null,
    "isAIPredictionField" : null,
    "isConvertLeadDisabled" : null,
    "isFilteringDisabled" : null,
    "isNameField" : null,
    "isSortingDisabled" : null,
    "label" : "Community (Region)",
    "length" : null,
    "lookupFilter" : null,
    "maskChar" : null,
    "maskType" : null,
    "metadataRelationshipControllingField" : null,
    "populateExistingRows" : null,
    "precision" : null,
    "readOnlyProxy" : null,
    "referenceTargetField" : null,
    "referenceTo" : null,
    "relationshipLabel" : null,
    "relationshipName" : null,
    "relationshipOrder" : null,
    "reparentableMasterDetail" : null,
    "required" : false,
    "restrictedAdminField" : null,
    "scale" : null,
    "securityClassification" : null,
    "startingNumber" : null,
    "stripMarkup" : null,
    "summarizedField" : null,
    "summaryFilterItems" : null,
    "summaryForeignKey" : null,
    "summaryOperation" : null,
    "trackFeedHistory" : false,
    "trackHistory" : false,
    "trackTrending" : null,
    "translateData" : null,
    "type" : "Picklist",
    "unique" : null,
    "urls" : null,
    "valueSet" : {
      "controllingField" : null,
      "restricted" : null,
      "valueSetDefinition" : {
        "sorted" : false,
        "value" : [ {
          "color" : null,
          "default" : false,
          "description" : null,
          "isActive" : null,
          "label" : "Canada",
          "urls" : null,
          "valueName" : "Canada"
        }, {
          "color" : null,
          "default" : false,
          "description" : null,
          "isActive" : null,
          "label" : "US",
          "urls" : null,
          "valueName" : "US"
        },
         {
          "color" : null,
          "default" : false,
          "description" : null,
          "isActive" : null,
          "label" : "UKk",
          "urls" : null,
          "valueName" : "UK"
        } 
       ]
      },
      "valueSetName" : null,
      "valueSettings" : null
    },
    "visibleLines" : null,
    "writeRequiresMasterRead" : null
  },
  "FullName" : "Case.Community_Group__c"
}

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

1
  • 1
    I needed to update "valueSettings":[] instead of null as I was getting error related to deserialize null . Commented Jun 22, 2021 at 19:36

You must log in to answer this question.

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