Utilities

Utilities

Source:

Utility methods. More coming soon.

Methods

(static) copyRecord(tableName, whereClauseToFindRecord, recPropUpdateObjopt) → {Promise.<Object>}

Source:
Since:
  • 1.0.0

Copies a record from table tableName by identifying a copy source (i.e., a single record matched by a WHERE clause, whereClauseToFindRecord) and then creating the same or slightly modified record in table tableName.

The record that servers as a copy source may have its field values updated/changed by providing an object, recPropUpdateObj, which should have as its keys the field names in need of value updates and key values as the desired new values. Only fields that are editable should be specified in recPropUpdateObj (i.e., you cannot change or update a field that has a Type of 'AUTONUMBER', a formula field, etc.).

Example
// Copy a record and change some of its values
const caspio = require('caspio-sdk')(caspioCredentials);

// original record corresponding to email 'Lelah.Hoppe@gmail.com' appears below
// we want to copy this record but change values for a few fields:
// `First_Name`, `Last_Name`, `Title`, `Email`, `Profile_Photo`, `About_Section`
// (note that the `Password` field value cannot be copied)
const lelahRecord = {
  Physician_ID: '6T2C9HW8',
  Date_Created: '2020-10-07T04:54:19',
  First_Name: 'Lelah',
  Last_Name: 'Hoppe',
  Title: 'Adolescent Medicine Specialist',
  Full_Name: 'Lelah Hoppe',
  Gender: 'Other',
  Email: 'Lelah.Hoppe@gmail.com',
  Account_Status: true,
  Profile_Status: false,
  Profile_Photo: '/Demo/LelahHoppe.png',
  Specialties: {
    '3': 'Family Medicine',
    '6': 'Obstetrics and Gynecology',
    '8': 'Pain Management',
    '9': 'Pathology'
  },
  Accepting_New_Patients: true,
  About_Section: 'Exercitationem ... labore.',
  Affiliations: 'American College of Endocrinology\n' +
    'The Endocrine Society\n' +
    'American Diabetes Association',
  Board_Certifications: 'American Board of Pediatrics',
  Education: 'David Geffen School of Medicine - MD\n' +
    'Yale - Residency, Ophthalmology\n' +
    'Yale - Fellowship, Cataracts and Refractive Surgery',
  Languages: { '2': 'English', '4': 'French', '7': 'Japanese', '9': 'Spanish' },
  Office_Name: 'Davis Family Medical Group',
  Office_Address: '0519 Mustafa Via',
  Office_City: 'New Bradlyhaven',
  Office_State: 'MI',
  Office_Zip: '50198',
  Office_Phone: '967-841-6054',
  ViewCount: 3654
}

async function copyTableRecord() {
  const newFieldValues = {
    First_Name: 'Pam',
    Last_Name: 'Halpert',
    Title: 'Artist',
    Email: 'pam.halpert@gmail.com',
    Profile_Photo: '',
    About_Section: 'I grew up in Scranton and I am super cool!'
  }
  const whereClause = `Email = 'Lelah.Hoppe@gmail.com'`;
  const copiedRecord = await caspio.utils.copyRecord('Demo_Physicians', whereClause, newFieldValues);
  console.log(copiedRecord);
  return copiedRecord;
}

copyTableRecord();

// sample return value
{
  Physician_ID: '0R50T9HF',
  Date_Created: '2020-10-07T04:54:19',
  First_Name: 'Pam',
  Last_Name: 'Halpert',
  Title: 'Artist',
  Full_Name: 'Pam Halpert',
  Gender: 'Other',
  Email: 'pam.halpert@gmail.com',
  Account_Status: true,
  Profile_Status: false,
  Profile_Photo: '',
  Specialties: {
    '3': 'Family Medicine',
    '6': 'Obstetrics and Gynecology',
    '8': 'Pain Management',
    '9': 'Pathology'
  },
  Accepting_New_Patients: true,
  About_Section: 'I grew up in Scranton and I am super cool!',
  Affiliations: 'American College of Endocrinology\n' +
    'The Endocrine Society\n' +
    'American Diabetes Association',
  Board_Certifications: 'American Board of Pediatrics',
  Education: 'David Geffen School of Medicine - MD\n' +
    'Yale - Residency, Ophthalmology\n' +
    'Yale - Fellowship, Cataracts and Refractive Surgery',
  Languages: { '2': 'English', '4': 'French', '7': 'Japanese', '9': 'Spanish' },
  Office_Name: 'Davis Family Medical Group',
  Office_Address: '0519 Mustafa Via',
  Office_City: 'New Bradlyhaven',
  Office_State: 'MI',
  Office_Zip: '50198',
  Office_Phone: '967-841-6054',
  ViewCount: 3654
}
Parameters:
Name Type Attributes Default Description
tableName string

Name of table (case-insensitive)

whereClauseToFindRecord string

WHERE clause to unambiguously determine the record to be copied. If no record is found, then an error is thrown to indicate this. If more than one record is found, then an error is thrown to indicate that a record could not be unambiguously copied.

recPropUpdateObj object <optional>
{}

Object with key-value pairs in the form <fieldName>:<newFieldValue> where fieldName is the Name of a field from table tableName and newFieldValue is a new or modified value to be used for the newly copied record instead of the original value(s) from the copy source.

This object effectively allows you to change, modify, or update editable values from the copy source before creating the copied/modified record. Attempting to write to read-only fields (e.g., fields with type 'TIMESTAMP', 'AUTONUMBER', etc., or formula fields) will cause an error.

Returns:

The copied record

Type
Promise.<Object>