首页 > 解决方案 > 使用 Python 在 Salesforce 中更新自定义选项列表

问题描述

目前我正在尝试将值添加到我们在 Salesforce 中使用的自定义选项列表中。目前,经过多天的尝试,我能够创建一个新的自定义选项列表,如下所示:

url2 = "https://INSTANCE.salesforce.com/services/Soap/m/45.0/ORGID"
headers2 = {'content-type': 'text/xml; charset=utf-8', "SOAPAction":"POST"}

body2 = """<?xml version="1.0" encoding="utf-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:apex="http://soap.sforce.com/2006/08/apex" 
xmlns:cmd="http://soap.sforce.com/2006/04/metadata" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Header>
      <cmd:SessionHeader>
         <cmd:sessionId>{0}</cmd:sessionId>
      </cmd:SessionHeader>
    </soapenv:Header>
  <soapenv:Body>
      <create xmlns="http://soap.sforce.com/2006/04/metadata">
         <metadata xsi:type="CustomField">
            <fullName>Case.verursacht_durch_MA2__c</fullName>
            <label>verursacht_durch_MA2</label>
            <type>Picklist</type>
            <valueSet>
                <restricted>true</restricted>
                <valueSetDefinition>
                    <sorted>false</sorted>
                    <value>
                        <fullName>ValueTest</fullName>
                        <default>false</default>
                        <label>ValueTest</label>
                    </value>
                </valueSetDefinition>
            </valueSet>
         </metadata>
        </create>
  </soapenv:Body>
</soapenv:Envelope>""".format(sessionId)

response2 = requests.post(url2,data=body2,headers=headers2)

现在我尝试使用 xml 更新已经存在的选项列表中的值。但是当我试图用更新标签替换创建标签时,它告诉我像“fullName”“label”等标签在这个位置是无效的。

任何帮助将非常感激!

标签: pythonapisoapsalesforce

解决方案


经过几天的尝试,我终于找到了一种更新 Salesforce 中现有选项列表的方法!

使用这个 --> https://github.com/gbarger/PySalesforce

import sys, os

sys.path.append(os.path.dirname(os.path.realpath(__file__)) + '/PySalesforce')
import pysalesforce

# IS_PRODUCTION is a bool value
# set it to False when working on sandbox
login = pysalesforce.Authentication.get_oauth_login("USERNAME", "passwordSECRETKEY",
                                                    "CLIENT_ID",
                                                    "CLIENT_SECRET", IS_PRODUCTION)

# Instance Url is inside the login variable, typically you only need to append
# services/Soap/m/38.0/ORGID
# 38.0 is the used api version
metadataUrl = 'METADATA_URL'

# Here im getting the current picklist which is a custom picklist used in our Cases
# The picklist im editing is called verursacht_durch_MA2 and you need to append __c because its a custom field
getPicklist = pysalesforce.Metadata.read_metadata(metadata_type="CustomField", full_names="Case.verursacht_durch_MA2__c",
                                                  session_id=login['access_token'], metadata_url=metadataUrl,
                                                  client_name="Client-Name")

# Here im changing some names inside the existing picklist.
# Values can be added when appending to CustomField['valueSet']['valueSetDefinition']['value']
# Values look like this:
# {'fullName': 'test456',    'color': None,    'default': False,    'description': None,    'isActive': True,    'label': 'test456label'}
CustomField = getPicklist[0]
CustomField['valueSet']['valueSetDefinition']['value'][0]['fullName']="test456"
CustomField['valueSet']['valueSetDefinition']['value'][0]['label']="test456label"
CustomField['valueSet']['valueSetDefinition']['value'][0]['isActive']=True

# Here im updating the picklist, if all_or_none is True it will rollback all changes if any error occurs,
# if its set to False it will keep all already made changes on error
response = pysalesforce.Metadata.update_metadata(metadata_list=[CustomField], client_name="Client-Name", session_id=login['access_token'],
                                             metadata_url=metadataUrl, all_or_none=False)

推荐阅读