SalesForce SObject
Allows queries and CRUD operations to be performed on a SalesForce SObject with minimal setup and a friendly API.
Simply give it the SObject's name (e.g. 'Account'
) and an object which defines friendly names for the properties in which you're interested (e.g. { primaryContactName: 'Primary_Contact__r.Name' }
). Now you can query and update records using the friendly names, and this class takes care of the conversion to and from the SalesForce format.
Basic Usage
Either extend SObject
to override its objectName
and propertyMap
properties, or simply create an instance by passing those properties into this class's constructor. Either approach will allow you to use the default implementations of the CRUD methods (e.g. query()
, insert()
, etc.) which automatically handle the property name conversion.
// AccountStorage.js: This shows how you extend SObject for your various objects. Keeping these declarations// in separate files is a good idea if you're working with many different objects. ; { return 'Account'; } { return id: 'Id' name: 'Name' primaryContactId: 'Primary_Contact__c' primaryContactEmail: 'Primary_Contact__r.Email' ; }
// index.js;; // Create a connection, which handles sending authenticated requests to the API.let connection = loginUrl: 'https://test.salesforce.com/services/oauth2/token' clientId: '3MVG9lKcPoNINVBJSoQsNCD.HHDdbugPsNXwwyFbgb47KWa_PTv' clientSecret: '5678471853609579508' username: 'steve.stevens@example.com' password: 'I❤️SalesForce'; // Instantiate the class we created for handling Account objects.let accountStorage = connection ; // We can also just create an SObject by passing its constructor those `objectName` and `propertyMap`// properties. This is handy for simple scripting.let contactStorage = connection objectName: 'Contact' propertyMap: id: 'Id' accountId: 'AccountId' firstName: 'FirstName' lastName: 'LastName' email: 'Email' ; let accountId; // Get a specific account by nameaccountStorage;
For more detailed information, check out this module's API docs and the SalesForce documentation on setting up REST API authorization.
Development
This module is written in ES2015, but is currently transpiled to ES5 for distribution.
Building
npm run build
There's also a git pre-commit hook which automatically transpiles and regenerates the docs upon commit.
Testing
npm test