API Reference: Organization Customization

Preamble

Solution Partners can use Solve CRM’s main JavaScript API to automate the customization of new organizations.

Enviroment

Login to a Solve CRM admin account and active on the developer console panel.

Developer console

Creating a simple form element

Command

FieldsData[itemType].actionAdd('fieldType', 'fieldLabel', column, refId, false, function (r, ok) { // r.id – id of the newly created field });

Parameters

itemType
[1, 2, or 40] for contact, blog, company
fieldType
[text, select, multiselect, date, textarea, email, url, number, phonenumber, currency, separator, sectionbreak, uniqueid]
fieldLabel
{user string}
column
[1, 2]
refId
{fieldId} field before which to create the new one, or 0 to position at the end of the list of existing fields

Example

FieldsData[2].actionAdd('text', 'My new text field', 1, 0, false, function (r, ok) { });

Adding options to a select or multi-select field

Command

FieldsData[itemType].actionOptions(fieldId, optionsList, function (r, ok) { });

Parameters

itemType
[1, 2, or 40] for contact, blog, company
fieldId
{fieldId}
optionslist
{user string, …} comma delimited list of options

Example

FieldsData[2].actionAdd('multiselect', 'Color', 1, 0, false, function (r, ok) {
  FieldsData[2].actionOptions(r.id, ['Red', 'Blue', 'Green'], function (r, ok) { });
});

Adding category tags

Command

ListsData.actionAdd(itemType, 'tagLabel', function (r, ok) { });

Note: There is currently an issue creating multiple category tags at the same time. Bypass by creating one category tag at a time.

Parameters

itemType
[1, 2, or 40] for contact, blog, company
tagLabel
{user string}

Example

ListsData.actionAdd(1, 'My new contact category tag’', function (r, ok) { });

Reposition a form element

Command

FieldsData[itemType].actionPositionField(fieldId, 'beforeFieldName', function (r, ok) { });

Parameters

itemType
[1, 2, or 40] for contact, blog, company
fieldId
{fieldId}
beforeFieldName
{fieldName} position field below this fieldName

Field Names are listed under Solve menu > My Account > API Reference.

It also can be number of the column to move the field to (1 or 2), in this case the field will be placed at the bottom of the column.

Example

FieldsData[1].actionPositionField(1234, 'firstname', function (r, ok) { });

Putting it together in a use-case

/**
 * Utility functions
 */

function sayError(msg) {
  console.warn('ERROR: ' + msg);
}

/**
 * Delete fields
 * @param {Array} fieldsArr Ids of the fields to delete
 * @param {Function} callback Function called when operation is finished
 */
function deleteFields(fieldsArr, callback, fieldIndex) {
  if (!Ext.isDefined(fieldIndex)) fieldIndex = 0;

  store.actionRemoveField(fieldsArr[fieldIndex], function (r, ok) {
    if (!ok) {
      sayError('cannot delete field "' + fieldsArr[fieldIndex] + '"');
      callback(false);
      return;
    }

    fieldIndex++;

    if (fieldIndex < fieldsArr.length) {
      deleteFields(fieldsArr, callback, fieldIndex);
    } else {
      callback(true);
    }
  });

}

/**
 * Create fields
 * @param {Array} fieldsArr Fields data in the following view to create new fields:
 *  {
 *    type: 'sectionbreak',
 *    label: 'My section break'
 *    column: RECORD_COLUMN_RIGHT, // optional, column index to create the field in, left column is default
 *    callback: function (r) { // optional, function called for each field created, r.id - id of the newly created field
 *    }
 *  }
 * @param {Function} callback Function called when operation is finished
 *  The function is called with two parameters:
 *    @param {Boolean} ok True if all fields were created successfully
 *    @param {Array} fieldIds  Array of ids of the fields created
 */
function createFields(fieldsArr, callback, fieldIndex, fieldIds) {
  var fieldData;

  if (!Ext.isDefined(fieldIndex)) {
    fieldIndex = 0;
    fieldIds = [];
  }

  fieldData = fieldsArr[fieldIndex];

  store.actionAdd(fieldData.type, fieldData.label, fieldData.column || RECORD_COLUMN_LEFT, 0, false, function (r, ok) {
    if (!ok) {
      sayError('cannot create field "' + fieldData.name + '"');
      callback(false);

      return;
    }

    // some additional actions necessary for the field created
    if (Ext.isFunction(fieldData.callback)) {
      fieldData.callback(r);
    }

    fieldIndex++;
    fieldIds.push(r.id);

    if (fieldIndex < fieldsArr.length) {
      createFields(fieldsArr, callback, fieldIndex, fieldIds);
    } else {
      callback(ok, fieldIds);
    }
  });

}

/**
 *  Custom process routine
 */

var store = FieldsData[CONTACT_ITEM_TYPE];

// delete the three fields specified in fieldsToDelete
deleteFields(['assistantname', 'businessmessenger', 'otherphone'], function (ok) {
  if (!ok) {
    sayError('failed to delete fields');
    return;
  }

    // create a category tag for contacts
    ListsData.actionAdd(CONTACT_ITEM_TYPE, 'My category tag', function (tagRec, ok) {
    var fieldsArr = [{
      type: 'sectionbreak',
      label: 'My section break'
    }, {
      type: 'text',
      label: 'My text field'
    }, {
      type: 'select',
      label: 'My select option field',
      column: RECORD_COLUMN_RIGHT,
      callback: function (r) {
        store.actionOptions(r.id, ['Red', 'Blue', 'Green'], function (r, ok) { });
      }
    }, {
      type: 'multiselect',
      label: 'My multi-select option field',
      callback: function (r) {
        store.actionOptions(r.id, ['Dark red', 'Dark blue', 'Dark green'], function (r, ok) { });
      }
    }, {
      type: 'date',
      label: 'My date field'
    }, {
      type: 'textarea',
      label: 'My text area field'
    }, {
      type: 'email',
      label: 'My email address field'
    }, {
      type: 'url',
      label: 'My website field'
    }, {
      type: 'separator',
      label: '',
      column: RECORD_COLUMN_RIGHT,
    }, {
      type: 'number',
      label: 'My number field'
    }, {
      type: 'phonenumber',
      label: 'My phone number field'
    }, {
      type: 'currency',
      label: 'My currency field'
    }, {
      type: 'uniqueid',
      label: 'My unique record identifier'
    }];

    if (!ok) {
      sayError('cannot create a tag');
      return;
    }

    createFields(fieldsArr, function (ok, fieldIds) {
      var len;

      if (!ok) {
        sayError('failed to create fields');
        return;
      }

      // make the last two fields visible with the tag only
      len = fieldIds.length;

      ListsData.actionEditFields(tagRec.id, fieldIds[len - 1], function (r, ok) {
        if (!ok) {
          sayError('failed to assign field "' + fieldsArr[len - 1].label + '" to a tag');
        }
      });
      ListsData.actionEditFields(tagRec.id, fieldIds[len - 2], function (r, ok) {
        if (!ok) {
          sayError('failed to assign field "' + fieldsArr[len - 2].label + '" to a tag');
        }
      });

      // move 3rd field after 'lastname' field
      store.actionPositionField(fieldIds[2], 'lastname', function (r, ok) {
        if (!ok) {
          sayError('failed to reposition field "' + fieldsArr[2].label + '"');
        }
      });

    });

    }); 

});

Posted by