/**
* @param variableSchemaName string
* @returns Returns undefined if env var doesn't exist.
* Returns the default value if the currently value is null or if no value exists yet.
* The default value can be null.
*/
async function getEnvironmentVariableValueUsingFunction(variableSchemaName) {
try {
const request = {
'DefinitionSchemaName': variableSchemaName,
getMetadata: () => {
return {
boundParameter: null, // undefined = CRUD, null = no table bound, 'entityLogicalName' = if bound to a table
operationName: 'RetrieveEnvironmentVariableValue', // action, function or CRUD opetaion name ('Create', 'Retrieve', 'Update', or 'Delete')
operationType: 1, // 0 = Action, 1 = Function, 2 = CRUD
parameterTypes: {
'DefinitionSchemaName': {
typeName: 'Edm.String',
structuralProperty: 1 // 0 = Unknown, 1 = PrimitiveType, 2 =ComplexType, 3 = EnumerationType, 4 = Collection, 5 = EntityType
}
}
}
}
};
const response = await Xrm.WebApi.online.execute(request);
const data = await response.json();
return data.Value;
} catch (error) {
console.log(error);
const baseUrl = Xrm.Utility.getGlobalContext().getClientUrl();
const methodUrl = `/api/data/v9.2/RetrieveEnvironmentVariableValue(DefinitionSchemaName='${variableSchemaName}')`;
const response = await fetch(baseUrl + methodUrl);
const data = await response.json();
return data.Value;
}
}
/**
* @param variableSchemaName string
* @returns Returns undefined if env var doesn't exist.
* Returns the default value if the currently value is null or if no value exists yet.
* The default value can be null.
*/
async function getEnvironmentVariableValueUsingQuery(variableSchemaName) {
const result = await parent.Xrm.WebApi.retrieveMultipleRecords(
`environmentvariabledefinition`,
`?$select=displayname,schemaname,defaultvalue` +
`&$expand=environmentvariabledefinition_environmentvariablevalue($select=value,createdon;$orderby=createdon desc)` +
`&$filter=(schemaname eq '${variableSchemaName}')`
);
const value = result.entities[0]?.environmentvariabledefinition_environmentvariablevalue[0]?.value
if (value === undefined || value === null) { // keep 0, false, '' as valid values
return result.entities[0]?.defaultvalue
} else {
return value;
}
}