如何通过Salesforce Metadata API和simple-salesforce读取Picklist值。

huangapple go评论162阅读模式
英文:

How to read Picklist values via Salesforce Metadata API & simple-salesforce

问题

我正在使用simple-salesforce从一些自定义对象中读取元数据:metadata = _sf.mdapi.CustomObject.read(sf_object_name)

一般情况下,这可以正常工作,但对于一些下拉列表字段,选项以命名的ValueSet形式返回,而不是选项列表(请参见下文)。

如何获取实际的下拉列表选项?我是否需要使用ValueSetName(例如Student_Challenge_Experience_Homesick)调用不同的API方法?

{
  'fullName': 'Experiencing_Homesickness__c',
  ...
  ...
  'type': 'Picklist',
  'unique': None,
  'valueSet': {
    'controllingField': None,
    'restricted': True,
    'valueSetDefinition': None,
    'valueSetName': 'Student_Challenge_Experience_Homesick',
    'valueSettings': [

    ]
  },
  'visibleLines': None,
  'writeRequiresMasterRead': None
},

为了完整起见,某些字段确实返回带有其标签和值的下拉列表值列表:

  'valueSet': {
    'controllingField': None,
    'restricted': None,
    'valueSetDefinition': {
      'sorted': False,
      'value': [
        {
          'fullName': '3',
          'color': None,
          'default': False,
          'description': None,
          'isActive': None,
          'label': '3'
        },
        {
          'fullName': '4',
          'color': None,
          'default': False,
          'description': None,
          'isActive': None,
          'label': '4'
        },
        {
          'fullName': '5+',
          'color': None,
          'default': False,
          'description': None,
          'isActive': None,
          'label': '5+'
        }
      ]
    },
    'valueSetName': None,
    'valueSettings': [

    ]
  },
  'visibleLines': None,
  'writeRequiresMasterRead': None
},
英文:

I am using simple-salesforce to read metadata from some CustomObjects: metadata = _sf.mdapi.CustomObject.read(sf_object_name)

This generally works, EXCEPT for a number of Picklist fields: the options return as a named ValueSet instead of a list of options (see below).

How can I get the actual Picklist options? Do I need to call a different API method with the ValueSetName (ex. Student_Challenge_Experience_Homesick)?

    {
      'fullName': 'Experiencing_Homesickness__c',
      ...
      ...
      'type': 'Picklist',
      'unique': None,
      'valueSet': {
        'controllingField': None,
        'restricted': True,
        'valueSetDefinition': None,
        'valueSetName': 'Student_Challenge_Experience_Homesick',
        'valueSettings': [
          
        ]
      },
      'visibleLines': None,
      'writeRequiresMasterRead': None
    },

For completeness' sake: some fields DO return a list of picklist values with their label & value:

      'valueSet': {
        'controllingField': None,
        'restricted': None,
        'valueSetDefinition': {
          'sorted': False,
          'value': [
            {
              'fullName': '3',
              'color': None,
              'default': False,
              'description': None,
              'isActive': None,
              'label': '3'
            },
            {
              'fullName': '4',
              'color': None,
              'default': False,
              'description': None,
              'isActive': None,
              'label': '4'
            },
            {
              'fullName': '5+',
              'color': None,
              'default': False,
              'description': None,
              'isActive': None,
              'label': '5+'
            }
          ]
        },
        'valueSetName': None,
        'valueSettings': [
          
        ]
      },
      'visibleLines': None,
      'writeRequiresMasterRead': None
    },

答案1

得分: 1

听起来像是一个全局选项列表(可以在不同的对象上重复使用的选项列表定义,在一个地方维护,如果添加一个新的值,它会在所有地方都添加)。https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_globalvalueset.htm

看看是否可以使用 _sf.mdapi.GlobalValueSet.read('Student_Challenge_Experience_Homesick') 来获取它。

或者可以尝试另一种方法来查询选项列表:https://stackoverflow.com/a/76840387/313628

英文:

Sounds like a global picklist (picklist definition that can be reused, even on different objects, maintained in 1 place and if you add a new value - it adds everywhere). https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_globalvalueset.htm

See if you can pull it with _sf.mdapi.GlobalValueSet.read('Student_Challenge_Experience_Homesick')

Or alternative approach is to query the picklists: https://stackoverflow.com/a/76840387/313628

huangapple
  • 本文由 发表于 2023年8月11日 03:55:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定