能否使用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
相关产品推荐
相关产品推荐

