Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Last active March 19, 2025 20:33
Show Gist options
  • Save cbaragao/3cb1a5a95d7e4cfbec70b97e20292bdc to your computer and use it in GitHub Desktop.
Save cbaragao/3cb1a5a95d7e4cfbec70b97e20292bdc to your computer and use it in GitHub Desktop.
let
// Define the base API endpoint for the Dataverse environment
baseUrl = "https://your-org.crm.dynamics.com/api/data/v9.2/",
// Define the schema name of the environment variable you want to fetch
schemaName = "schema_name", // Replace with the actual schema name
// Fetch the environment variable definition that matches the given schema name
definitionQuery = Web.Contents(
baseUrl,
[
RelativePath = "environmentvariabledefinitions",
Query = [
#"$select" = "environmentvariabledefinitionid",
#"$filter" = "schemaname eq '" & schemaName & "'"
],
Headers = [#"Accept" = "application/json"]
]
),
definitionJson = Json.Document(definitionQuery),
definitionRecord = try definitionJson[value]{0} otherwise null,
// If a definition was found, fetch the associated environment variable value
valuesQuery = if definitionRecord <> null then
Web.Contents(
baseUrl,
[
RelativePath = "environmentvariablevalues",
Query = [
#"$select" = "value",
#"$filter" = "_environmentvariabledefinitionid_value eq " & "'" & definitionRecord[environmentvariabledefinitionid] & "'"
],
Headers = [#"Accept" = "application/json"]
]
)
else
null,
valuesJson = if valuesQuery <> null then Json.Document(valuesQuery) else null,
valueRecord = try valuesJson[value]{0} otherwise null,
// Extract the value if available
envVariableValue = if valueRecord <> null then valueRecord[value] else null,
// Return the result as a record (schema name and value)
result = [
SchemaName = schemaName,
Value = envVariableValue
]
in
result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment