如何通过API在Salesforce标准对象Contact中创建自定义字段
通过API在Salesforce Contact对象创建自定义字段
前提条件
- 拥有足够的API权限:需要
Customize Application或Modify All Data权限,确保能创建自定义字段 - 明确字段属性:提前确定字段类型(如文本、多选列表、数字等)、标签、API名称(必须以
__c结尾)、长度、是否必填等参数
使用REST API创建
发送POST请求到Salesforce REST API端点,目标对象为CustomField(自定义字段本身是标准对象):
请求端点
/services/data/v59.0/sobjects/CustomField
注:将v59.0替换为你正在使用的Salesforce API版本
请求头
Authorization: Bearer {你的访问令牌}Content-Type: application/json
请求体示例(创建文本字段)
{ "FullName": "Contact_Preferred_Contact_Method__c", "Label": "Preferred Contact Method", "Type": "Text", "Length": 50, "Visible": true, "Required": false, "TableEnumOrId": "Contact" }
关键参数说明
FullName:字段的API名称,必须以__c结尾,且全局唯一Label:字段在Salesforce界面显示的名称Type:字段类型,可选值如Text、Picklist、Number、Checkbox等TableEnumOrId:指定关联的标准对象,这里填Contact即可
使用SOAP API创建
通过SOAP API的create操作创建自定义字段,以下是SOAP请求体示例:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:enterprise.soap.sforce.com"> <soapenv:Header> <urn:SessionHeader> <urn:sessionId>{你的会话ID}</urn:sessionId> </urn:SessionHeader> </soapenv:Header> <soapenv:Body> <urn:create> <urn:sObjects xsi:type="urn:CustomField" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <urn:FullName>Contact_Preferred_Contact_Method__c</urn:FullName> <urn:Label>Preferred Contact Method</urn:Label> <urn:Type>Text</urn:Type> <urn:Length>50</urn:Length> <urn:Visible>true</urn:Visible> <urn:Required>false</urn:Required> <urn:TableEnumOrId>Contact</urn:TableEnumOrId> </urn:sObjects> </urn:create> </soapenv:Body> </soapenv:Envelope>
注意事项
- 字段API名称(
FullName)不能与现有标准/自定义字段重名 - 不同字段类型需要额外参数:比如创建多选列表字段时,需添加
ValueSet参数定义选项 - 创建后可通过
GET /services/data/vXX.X/sobjects/Contact/describe验证字段是否成功添加 - 确保API版本支持你要创建的字段类型(部分新字段类型需要较高版本的API)
内容的提问来源于stack exchange,提问作者Roland
相关产品推荐
相关产品推荐

