Skip to content

Commit

Permalink
fix: check reserved bits are equal to 0 for more accurate SF ID matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeneos committed May 27, 2024
1 parent 530c863 commit 1543d9c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/util/src/salesforce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,24 @@ export function extractNamespaceAndName(typeName : string) : { name: string; nam
* @param id ID like string to check
*/
export function isSalesforceId(id : string) : boolean {
return /^([a-z0-9]{15}|[a-z0-9]{18})$/i.test(id);
return decodeSalesforceId(id) !== undefined;
}

/**
* Decodes a salesforce ID into its parts. Returns undefined if the ID is not a valid Salesforce ID.
* @param id ID to decode into parts
*/
export function decodeSalesforceId(id : string) {
const match = /^([a-z0-9]{3})([a-z0-9]{2})([a-z0-9]{2})([a-z0-9]{8})([a-z0-9]{3})?$/i.exec(id);
if (!match || match[3] !== '00') {
return;
}
return {
objectPrefix: match[1],
instanceId: match[2],
recordId: match[4],
caseChecksum: match[5]
};
}

/**
Expand Down

0 comments on commit 1543d9c

Please sign in to comment.