本文介绍了使用 Python 在 Salesforce 中更新自定义选项列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试向我们在 Salesforce 中使用的自定义选项列表添加值.目前,经过多天的尝试,我能够像这样创建一个新的自定义选择列表:

Currently im trying to add values to a custom picklist we are using inside Salesforce.Currently, after multiple days of trying, im able to create a new custom picklist like this:

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"等标签在这个位置是无效的.

Now im trying to update values in an already existing picklist using xml.But when im trying to replace the create tag with a update tag, it tells me that tags like "fullName" "label" etc. are invalid at this location.

任何帮助将不胜感激!

推荐答案

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

I finally, after days of trying, found a way to update existing picklists in 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)

这篇关于使用 Python 在 Salesforce 中更新自定义选项列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 06:54