getEntityData method
Retrieves data from a specified Dynamics 365 entity using an optional filter.
entity - the name of the entity to fetch data from.
filterByField & filterValue - optional parameters to filter the result set.
Returns a list of records if the request succeeds, otherwise an empty list.
Implementation
Future<List<dynamic>> getEntityData({
required String entity,
String? filterByField,
String? filterValue,
}) async {
final token = await getAccessToken();
if (token == null) return [];
final String url = (filterByField != null &&
filterByField.isNotEmpty &&
filterValue != null &&
filterValue.isNotEmpty)
? "$resource/data/$entity?\$filter=$filterByField eq '$filterValue'"
: "$resource/data/$entity";
final response = await http.get(
Uri.parse(url),
headers: {
"Authorization": "Bearer $token",
"Content-Type": "application/json",
"Accept": "application/json",
},
);
if (response.statusCode == 200) {
return jsonDecode(response.body)["value"] ?? [];
} else {
print(
"❌ Failed to get $entity: ${response.statusCode} - ${response.body}");
return [];
}
}