使用Python更新Salesforce中的自定义选择列表

时间:2019-04-05 14:14:14

标签: python api soap 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更新现有选择列表中的值。 但是当我尝试用更新标签替换create标签时,它告诉我在该位置无效的标签如“ fullName”,“ label”等。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

经过数天的尝试,我终于找到了一种更新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)