diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d362f99098358..c0687aa497d39 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1040,12 +1040,14 @@ Onyx.connect({ let allPolicies: OnyxCollection; let hasPolicies: boolean; +let policiesArray: Policy[] = []; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, waitForCollectionCallback: true, callback: (value) => { allPolicies = value; hasPolicies = !isEmptyObject(value); + policiesArray = Object.values(value ?? {}).filter((policy): policy is Policy => !!policy); }, }); @@ -4849,12 +4851,8 @@ function canEditFieldOfMoneyRequest( // Check if there are multiple outstanding reports across policies let outstandingReportsCount = 0; - for (const currentPolicy of Object.values(allPolicies ?? {})) { - const reports = getOutstandingReportsForUser( - currentPolicy?.id, - moneyRequestReport?.ownerAccountID, - outstandingReportsByPolicyID?.[currentPolicy?.id ?? CONST.DEFAULT_NUMBER_ID] ?? {}, - ); + for (const currentPolicy of policiesArray) { + const reports = getOutstandingReportsForUser(currentPolicy.id, moneyRequestReport?.ownerAccountID, outstandingReportsByPolicyID?.[currentPolicy?.id] ?? {}); outstandingReportsCount += reports.length; // Short-circuit once we find more than 1 @@ -5932,8 +5930,8 @@ function getReportSubtitlePrefix(report: OnyxEntry): string { } let policyCount = 0; - for (const policy of Object.values(allPolicies ?? {})) { - if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { + for (const policy of policiesArray) { + if (!shouldShowPolicy(policy, false, currentUserEmail)) { continue; } @@ -11051,8 +11049,8 @@ function createDraftTransactionAndNavigateToParticipantSelector( let firstPolicy: Policy | undefined; let filteredPoliciesCount = 0; - for (const policy of Object.values(allPolicies ?? {})) { - if (!policy || !shouldShowPolicy(policy, false, currentUserEmail)) { + for (const policy of policiesArray) { + if (!shouldShowPolicy(policy, false, currentUserEmail)) { continue; }