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

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

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方法?

  1. {
  2. 'fullName': 'Experiencing_Homesickness__c',
  3. ...
  4. ...
  5. 'type': 'Picklist',
  6. 'unique': None,
  7. 'valueSet': {
  8. 'controllingField': None,
  9. 'restricted': True,
  10. 'valueSetDefinition': None,
  11. 'valueSetName': 'Student_Challenge_Experience_Homesick',
  12. 'valueSettings': [
  13. ]
  14. },
  15. 'visibleLines': None,
  16. 'writeRequiresMasterRead': None
  17. },

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

  1. 'valueSet': {
  2. 'controllingField': None,
  3. 'restricted': None,
  4. 'valueSetDefinition': {
  5. 'sorted': False,
  6. 'value': [
  7. {
  8. 'fullName': '3',
  9. 'color': None,
  10. 'default': False,
  11. 'description': None,
  12. 'isActive': None,
  13. 'label': '3'
  14. },
  15. {
  16. 'fullName': '4',
  17. 'color': None,
  18. 'default': False,
  19. 'description': None,
  20. 'isActive': None,
  21. 'label': '4'
  22. },
  23. {
  24. 'fullName': '5+',
  25. 'color': None,
  26. 'default': False,
  27. 'description': None,
  28. 'isActive': None,
  29. 'label': '5+'
  30. }
  31. ]
  32. },
  33. 'valueSetName': None,
  34. 'valueSettings': [
  35. ]
  36. },
  37. 'visibleLines': None,
  38. 'writeRequiresMasterRead': None
  39. },
英文:

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)?

  1. {
  2. 'fullName': 'Experiencing_Homesickness__c',
  3. ...
  4. ...
  5. 'type': 'Picklist',
  6. 'unique': None,
  7. 'valueSet': {
  8. 'controllingField': None,
  9. 'restricted': True,
  10. 'valueSetDefinition': None,
  11. 'valueSetName': 'Student_Challenge_Experience_Homesick',
  12. 'valueSettings': [
  13. ]
  14. },
  15. 'visibleLines': None,
  16. 'writeRequiresMasterRead': None
  17. },

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

  1. 'valueSet': {
  2. 'controllingField': None,
  3. 'restricted': None,
  4. 'valueSetDefinition': {
  5. 'sorted': False,
  6. 'value': [
  7. {
  8. 'fullName': '3',
  9. 'color': None,
  10. 'default': False,
  11. 'description': None,
  12. 'isActive': None,
  13. 'label': '3'
  14. },
  15. {
  16. 'fullName': '4',
  17. 'color': None,
  18. 'default': False,
  19. 'description': None,
  20. 'isActive': None,
  21. 'label': '4'
  22. },
  23. {
  24. 'fullName': '5+',
  25. 'color': None,
  26. 'default': False,
  27. 'description': None,
  28. 'isActive': None,
  29. 'label': '5+'
  30. }
  31. ]
  32. },
  33. 'valueSetName': None,
  34. 'valueSettings': [
  35. ]
  36. },
  37. 'visibleLines': None,
  38. 'writeRequiresMasterRead': None
  39. },

答案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:

确定