Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Create Named Query APIs in VS Code

In VS Code, you can create a Named Query API and deploy it to your org. In Setup, you can view, edit, and delete the Named Query API.

User Permissions Needed
To work with Named Query API: Allows users to create, read, update and delete Named Query API records
  1. Create a Salesforce DX project.
  2. In your force-app/main/default directory, create a folder for Named Query APIs.
  3. In the new folder, create a file for your Named Query API.
    Name the file by using the format <NamedQueryAPIName>.apiNamedQuery-meta.xml. For example, myNamedQuery.apiNamedQuery.xml.

    The name must begin with a letter. It can contain only alpha-numeric characters.

    Important

  4. Add the metadata describing the Named Query API that you're creating.
    See the example later on this page.
  5. Deploy the metadata file to your target org.
    These options are supported deployment methods:
    • Use the Salesforce CLI deploy command. For example: sf project deploy start -o <TargetOrg>, where <TargetOrg> is the username or alias of the target org.
    • Inside your metadata file, right-click to open a list of Salesforce DX commands. Select Salesforce DX: Deploy This Source to Org.

Example

Here's an example of a valid apiNamedQuery-meta.xml file. You must include these elements:

  • Label (<masterLabel>)
  • Description (<description>)
  • SOQL statement for the Named Query API (<body2>)
1<?xml version="1.0" encoding="UTF-8"?>
2<ApiNamedQuery xmlns="http://soap.sforce.com/2006/04/metadata">
3 <apiNamedQueryParameters>
4   <description>Name of this account</description>
5   <parameterLabel>ApiNamedQueryParameterLabel</parameterLabel>
6   <parameterName>name</parameterName>
7 </apiNamedQueryParameters>
8 <apiVersion>64.0</apiVersion>
9 <body2>SELECT Id, Name, NumberOfEmployees FROM Account WHERE Name = :name</body2>
10 <description> Get id, name, and annual revenue from Account entity filtered by name</description>
11 <masterLabel>Get Account Details</masterLabel>
12</ApiNamedQuery>
The ApiNamedQuery and ApiNamedQueryParameter objects are supported for use with Metadata API (Retrieve and Deploy).