To interact with the Salesforce API and execute SOQL queries via a REST endpoint, you can use the example provided but adapted for use in Katalon Studio. The approach involves constructing a REST API request that includes the SOQL query as a URL parameter, ensuring the query is properly URL-encoded. Here’s how you can achieve this in Katalon Studio:
Step 1: Construct the SOQL Query
First, you need to have your SOQL query ready. For example:
SELECT name FROM Account WHERE name = 'sree'
Step 2: URL Encode the SOQL Query
Before appending the SOQL query to the URL, it must be URL-encoded to ensure it’s
SELECT name FROM Account WHERE name = 'sree'
SELECT+name+FROM+Account+WHERE+name+%3D+%27sree%27
Step 3: Construct the API Request in Katalon Studio
- Create a New Test Case: In Katalon Studio, start by creating a new test case for your project.
- Add a Request Object: Navigate to the Object Repository, right-click, and select New > REST Request. Give it a name, e.g., QueryAccountByName.
- Configure the Request:
- Method: GET
- URL: Construct the URL using your Salesforce instance URL, the REST API endpoint for querying, and the encoded SOQL query. It should look something like this:
https://yourInstance.salesforce.com/services/data/v20.0/query/?q=SELECT+name+FROM+Account+WHERE+name+%3D+%27sree%27
Replace yourInstance.salesforce.com with your actual Salesforce instance URL.
Authorization: Ensure you add authorization to your request. Salesforce requires an OAuth 2.0 access token for authentication. In the Authorization tab of the request object, select Bearer Token and provide the access token you’ve obtained through your preferred authentication method.
Send the Request and Process the Response:
Back in your test case, use the sendRequest method to execute the API call and store the response.
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
RequestObject request = findTestObject('Object Repository/QueryAccountByName')
def response = WS.sendRequest(request)
// Optionally, process the response, e.g., verify status code, extract data
WS.verifyResponseStatusCode(response, 200)
println('Response: ' + response.getResponseText())
Additional Notes
- Remember to obtain and refresh your access token as needed.
- Always URL-encode query parameters to avoid errors.
- Salesforce’s REST API version may update; use the latest version for optimal features and compatibility.
- Consider using Katalon’s built-in variables and profiles to manage sensitive data like access tokens and instance URLs securely. This approach allows you to seamlessly integrate Salesforce API calls into your Katalon Studio test cases, enabling automated testing of Salesforce applications by leveraging SOQL queries.
Step 1: Identify the Record to Delete
Ensure you have the Salesforce Object ID of the record you wish to delete. For instance, if you’re deleting an Account record, you need the Account ID, which is typically a 15 or 18 character string.
Step 2: Create a New Test Case and Request Object in Katalon
- Create a New Test Case: In your Katalon Studio project, create a new test case for the delete operation.
- Add a New REST Request Object: Go to the Object Repository, right-click, and select New > REST Request. Name it appropriately, e.g., DeleteAccountRecord.
- Configure the REST Request Object:
-
Method: DELETE
-
URL: Construct the URL using your Salesforce instance URL followed by the specific endpoint for the record you intend to delete. For an Account record, the URL format will be:
-
bash
-
Copy code https://yourInstance.salesforce.com/services/data/v20.0/sobjects/Account/{AccountId}
-
Replace yourInstance.salesforce.com with your Salesforce instance URL and {AccountId} with the actual ID of the account you’re deleting.
- Set Authorization: In the request object, go to the Authorization tab, select Bearer Token, and enter your access token.
Step 3: Implement the Delete Operation in Your Test Case
Add the following script to your test case, adjusting the request object path and the specific ID of the record to delete:
import com.kms.katalon.core.testobject.RequestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
// Replace ‘Object Repository/DeleteAccountRecord’ with the correct path to your request object
RequestObject deleteRequest = findTestObject(‘Object Repository/DeleteAccountRecord’, [(‘AccountId’) : ‘yourAccountIdHere’])
// Sending the DELETE request
def response = WS.sendRequest(deleteRequest)
// Optional: Verify the response status code to ensure successful deletion
WS.verifyResponseStatusCode(response, 204) // HTTP Status Code 204 means “No Content” indicating successful deletion
println(‘Deletion successful for record ID: yourAccountIdHere’)
We will discus how to execute a real testcase in the next lesson