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.
Before You Begin
| User Permissions Needed | |
|---|---|
| To work with Named Query API: | Allows users to create, read, update and delete Named Query API records |
Steps
-
Create a Salesforce DX project.
-
In your force-app/main/default directory, create a folder for Named Query APIs.
-
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.
important: The name must begin with a letter. It can contain only alpha-numeric characters.
-
Add the metadata describing the Named Query API that you're creating.
See the example later on this page.
-
Deploy the metadata file to your target org.
These options are supported deployment methods:
- Use the Salesforce CLI
deploycommand. 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.
- Use the Salesforce CLI
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>)
<?xml version="1.0" encoding="UTF-8"?>
<ApiNamedQuery xmlns="http://soap.sforce.com/2006/04/metadata">
<apiNamedQueryParameters>
<description>Name of this account</description>
<parameterLabel>ApiNamedQueryParameterLabel</parameterLabel>
<parameterName>name</parameterName>
</apiNamedQueryParameters>
<apiVersion>64.0</apiVersion>
<body2>SELECT Id, Name, NumberOfEmployees FROM Account WHERE Name = :name</body2>
<description> Get id, name, and annual revenue from Account entity filtered by name</description>
<masterLabel>Get Account Details</masterLabel>
</ApiNamedQuery>
Next Steps
The ApiNamedQuery and ApiNamedQueryParameter objects are supported for use with Metadata API (Retrieve and Deploy).