@@ -47,6 +47,11 @@ type EntityDefinitionsDto struct {
47
47
MetadataID string `json:"MetadataId"`
48
48
}
49
49
50
+ type RelationApiResponse struct {
51
+ OdataContext string `json:"@odata.context"`
52
+ Value []RelationApiBody `json:"value"`
53
+ }
54
+
50
55
type RelationApiBody struct {
51
56
OdataID string `json:"@odata.id"`
52
57
}
@@ -286,6 +291,36 @@ func (client *DataRecordClient) ApplyDataRecord(ctx context.Context, recordId st
286
291
Path : fmt .Sprintf ("/api/data/%s/%s(%s)/%s/$ref" , constants .DATAVERSE_API_VERSION , entityDefinition .LogicalCollectionName , result .Id , key ),
287
292
}
288
293
294
+ existingRelationsResponse := RelationApiResponse {}
295
+
296
+ apiResponse , _ := client .Api .Execute (ctx , "GET" , apiUrl .String (), nil , nil , []int {http .StatusOK , http .StatusNoContent }, nil )
297
+
298
+ json .Unmarshal (apiResponse .BodyAsBytes , & existingRelationsResponse )
299
+
300
+ var toBeDeleted []RelationApiBody = make ([]RelationApiBody , 0 )
301
+
302
+ for _ , existingRelation := range existingRelationsResponse .Value {
303
+ delete := true
304
+ for _ , nestedItem := range nestedMapList {
305
+ nestedMap := nestedItem .(map [string ]interface {})
306
+ relationEntityDefinition := getEntityDefinition (ctx , client , environmentUrl , nestedMap ["table_logical_name" ].(string ))
307
+ if existingRelation .OdataID == fmt .Sprintf ("%s/api/data/%s/%s(%s)" , environmentUrl , constants .DATAVERSE_API_VERSION , relationEntityDefinition .LogicalCollectionName , nestedMap ["data_record_id" ]) {
308
+ delete = false
309
+ break
310
+ }
311
+ }
312
+ if delete {
313
+ toBeDeleted = append (toBeDeleted , existingRelation )
314
+ }
315
+ }
316
+
317
+ for _ , relation := range toBeDeleted {
318
+ _ , err = client .Api .Execute (ctx , "DELETE" , relation .OdataID , nil , nil , []int {http .StatusOK , http .StatusNoContent }, nil )
319
+ if err != nil {
320
+ return nil , err
321
+ }
322
+ }
323
+
289
324
for _ , nestedItem := range nestedMapList {
290
325
nestedMap := nestedItem .(map [string ]interface {})
291
326
0 commit comments