Skip to main content

RevOps SDK

RevOps functions includes a built in SDK for accessing additional functionality. This SDK is automatically imported and is available globally for use.

interface RevOps {
salesforce: {
query: <T extends Record<string, any>>(query: string) => Promise<T[]>;
}
}

Salesforce

Customers that use Salesforce are able to access their Salesforce data in order to enhance their functions. Authentication is automatically handled using your connected user account.

SOQL

The Salesforce Object Query Language (SOQL) offers a flexible way of getting any data point that's stored in Salesforce. Any field or set of fields on any object (standard or custom) can be accessed.

Finding the Opportunity Id
const oppId = data.deal.externalConnections.find(conn => conn.externalSystem === "salesforce" && conn.externalObject === "opportunity")?.externalId

The Opportunity ID can be found from the externalConnections field. The above snippet represents how to find the Opportunity ID from the list. Note that it may be undefined if the deal is not connected to an opportunity.

Querying Data

Once you have the Opportunity ID, you can use it to query Salesforce by calling revops.salesforce.query. This takes a singular parameter of a SOQL query and returns a Promise of an array of results. You can also use any other data on the deal that may be an identifier, such as a billing contact email.

The Function Examples offer full examples of this in action.

Querying Data
const query = `SELECT Segment__c FROM Opportunity WHERE Id='${oppId}'`

const queryResult = await revops.salesforce.query(query)
console.log(queryResult[0]?.Segment__c)

Data can be read from connected objects as well. Objects that have a one-to-one relation, such as the Account, can be queried through the lookup field on the opportunity. When there is a one-to-many relation, other objects can be queried instead.

Querying Lookup Fields
const query = `SELECT Account.Segment__c FROM Opportunity WHERE Id='${oppId}'`

const queryResult = await revops.salesforce.query(query)
console.log(queryResult[0]?.Account?.Segment__c)
Querying Related Objects
const query = `SELECT ErrorMessage__c, ErrorStatus__c FROM Acme_Custom_Errors__c WHERE OpportunityId__c='${oppId}'`

const queryResults = await revops.salesforce.query(query)
console.log(queryResults)

Generic Types

The return value of query varies based on the provided query. The return type can be more specific than Record<string, any>[] by providing a type to the query call.

Generic Types
const queryResult = await revops.salesforce.query<{ Segment__c: "small" | "medium" | "large" | null }>(query)
CRM Sync Timing

RevOps CRM sync occurs as a background job and generally runs parallel to or after functions are run. You should not rely on querying the CRM for any of the data that is being synced from the current deal.

Queried data should reference fields not set by RevOps or for a prior deal (i.e. checking the previous new order form data when processing a renewal deal). Data that is being sent via CRM sync should be accessed directly from the data provided to the function and not through the CRM.

Common Mistakes and Tips

Missing async

When using the await keyword, the function must be async. Your function will typically start as:

export const validateDealBeforeSubmission = async (data: PreSubmitTestData): Promise<PreSubmitResponse> =>

This error will appear in the logs as [ERROR] Failed to import: SyntaxError: Unexpected reserved word. This error message may also occur when any other Reserved JavaScript Word is used incorrectly.

See the CRM Data Function Examples for additional examples.

Using Double Quotes in SOQL

SOQL does not support " (double quotes). Use ' (single quotes) when quoting a value.

Correct ✅Incorrect ❌
WHERE Id='${oppId}'WHERE Id="${oppId}"

Debugging SOQL

Salesforce offers a SOQL UI that can be helpful for debugging. Use the gear in the top right menu to access the Developer Console. Then use the Query Editor tab to test your queries.

Performance and Timeouts

Executing a SOQL query requires connecting to our proxy to access Salesforce credentials and then sending the query to Salesforce. Since this requires going between networks, each query takes some time to execute. There are a few best practices to have your function execute as quickly as possible.

  • Don't call the same query multiple times. If you need to use the same query in multiple places, run the query once and store as a variable. Pass the variable along as needed.
  • Run parallel code when queries do not depend on each other. Promise.all() and Promise.allSettled() offer the ability to run multiple async calls simultaneously.
  • Avoid querying more fields than you need. SELECT FIELDS(ALL) will take longer to run than SELECT Name, MyCustomField__c.

Each function type has a specific time limit for a function execution. Using these best practices will help you stay within those limits.

Beta Feature

The RevOps SDK is a beta feature. The information in this document is a work in progress and is subject to change as new features are released.

Please provide any questions or feedback to support@revops.io or via your Slack Connect channel if applicable.