You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否使用jsForce实现Schema.DescribeFieldResult并获取自定义字段元数据?

使用jsForce获取Salesforce自定义字段元数据

当然可以用jsForce实现和Apex中DescribeFieldResult一样的字段元数据查询功能,具体操作如下:

1. 先完成Salesforce连接

这是基础操作,先确保jsForce已经成功连接到你的Salesforce环境:

const jsforce = require('jsforce');
const conn = new jsforce.Connection({
  loginUrl: 'https://login.salesforce.com' // 沙盒环境替换成https://test.salesforce.com
});

conn.login('你的用户名', '密码+安全令牌', (err) => {
  if (err) return console.error(err);
  // 连接成功后执行字段查询操作
});

2. 查询对象所有自定义字段元数据

通过describeSObject方法获取指定对象的完整描述,从中筛选出自定义字段:

// 以Account对象为例
conn.describeSObject('Account', (err, objDesc) => {
  if (err) return console.error(err);
  
  // 筛选所有自定义字段
  const customFields = objDesc.fields.filter(field => field.isCustom);
  
  // 输出字段关键信息,按需提取属性
  customFields.forEach(field => {
    console.log(`字段API名: ${field.name}`);
    console.log(`字段标签: ${field.label}`);
    console.log(`数据类型: ${field.type}`);
    console.log(`是否必填: ${field.nillable ? '否' : '是'}`);
    console.log('---');
  });
});

3. 查询单个自定义字段的详细元数据

如果只需要某个特定字段的信息,用describeField方法直接查询:

// 查询Account对象的Custom_Field__c字段
conn.describeField('Account', 'Custom_Field__c', (err, fieldDesc) => {
  if (err) return console.error(err);
  
  console.log('字段详细信息:');
  console.log(`标签: ${fieldDesc.label}`);
  console.log(`API名称: ${fieldDesc.name}`);
  console.log(`数据类型: ${fieldDesc.type}`);
  console.log(`最大长度: ${fieldDesc.length}`);
  console.log(`是否唯一: ${fieldDesc.unique}`);
  console.log(`是否为外部ID: ${fieldDesc.externalId}`);
});

注意事项

返回的字段元数据属性和Apex中DescribeFieldResult的方法一一对应,比如field.isCustom对应Apex里的isCustom(),field.label对应getLabel(),你可以根据需求直接提取需要的属性。

内容的提问来源于stack exchange,提问作者user1517566

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 05:45:34