Article Overview:
This article will cover how you can achieve the following:
Virtuoso allows referencing environment variables in journeys, but there is no native solution to update an environment variable directly from a journey due to platform limitations.
Problem Statement:
If updating environment variables from within a journey is crucial, this can be achieved using a custom extension that makes an API call to update the variable.
Solution:
Extension to Update Environment Variables
The following JavaScript function can be used to update an environment variable by making a PUT request to Virtuoso's API:
Code:
// Function to make the API call
async function updateEnvironmentVariable(envId, token, variableName, updatedValue) {
const url = `https://api.virtuoso.qa/api/environments/${envId}/variables/${variableName}`;
const headers = {
"Authorization": `Bearer ${token}`, // Replace 'token' with your actual token
"Content-Type": "application/json"
};
const body = JSON.stringify({ value: updatedValue });
console.log(body);
try {
const response = await fetch(url, {
method: "PUT",
headers: headers,
body: body
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const responseData = await response.json();
console.log("Response:", responseData);
} catch (error) {
console.error("Error:", error);
}
}
// Function from where the API call is made
function performApiCall(envId, token, variableName, updatedValue) {
console.log(`Updating variable: ${variableName} with value: ${updatedValue}`);
updateEnvironmentVariable(envId, token, variableName, updatedValue);
}
// Call the function
performApiCall(envId, token, variableName, updatedValue);
Reference Screenshot:
Parameters:
The extension requires the following parameters to work, In our case, you'll need just the variable name,
e.g., TestVar from QA Env

- Environment ID: (e.g.,
39707)
- Token: (Your Virtuoso API Token)

- Variable Name: (e.g.,
TestVarfrom your Env)

- Updated Value: (e.g.,
$QuoteID)

This is what it should look like when all the parameters are in place.
Important Considerations:
- Using environment variables is the best approach when a global variable is needed across different goals.
- However, if multiple parallel executions attempt to update the same variable, consistency cannot be guaranteed.
Example:
- Scenario: A user needs to dynamically update an environment variable during a journey execution.
- Solution: The provided script is used to send an API request to update the variable.
Comments
0 comments
Please sign in to leave a comment.