Reading Time: 2 minutes
Salesforce ContentVersion is a standard Salesforce object that represents a specific version of a file or document in Salesforce. It is used to store and manage various types of content, such as attachments, files, or document versions. Each ContentVersion record contains information about the file, including its title, file name, file extension, and other metadata. ContentVersion records can be associated with specific records in Salesforce, such as accounts, opportunities, or custom objects, allowing users to easily access and manage the related files.
Node-fetch is a JavaScript library that provides an easy and convenient way to make HTTP requests in a Node.js environment. It allows you to send HTTP requests to remote servers and receive responses, making it useful for tasks such as fetching data from APIs or downloading files. Node-fetch simplifies the process of making HTTP requests by providing a simple and intuitive API.
Let’s say you want to get the file info without downloading it. For example, you want to know the file name and extension. The way to do it is to make a request to:
https://myinstance-dev-ed.my.salesforce.com/services/data/v52.0/sobjects/ContentVersion/0688J000000Di2xTBA
First, let’s have a method to get the access token
const getJsForceConnection = async () => {
const username = "******";
const password = "******";
const securityToken = "******";
const salesforceUrl = "https://myinstance-dev-ed.my.salesforce.com";
const salesforceApiVersion = "52.0";
const options = {instanceUrl: salesforceUrl, loginUrl: salesforceUrl, version: salesforceApiVersion};
const conn = new jsForce.Connection(options);
await conn.login(username, password + securityToken);
return conn;
}
And then let’s make a request to get the file info with node-fetch
const getFileInfo = async (conn, salesforceFileId, salesforceApiVersion) => {
const url = `${conn.instanceUrl}/services/data/v${salesforceApiVersion}/sobjects/ContentVersion/${salesforceFileId}`;
const ops = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'OAuth '+conn.accessToken
}
};
const res = await fetch(url, ops);
if(!res.ok) throw new Error(`error getting file info from ${url} status code: ${res.status} status message ${res.statusText}`);
const json = await res.json();
return {
fileTitle: json.Title,
fileName: `${json.Title}.${json.FileExtension}`,
fileExtension: json.FileExtension,
fileId: salesforceFileId,
};
}