angular-normalize-salesforce

Angular module to normalize and denormalize salesforce field and object names


Keywords
angular, salesforce, spa
License
MIT
Install
bower install angular-normalize-salesforce

Documentation

Angular Normalize Salesforce

Build Status devDependency Status

Angular module to normalize and denormalize Salesforce fields and object names.

Installation

Download the .js file from the dist folder or use Bower

$ bower install angular-normalize-salesforce --save

After including the dist file into your project you have to add the module as a dependency to your Angular project module:

angular.module('myModule', ['angular-normalize-salesforce'])

Usage

The normalizeSalesforce service offers 3 main methods:

  • normalize(part)
    Supports string, array, object. It removes __c from the string, or all strings in the array. If part is an object, all keys are normalized.

  • denormalize(part, sObject)
    Also supports string, array and object. Needs the name of the sObject as a parameter to denormalize only custom fields and keep standard fields plain.

  • denormalizeObjectName(name)
    Supports only a plain string. Adds __c to the given name, if the object is not a standard object.
    This is a separate function to allow custom fields with the name of a standard object (e.g. Account__c)

sObject Support

In order to support all Salesforce standard objects, all standard fields have to be defined in the source code. We currently support all standard objects from API v34.0

Example Usage

angular.module('myModule', ['angular-normalize-salesforce']);
angular.module('myModule')
  .factory('exampleFactory', ['normalizeSalesforce', function (normalizeSalesforce) {
    var sObject = {
      'Field1__c': 'value1',
      'field2__c': 'value2',
      'ns__Field1__c': 'value3',
      'Name': 'value4'
    };

    /**
     * Should return
     * {
     *   'field1': 'value1',
     *   'field2': 'value2',
     *   'ns_field1': 'value3',
     *   'name': 'value4'
     * }
     */
    console.log(normalizeSalesforce.normalize(sObject));

    var object = {
      'field1': 'value1',
      'ns__field2': 'value2',
      'name': 'value3'
    }

    /**
     * Should return
     * {
     *   'field1__c': 'value1',
     *   'ns__field2__c': 'value2',
     *   'name': 'value3'
     * }
     */
    console.log(normalizeSalesforce.denormalize(object, 'CustomObject__c'));

    return 'yeah :)';
  }]);

Contributing

Prepare your environment

  • Install Node.js and NPM
  • Install global dev dependencies: npm install -g gulp
  • Install local dev dependencies: npm install

Build

  • Build the whole project with gulp

[Internal] Load describe data from Salesforce REST JS

DEBUG.pbJSRestConnector.client.apiVersion = "v37.0"

allObjects = []

DEBUG.pbJSRestConnector.global(function(cb){allObjects = cb})

allObjectsLowercased = _(allObjects.sobjects).filter({custom: false}).map('name').map(function(name){return name.toLowerCase()}).value()

standardFields = {}

_(allObjectsLowercased).each(function(objectName){DEBUG.pbJSRestConnector.describe(objectName,function(cb){standardFields[objectName] = _(cb.fields).filter({custom:false}).map('name').map(function(name){return name.toLowerCase()}).value()})}).value()

[Internal] Add fields from new dataSet

_(newData).each(function(value, key){if (!_.has(original, key)) {original[key] = value } else {_.each(value,function(field){if (!_.contains(original[key], field)) {original[key].push(field) } }) } }).value()