首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >simple-salesforce创建自定义字段

simple-salesforce创建自定义字段
EN

Stack Overflow用户
提问于 2022-05-05 19:10:19
回答 1查看 32关注 0票数 0

我很难用简单的salesforce在自定义对象上创建字段。如果我追溯到代码中,我可以看到API正在返回成功,但我没有看到该字段是在对象上创建的。

我对Salesforce几乎一无所知,所以我可能在创建自定义对象时缺少一些关于如何设置权限或可见性的内容。我相信我有管理和API权限。

环境:

3.9.7

  • simple-salesforce: 1.11.6

这是我的测试脚本。

代码语言:javascript
复制
# -*- 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"])

脚本输出:

代码语言:javascript
复制
Id
OwnerId
IsDeleted
Name
CreatedDate
CreatedById
LastModifiedDate
LastModifiedById
SystemModstamp
EN

回答 1

Stack Overflow用户

发布于 2022-05-06 20:56:33

通过一些研究,我发现我需要使该字段在与用户关联的配置文件中可编辑。

代码语言:javascript
复制
# 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72132475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档