Skip to content

Commit 540f475

Browse files
feat: handled field integrity exception
1 parent 7946432 commit 540f475

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

messages/migrate.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
"omniStudioSettingsMetadataEnabled": "The Omnistudio Metadata setting is enabled with standard data model.",
261261
"timeoutEnablingOmniStudioSettingsMetadata": "Timeout while checking the metadata enablement status. Tried for %s seconds.",
262262
"errorEnablingOmniStudioSettingsMetadata": "Error while enabling the Omnistudio Metadata setting: %s. Enable it manually.",
263+
"fieldIntegrityException": "Field integrity Exception while deleting metadata records from %s: %s",
263264
"manuallyEnableOmniStudioSettingsMetadata": "Manually enable the Omnistudio Metadata setting in your org’s Omnistudio Settings page.",
264265
"omniStudioMetadataEnableConsentNotGiven": "You’ve not consented to proceed with enabling the Omnistudio Metadata setting. We’ll not be able to proceed with the migration.",
265266
"enablingOmniStudioSettingsMetadataStatus": "Enabling Omnistudio Metadata setting…",

src/utils/config/OmniStudioMetadataCleanupService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,11 @@ export class OmniStudioMetadataCleanupService {
9797
return 0;
9898
}
9999

100-
const deleteSuccess = await NetUtils.delete(this.connection, recordIds);
101-
return deleteSuccess ? recordIds.length : -1;
100+
const deleteResult = await NetUtils.deleteWithFieldIntegrityException(this.connection, recordIds);
101+
if (!deleteResult.success && deleteResult.statusCode === 'FIELD_INTEGRITY_EXCEPTION') {
102+
Logger.error(this.messages.getMessage('fieldIntegrityException', [tableName, deleteResult.message || '']));
103+
return -1;
104+
}
105+
return deleteResult.success ? recordIds.length : -1;
102106
}
103107
}

src/utils/net/index.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,52 @@ class NetUtils {
124124
return true;
125125
}
126126

127+
public static async deleteWithFieldIntegrityException(
128+
connection: Connection,
129+
data: string[]
130+
): Promise<{ success: boolean; statusCode?: string; message?: string }> {
131+
// Metadata API only accepts 200 records per request
132+
const chunks = chunk(data, NetUtils.CHUNK_SIZE);
133+
let hasFieldIntegrityException = false;
134+
let hasErrors = false;
135+
let errorCode: string | undefined;
136+
let message: string | undefined;
137+
138+
for (let curr of chunks) {
139+
const deleteUrl = 'composite/sobjects?allOrNone=true&ids=' + curr.join(',');
140+
141+
const response = await this.request<DeleteResponse[]>(connection, deleteUrl, [], RequestMethod.DELETE);
142+
// Check each response for errors
143+
response.forEach((result) => {
144+
if (!result.success && result.errors && result.errors.length > 0) {
145+
result.errors.forEach((error) => {
146+
hasErrors = true;
147+
// Check if this error is a FIELD_INTEGRITY_EXCEPTION
148+
if (error.statusCode === 'FIELD_INTEGRITY_EXCEPTION') {
149+
hasFieldIntegrityException = true;
150+
message = error.message;
151+
return;
152+
} else {
153+
errorCode = error.statusCode;
154+
}
155+
});
156+
}
157+
});
158+
}
159+
160+
// If there are failed records, return failure with status code
161+
if (hasErrors) {
162+
return {
163+
success: false,
164+
// Override with FIELD_INTEGRITY_EXCEPTION if found, otherwise use first encountered
165+
statusCode: hasFieldIntegrityException ? 'FIELD_INTEGRITY_EXCEPTION' : errorCode,
166+
message: message,
167+
};
168+
}
169+
170+
return { success: true };
171+
}
172+
127173
public static async request<TResultType>(
128174
connection: Connection,
129175
url: string,
@@ -156,4 +202,14 @@ interface TreeResult {
156202
results: UploadRecordResult[];
157203
}
158204

205+
interface DeleteResponse {
206+
id: string;
207+
success: boolean;
208+
errors: Array<{
209+
statusCode: string;
210+
message: string;
211+
fields: string[];
212+
}>;
213+
}
214+
159215
export { NetUtils, RequestMethod, TreeResult };

0 commit comments

Comments
 (0)