updateEntity method

Future<bool> updateEntity(
  1. String entity,
  2. Map<String, dynamic> payload, {
  3. required String keyField,
  4. required String keyValue,
})

Sends a PATCH request to update an existing record in the specified Dynamics 365 entity.

entity - the name of the target entity. payload - the fields and values to update. keyField - the primary key field used to identify the record. keyValue - the value of the key field to locate the record.

Returns true if the request succeeds (204/200), otherwise false.

Implementation

Future<bool> updateEntity(String entity,
    Map<String, dynamic> payload, {
      required String keyField,
      required String keyValue,
    }) async {
  final token = await getAccessToken();
  if (token == null) return false;

  final url = Uri.parse("$resource/data/$entity($keyField='$keyValue')");

  final response = await http.patch(
    url,
    headers: {
      'Authorization': 'Bearer $token',
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    },
    body: json.encode(payload),
  );

  if (response.statusCode == 204 || response.statusCode == 200) {
    return true;
  } else {
    print("❌ Failed to update $entity: ${response.statusCode} - ${response
        .body}");
    return false;
  }
}