我很难用简单的salesforce在自定义对象上创建字段。如果我追溯到代码中,我可以看到API正在返回成功,但我没有看到该字段是在对象上创建的。
我对Salesforce几乎一无所知,所以我可能在创建自定义对象时缺少一些关于如何设置权限或可见性的内容。我相信我有管理和API权限。
环境:
3.9.7
这是我的测试脚本。
# -*- coding: utf-8 -*-
import os
from simple_salesforce import Salesforce
secrets = dict()
secrets["username"] = os.environ["DEV_SALESFORCE_USER"]
secrets["password"] = os.environ["DEV_SALESFORCE_PASSWORD"]
secrets["security_token"] = os.environ["DEV_SALESFORCE_SECURITY_TOKEN"]
sf = Salesforce(
username=secrets["username"],
password=secrets["password"],
security_token=secrets["security_token"],
client_id="Data Engineering",
domain="test",
)
mdapi = sf.mdapi
obj_api_name = "NatesCustomObject__c"
obj_label = "Nates Custom"
field_api_name = "SomeText__c"
field_label = "Some Text"
# Try to delete the custom object so we are starting from scratch.
try:
mdapi.CustomObject.delete(obj_api_name)
except Exception:
pass
# Define the custom object.
custom_object = mdapi.CustomObject(
fullName=obj_api_name,
label=obj_label,
pluralLabel="Nates Custom Objects",
nameField=mdapi.CustomField(label="Name", type=mdapi.FieldType("Text")),
deploymentStatus=mdapi.DeploymentStatus("Deployed"),
sharingModel=mdapi.SharingModel("Read"),
)
# Create the custom object.
mdapi.CustomObject.create(custom_object)
# Define the custom field.
field = mdapi.CustomField(
fullName=f"{obj_api_name}.{field_api_name}", label=field_label, type=mdapi.FieldType("Text"), length=10
)
# Create the custom field.
mdapi.CustomField.create(field)
describe_response = sf.NatesCustomObject__c.describe()
for f in describe_response["fields"]:
print(f["name"])脚本输出:
Id
OwnerId
IsDeleted
Name
CreatedDate
CreatedById
LastModifiedDate
LastModifiedById
SystemModstamp发布于 2022-05-06 20:56:33
通过一些研究,我发现我需要使该字段在与用户关联的配置文件中可编辑。
# Get the current profile
profile = mdapi.Profile.read("Integration User")
# Add the new field to the profile
field_security = mdapi.ProfileFieldLevelSecurity(
field=f"{obj_api_name}.{field_api_name}", editable=True, readable=True
)
profile.fieldPermissions.append(field_security)
# Remove a couple of tabs that aren't owned by the current user.
uneditable_tabs = ["standard-LightningInstrumentation", "standard-Snippet"]
profile.tabVisibilities = [t for t in profile.tabVisibilities if t.tab not in uneditable_tabs ]
# Update the profile
mdapi.Profile.update(profile)https://stackoverflow.com/questions/72132475
复制相似问题