It's All Writing.

Writing makes you happy.

How to get Salesforce sObject's fields information by node

It's simple. You can use jsforce for this purpose. The describe method returns sObject's meta data. The environment and code is below.

Enviroment

  • node
  • jsforce
  • lodash

Code

const jsforce = require('jsforce');
const lodash = require('lodash');

const conn = new jsforce.Connection({
  loginUrl: 'https://login.salesforce.com'
});

execAll();

async function execAll() {
  try {
    await login();

    const fields = await getFields('Account');
    # => ['Id', 'IsDeleted', 'MasterRecordId', 'Name', .... ]

    return;
  }
  catch(err) {
    console.log(err);
  }
}

function login() {
  return new Promise(function(resolve, reject) {
    conn.login('your_sf_account', 'your_sf_password', function(err, res) {
      if (err) {
        reject(err);
      }
      resolve();
    });
  });
}

function getFields(sobject_name) {
  return new Promise(function(resolve, reject) {
    conn.sobject(sobject_name).describe(function(err, meta) {
      if (err) {
        console.error(err);
        reject(err);
      }
      let fields = lodash.map(meta.fields, (field) => {
        return field.name;
      })
      resolve(fields);
    });
  });
}