1.0.6 • Published 9 months ago
@_kyletan/sftoolkit v1.0.6
Here's a README.md for the provided JavaScript code:
Salesforce Data Management Utilities
This module provides utilities for handling CSV and JSON data interactions with Salesforce. It includes functions for converting CSV to JSON, performing bulk inserts into Salesforce, running anonymous Apex scripts, and creating CSV files from JSON data.
Prerequisites
- Node.js (version 14 or higher)
- Salesforce CLI (
sf
command installed and configured with Salesforce org aliases)
Import
- Import methods similar to below or reference this code: https://github.com/jkyletan/nodePhDreamin2025
import {getJsonDataFromCsv, createCsvFileFromJson, bulkInsertToSalesforce} from '@_kyletan/sftoolkit'
Functions
getJsonDataFromCsv(filePath)
- Description: Converts a CSV file to JSON format.
- Parameters:
filePath
: Path to the CSV file.
- Returns: A Promise that resolves to an array of JSON objects.
bulkInsertToSalesforce(filePath, sObject, orgAlias)
- Description: Performs a bulk insert into Salesforce using the Salesforce CLI.
- Parameters:
filePath
: Path to the CSV file to be imported.sObject
: The Salesforce object to insert into.orgAlias
: Alias for the Salesforce org to interact with.
- Returns: A Promise that resolves with the result of the bulk insert operation.
runAnonymousApexScript(filePathOfApexScript, orgAlias)
- Description: Executes an anonymous Apex script in Salesforce.
- Parameters:
filePathOfApexScript
: Path to the Apex script file.orgAlias
: Alias for the Salesforce org where the script will run.
- Returns: A Promise that resolves with the execution results.
createCsvFileFromJson(jsonData)
- Description: Converts JSON data to a CSV file.
- Parameters:
jsonData
: JSON data to convert to CSV.
- Returns: A Promise that resolves with the name of the newly created CSV file.
insertJsonDataToSalesforce(jsonData, sObject, orgAlias)
- Description: Converts JSON data to CSV and then performs a bulk insert into Salesforce.
- Parameters:
jsonData
: JSON data to insert into Salesforce.sObject
: The Salesforce object to insert into.orgAlias
: Alias for the Salesforce org to interact with.
- Returns: A Promise that resolves with the result of the bulk insert operation.
Usage
Here's how you might use these functions:
const { getJsonDataFromCsv, bulkInsertToSalesforce, runAnonymousApexScript, createCsvFileFromJson, insertJsonDataToSalesforce } = require('./path-to-your-file');
// Example: Reading CSV and inserting into Salesforce
getJsonDataFromCsv('path/to/your/file.csv')
.then(json => insertJsonDataToSalesforce(json, 'Account', 'myOrgAlias'))
.then(result => console.log('Insertion Result:', result))
.catch(err => console.error('Error:', err));
// Example: Running an anonymous Apex script
runAnonymousApexScript('path/to/your/apexScript.apex', 'myOrgAlias')
.then(result => console.log('Apex Execution Result:', result))
.catch(err => console.error('Error:', err));
// Example: Creating a CSV from JSON
createCsvFileFromJson([{name: 'John Doe', age: 30}])
.then(fileName => console.log('CSV file created:', fileName))
.catch(err => console.error('Error:', err));
Notes
Ensure your Salesforce CLI is set up correctly with the appropriate org aliases. The time-stamp package is used for generating unique file names based on the current timestamp. Error handling is implemented using Promises for asynchronous operations. The code assumes that the Salesforce CLI (sf) commands are available in the PATH.