App icon indicating copy to clipboard operation
App copied to clipboard

[Due for payment 2026-11-30] [$175] The expense confirmation screen shows an existing submitted report as the destination, but saving the expense silently creates a new report instead

Open m-natarajan opened this issue 2 months ago • 45 comments

If you haven’t already, check out our contributing guidelines for onboarding. To join our Slack channel, fill out this form.


Version Number: 9.4.93-3 Reproducible in staging?: Needs Reproduction Reproducible in production?: Needs Reproduction If this was caught during regression testing, add the test name, ID and link from BrowserStack: Email or phone of affected tester (no customers): Logs: https://stackoverflow.com/c/expensify/questions/4856 Expensify/Expensify Issue URL: Issue reported by: @Leeckhart Slack conversation (hyperlinked to channel name): #Expensify Bugs

Action Performed:

  1. Submit report, titled 9/21, so it is awaiting approval.
  2. From the workspace chat, tap the camera/photo option to scan and create a new cash expense.
  3. On the expense confirmation screen, confirm that the Report field displays the existing 9/21 report.
  4. Save the expense.
  5. Review the report where the expense was added.

Expected Result:

The expense should be added to the report displayed in the Report field. If a submitted report cannot accept new expenses, it should not appear as the selected destination, and the user should be told that a new report will be created.

Actual Result:

Although the confirmation screen displayed the 9/21 report, the app sent newly created report as the target. Report titled 9/21 was never passed during the save request. The customer then had to move the expense manually from the newly created report back to the intended report.

Note:

The issue appears to be caused by inconsistent eligibility logic: The Report field includes submitted reports that are awaiting first-level approval. The save logic only searches for an open/draft report. Therefore, the UI can display one report while the save action silently selects or creates another.

Workaround:

Unknown

Platforms:

Select the officially supported platforms where the issue was reproduced:

  • [ ] Android: App
  • [ ] Android: mWeb Chrome
  • [x] iOS: App
  • [ ] iOS: mWeb Safari
  • [ ] iOS: mWeb Chrome
  • [ ] Windows: Chrome
  • [ ] MacOS: Chrome / Safari

Screenshots/Videos

View all open jobs on GitHub

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @ZhenjaHorbach

m-natarajan avatar Sep 30 '26 19:09 m-natarajan

Proposal

What is the root cause of that problem?

The Report field and the save logic pick the default report using different rules.

When you submit 9/21, the workspace chat's iouReportID is cleared (ReportWorkflow.ts:1490-1502). Both sides then fall back to the submitter's outstanding reports:

  • Report field (display): ReportField.tsx:77-81 takes getOutstandingReportsForUser(..., false).at(0). That list still includes submitted reports that are awaiting first-level approval. isReportOutstanding accepts them through canAddTransaction, which returns true through isAwaitingFirstLevelApproval. So the field shows 9/21.
  • Save: MoneyRequestBuilder.ts:1429-1435 calls getNewestOutstandingReportForUser. It adds .filter(isOpenExpenseReport), so 9/21 is rejected and a new optimistic report is built. This is intentional: see the doc comment at ReportUtils.ts:12440-12443 and the test "creates a new report when the submitter only has a report that is awaiting approval".

The Report field's pick is display-only. It is never written to transaction.reportID, so 9/21's ID is never sent in RequestMoney. If you pick 9/21 by hand in the picker, it works, because useReportSelectionActions.ts:195-202 writes the choice to the transaction.

What changes do you think we should make in order to solve the problem?

Make the Report field use the same default the save path uses:

  1. In ReportField.tsx, replace the availableOutstandingReports.at(0) fallback with getNewestOutstandingReportForUser(...). When no open report exists, the field then shows "New report", which matches what gets created.
  2. Better: move "default destination report" into one shared helper. ReportField, MoneyRequestBuilder and PerDiem.ts:477-482 should all call it.
  3. Add a ReportField test: one submitted report awaiting first approval, no open report. The field should show "New report".

What alternative solutions did you explore? (Optional)

Remove the open-only filter so the expense goes into the submitted report. This reverses the intent of Reuse the submitter's outstanding report when the workspace chat has no iouReportID and its test, so it needs a product decision.

Investigation details

Next Steps for Contributor+ team: To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/102688) to create a draft PR. To refine: @MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.


view run

MelvinBot avatar Sep 30 '26 19:09 MelvinBot

Triggered auto assignment to Contributor-plus team member for initial proposal review - @ZhenjaHorbach (External)

melvin-bot[bot] avatar Sep 30 '26 19:09 melvin-bot[bot]

Job added to Upwork: https://www.upwork.com/jobs/~022105384062623167301

melvin-bot[bot] avatar Sep 30 '26 19:09 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a user submits an expense report (e.g. titled "9/21") so that it is awaiting approval, and then creates a new scan/cash expense from the workspace chat, the confirmation screen displays the existing "9/21" report in the Report field. However, upon saving the expense, the app creates a brand new report instead of adding the transaction to "9/21".


What is the root cause of that problem?

When an expense report is submitted, the workspace chat report's iouReportID is cleared to undefined/null (src/libs/actions/IOU/ReportWorkflow.ts). As a result, both the confirmation screen's ReportField UI and the save logic in MoneyRequestBuilder fall back to finding the submitter's outstanding reports:

  1. In ReportField.tsx (lines 77-81):
const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {}, false).sort(
    (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
);

const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;

getOutstandingReportsForUser(..., false) checks isReportOutstanding(report, policyID, rules, reportNameValuePairs, false). Inside isReportOutstanding, even when allowSubmitted = false, it returns true if canAddTransaction(report) is true. canAddTransaction(report) returns true for reports awaiting first-level approval (isAwaitingFirstLevelApproval(report)). Therefore, availableOutstandingReports includes the submitted "9/21" report, and availableOutstandingReports.at(0) selects it. ReportField displays "9/21".

  1. In MoneyRequestBuilder.ts (lines 1429-1435):
if (!iouReport && isPolicyExpenseChat) {
    iouReport = getNewestOutstandingReportForUser(chatReport.policyID, payeeAccountID, rules, getAllReportNameValuePairs(), allReports);
}

MoneyRequestBuilder calls getNewestOutstandingReportForUser(...) (src/libs/ReportUtils.ts:12449-12459). getNewestOutstandingReportForUser explicitly applies .filter(isOpenExpenseReport):

const openReports = getOutstandingReportsForUser(policyID, reportOwnerAccountID, rules, reportNameValuePairs, reports, false).filter(isOpenExpenseReport);

Because the "9/21" report is submitted (not open), it is filtered out. If the user has no other open reports, getNewestOutstandingReportForUser returns null. Then line 1453 executes:

if (!iouReport || shouldCreateNewMoneyRequestReport) {
    iouReport = isPolicyExpenseChat ? buildOptimisticExpenseReport(...) : ...

Because transaction.reportID was not manually picked in IOURequestStepReport, moneyRequestReportID is undefined. The save flow creates a brand new report, silently diverging from what ReportField rendered on screen.


What changes do you think we should make in order to solve the problem?

The fallback report displayed in ReportField.tsx should use the exact same open report resolution as MoneyRequestBuilder:

  1. In src/components/MoneyRequestConfirmationList/sections/ReportField.tsx:
  • Import getNewestOutstandingReportForUser from @libs/ReportUtils.
  • Replace the fallback availableOutstandingReports.at(0)?.reportID with getNewestOutstandingReportForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {})?.reportID:
--- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
+++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
@@ -9,7 +9,7 @@ import Navigation from '@libs/Navigation/Navigation';
 import Parser from '@libs/Parser';
 import {getReportName} from '@libs/ReportNameUtils';
-import {generateReportID, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
+import {generateReportID, getNewestOutstandingReportForUser, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
 
 import CONST from '@src/CONST';
@@ -78,7 +78,9 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
         (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
     );
 
-    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
+    const newestOutstandingReport = getNewestOutstandingReportForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {});
+    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? newestOutstandingReport?.reportID) : reportID;
 
     const [selectedReportID, selectedReport] = (() => {
         const reportIDToUse = shouldUseTransactionReport ? transactionReportID : outstandingReportID;
@@ -95,6 +97,8 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
             reportToUse = mainReport;
         } else if (reportIDToUse === iouReportIDFromMain) {
             reportToUse = iouReport;
+        } else if (reportIDToUse === newestOutstandingReport?.reportID) {
+            reportToUse = newestOutstandingReport;
         } else {
             reportToUse = availableOutstandingReports.find((r) => r?.reportID === reportIDToUse);
         }

Why this is the correct solution:

  • When the workspace chat has no active iouReportID and the submitter only has submitted reports (awaiting approval), newestOutstandingReport correctly returns null. outstandingReportID is undefined, so ReportField displays "New report" (translate('iou.newReport')). The confirmation screen accurately informs the user that a new report will be created.
  • If the user explicitly wants to attach the expense to the submitted report awaiting approval, they can tap the Report field to open the report picker (where availableOutstandingReports still presents the submitted report since it can accept transactions manually). Selecting it sets transaction.reportID, making shouldUseTransactionReport true, which both displays and saves to the chosen submitted report.
  • Zero regression for flows where the chat already has an open iouReportID or where an open expense report exists.

What alternative solutions did you explore? (Optional)

Change getNewestOutstandingReportForUser to include submitted reports awaiting approval. However, as documented in ReportUtils.ts:12440-12444, automatically dumping new expenses into a report that was already submitted for managerial review breaks the submission lifecycle and violates the test suite ("creates a new report when the submitter only has a report that is awaiting approval").

bonnhatnguyen avatar Sep 30 '26 20:09 bonnhatnguyen

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~0170d6b02e9440e604

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a user submits an expense report (e.g. titled "9/21") such that it is awaiting approval, and then initiates a new cash or scan expense from the workspace chat, the confirmation screen displays the existing "9/21" report in the Report field. However, upon saving the expense, the application creates a brand new report instead of attaching the transaction to the "9/21" report displayed on screen.

What is the root cause of that problem?

There is an architectural mismatch in fallback report resolution between the confirmation UI (ReportField.tsx) and the transaction persistence builder (MoneyRequestBuilder.ts):

  1. In ReportField.tsx (lines 77-84):

    const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {}, false).sort(
        (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
    );
    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
    

    When a report has been submitted for approval, the workspace chat's iouReportID is cleared to undefined. ReportField falls back to availableOutstandingReports.at(0)?.reportID. getOutstandingReportsForUser(..., false) checks isReportOutstanding(report, ...) which in turn checks canAddTransaction(report). Because canAddTransaction returns true for reports awaiting first-level approval (isAwaitingFirstLevelApproval(report)), the submitted "9/21" report is included in availableOutstandingReports. Consequently, ReportField displays the submitted "9/21" report as the default destination.

  2. In src/libs/actions/IOU/MoneyRequestBuilder.ts (lines 1429-1435 & 1453):

    if (!iouReport && isPolicyExpenseChat) {
        iouReport = getNewestOutstandingReportForUser(chatReport.policyID, payeeAccountID, rules, getAllReportNameValuePairs(), allReports);
    }
    

    When the user saves without manually interacting with the report picker, moneyRequestReportID is undefined. MoneyRequestBuilder therefore falls back to getNewestOutstandingReportForUser(...). Inside getNewestOutstandingReportForUser (src/libs/ReportUtils.ts:12457), it explicitly applies:

    const openReports = getOutstandingReportsForUser(policyID, reportOwnerAccountID, rules, reportNameValuePairs, reports, false).filter(isOpenExpenseReport);
    

    Because the "9/21" report is in SUBMITTED state (not open), isOpenExpenseReport filters it out. getNewestOutstandingReportForUser returns null. Line 1453 then triggers:

    if (!iouReport || shouldCreateNewMoneyRequestReport) {
        iouReport = isPolicyExpenseChat ? buildOptimisticExpenseReport(...) : ...
    }
    

    This creates a brand new report, directly conflicting with the "9/21" report shown to the user on the confirmation screen.

What changes do you think we should make in order to solve the problem?

Align the fallback report resolution in ReportField.tsx with MoneyRequestBuilder.ts by using getNewestOutstandingReportForUser:

  1. In src/components/MoneyRequestConfirmationList/sections/ReportField.tsx:

    • Import getNewestOutstandingReportForUser from @libs/ReportUtils.
    • Update outstandingReportID resolution so that the automatic fallback in policy expense chats selects newestOutstandingReport?.reportID rather than availableOutstandingReports.at(0)?.reportID:
    --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
    +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
    @@ -9,7 +9,7 @@ import Navigation from '@libs/Navigation/Navigation';
     import Parser from '@libs/Parser';
     import {getReportName} from '@libs/ReportNameUtils';
    -import {generateReportID, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
    +import {generateReportID, getNewestOutstandingReportForUser, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
    
     import CONST from '@src/CONST';
    @@ -78,7 +78,9 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
             (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
         );
    
    -    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
    +    const newestOutstandingReport = getNewestOutstandingReportForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {});
    +    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? newestOutstandingReport?.reportID) : reportID;
    
         const [selectedReportID, selectedReport] = (() => {
             const reportIDToUse = shouldUseTransactionReport ? transactionReportID : outstandingReportID;
    @@ -95,6 +97,8 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
                 reportToUse = mainReport;
             } else if (reportIDToUse === iouReportIDFromMain) {
                 reportToUse = iouReport;
    +        } else if (reportIDToUse === newestOutstandingReport?.reportID) {
    +            reportToUse = newestOutstandingReport;
             } else {
                 reportToUse = availableOutstandingReports.find((r) => r?.reportID === reportIDToUse);
             }
    
  2. User Experience & Contract Consistency:

    • When a user only has submitted reports awaiting approval, newestOutstandingReport returns undefined. outstandingReportID is undefined, so ReportField displays translate('iou.newReport') ("New report"). The UI accurately informs the user that a new report will be created upon save.
    • If the user explicitly wishes to add the expense to the submitted report awaiting approval, they can tap the Report field to open the report picker (where availableOutstandingReports still presents the submitted report because canAddTransaction(report) is true). Selecting it sets transaction.reportID, making shouldUseTransactionReport true, which both displays and successfully persists to the chosen submitted report.

Automated Tests

  1. Unit Test in ReportField.test.tsx:
    • Verify that when the user has only submitted expense reports awaiting approval and no open reports, ReportField renders "New report" as the default destination.
    • Verify that when an open expense report exists alongside submitted reports, ReportField defaults to the newest open report.
  2. Integration Test in MoneyRequestBuilderTest.ts:
    • Verify that saving an expense without an explicit report selection when only submitted reports exist generates a new report and matches what was presented on the confirmation screen.

What alternative solutions did you explore? (Optional)

Changing MoneyRequestBuilder (and getNewestOutstandingReportForUser) to automatically dump expenses into submitted reports awaiting approval. This was rejected because automatically injecting unexpected expenses into a report that was already submitted for manager approval violates the approval workflow lifecycle and contradicts established test suites in ReportUtilsTest ("creates a new report when the submitter only has a report that is awaiting approval").

camesenin avatar Sep 30 '26 21:09 camesenin

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Sep 30 '26 21:09 melvin-bot[bot]

Thread: https://expensify.slack.com/archives/C049HHMV9SM/p1790775102714569

@MelvinBot it was hard to repro this, but it has something to do with switching to the Inbox tab.

  1. Go to Spend
  2. Create an expense
  3. Submit the report
  4. Click the camera button > take a picture
  5. Observe the report row reads Report: New report :white_check_mark:
  6. Exit the flow
  7. Go to the Workspaces tab
  8. Click the camera button > take a picture
  9. Observe the report row reads Report: New report :white_check_mark:
  10. Go to the Account tab
  11. Click the camera button > take a picture
  12. Observe the report row reads Report: New report :white_check_mark:
  13. Go to the Inbox tab
  14. Click the camera button > take a picture
  15. Observe the report row reads Report: <the report you submitted in step 3> :x:

trjExpensify avatar Oct 01 '26 22:10 trjExpensify

Thanks trjExpensify, those steps explain it. Inbox is the only tab where the confirmation screen loads the workspace chat in the background. That fresh chat data is what makes the Report row pick the submitted report.

Why only Inbox: The camera button and the scan flow work the same on every tab. The difference comes when the confirmation screen opens on a narrow (mobile) layout. To speed up the screen you land on after saving, the app quietly opens that screen behind the confirmation (getSubmitExpensePreMountDestinationRoute.ts:65-78):

  • Inbox: opens the workspace chat behind the confirmation. Mounting that chat calls OpenReport (ReportFetchHandler.tsx:541-550).
  • Workspaces / Account: opens Search instead. No OpenReport on the chat.
  • Spend: Search is already on top, so nothing opens.

When the OpenReport response lands, the Report row recalculates. Its default (ReportField.tsx:81) doesn't check that the report is open. It accepts reports awaiting first approval, so it shows the submitted report. The save path only reuses open reports (ReportUtils.ts:12467-12477), so it creates a new report.

Fix: The earlier proposal still applies. Make the Report row use the same default as the save path, so the background OpenReport can't change what the row shows. The regression test should cover a flow started from Inbox on a narrow layout.

Investigation details
  • Entry point is the same on every tab: the camera button always calls startMoneyRequest(CREATE, <workspace chat>, SCAN) (BaseFloatingCameraButton.tsx:77-95). After capture, the flow sets the same participants and reportID on every tab (NavigateGlobalCreateContext.tsx:99-132).
  • How the issue started: [Performance] Migrate submit-expense flow to usePreMountDestination (merged 2026-07-24) added the background chat mount that only happens from Inbox. Reuse the submitter's outstanding report when the workspace chat has no iouReportID (merged 2026-09-16) made the save path open-only. It also sorted the Report row's list newest-first, so the report you just submitted now comes first.
  • Not confirmed: I can't tell from App code which field in the OpenReport response flips the row. It could be the chat's iouReportID or the report's managerID/state, which decides "awaiting first approval". Either way, the fix above covers it. Submitting from the Spend list doesn't set managerID or clear the chat's iouReportID locally (Search.ts:1582-1590). So the local data before OpenReport can differ from what the server returns.
  • Not reproduced: these findings come from reading code at 8249fbf, not from running the app. The background chat mount happens only on narrow layouts, so a wide desktop browser may not reproduce this.

Next Steps for Contributor+ team: To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/102688#issuecomment-5918469012) to create a draft PR. To refine: @MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.


view run

MelvinBot avatar Oct 01 '26 22:10 MelvinBot

Proposal for Issue #102688

Title: The expense confirmation screen shows an existing submitted report as the destination, but saving the expense silently creates a new report instead

Please re-state the problem that we are trying to solve in this issue.

In New Expensify, when an expense report has just been submitted, initiating an expense creation or receipt scan from the Inbox tab (via floating camera or FAB) causes the confirmation screen to display the previously submitted report under the Report field (e.g. Report: [Submitted Report Name]). However, upon saving the expense, the application silently creates a brand-new report instead of attaching the expense to the displayed report. Conversely, performing the exact same flow from other tabs (Spend, Workspaces, Account) correctly displays Report: New report.


What is the root cause of that problem?

  1. Background Workspace Chat Mount via usePreMountDestination on Inbox tab: On narrow layouts, navigating to the expense creation flow from the Inbox tab mounts the workspace chat in the background to pre-warm the post-submission destination (getSubmitExpensePreMountDestinationRoute.ts:65-78). This triggers OpenReport on the workspace chat (ReportFetchHandler.tsx:541-550), updating mainReport.iouReportID in Onyx with the ID of the recently submitted IOU report.

  2. Unvalidated Fallback on iouReportIDFromMain in ReportField.tsx: In src/components/MoneyRequestConfirmationList/sections/ReportField.tsx (lines 72–81):

    const transactionReportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${transactionReportID}`];
    const shouldUseTransactionReport = (!!transactionReportEntry && isReportOutstanding(transactionReportEntry, policyID, rules, transactionReportNameValuePair, false)) || isUnreported;
    
    const ownerAccountID = selectedParticipants?.at(0)?.ownerAccountID;
    
    const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {}, false).sort(
        (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
    );
    
    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
    

    Notice that while availableOutstandingReports (line 77) and shouldUseTransactionReport (line 73) strictly pass allowSubmitted = false to getOutstandingReportsForUser and isReportOutstanding to ensure submitted reports are excluded, iouReportIDFromMain (mainReport?.iouReportID) is consumed unconditionally without checking isReportOutstanding(..., false).

  3. Divergence from Expense Save Path: When saving the expense, the save logic (requestMoney / IOU.ts) only attaches transactions to open reports (isOpenReport(report)). Because the submitted report is in SUBMITTED / PROCESSING state, the save path discards it and generates a new report, diverging completely from what ReportField promised the user.


What changes do you think we should make in order to solve the problem?

In src/components/MoneyRequestConfirmationList/sections/ReportField.tsx: Validate iouReport against isReportOutstanding with allowSubmitted = false before selecting iouReportIDFromMain:

    const iouReportNameValuePair = reportNameValuePairs?.[`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${iouReportIDFromMain}`];
    const isIouReportOutstanding = !!iouReport && isReportOutstanding(iouReport, policyID, rules, iouReportNameValuePair, false);

    const outstandingReportID = isPolicyExpenseChat ? ((isIouReportOutstanding ? iouReportIDFromMain : undefined) ?? availableOutstandingReports.at(0)?.reportID) : reportID;

When iouReport has been submitted (or is otherwise not open/outstanding):

  1. isIouReportOutstanding evaluates to false.
  2. outstandingReportID skips iouReportIDFromMain and falls back to availableOutstandingReports.at(0)?.reportID.
  3. If no other open report exists for the policy, it resolves to undefined, which generates a new report ID and correctly renders Report: New report in ReportField.
  4. This guarantees that ReportField displays the exact destination report that the save path will attach the expense to across all tabs.

What alternative solutions did you explore? (Optional)

  • Suppressing usePreMountDestination on the Inbox tab: Rejected because background pre-mounting is a deliberate performance optimization to eliminate post-submit transition lag.
  • Clearing mainReport.iouReportID in Onyx on report submission: Rejected because iouReportID on the chat report is maintained by the backend to identify the active transaction thread / IOU report for chat action routing.

Contributor Details

Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~01ff3c252bcc7ad776

substeemguy avatar Oct 04 '26 01:10 substeemguy

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Oct 04 '26 01:10 melvin-bot[bot]

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~0170d6b02e9440e604

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a user submits an expense report (e.g. titled "9/21") such that it is awaiting approval, and then initiates a new cash or scan expense from the workspace chat, the confirmation screen displays the existing "9/21" report in the Report field. However, upon saving the expense, the application creates a brand new report instead of attaching the transaction to the "9/21" report displayed on screen.

What is the root cause of that problem?

There is an architectural mismatch in fallback report resolution between the confirmation UI (ReportField.tsx) and the transaction persistence builder (MoneyRequestBuilder.ts):

  1. In ReportField.tsx (lines 77-84):

    const availableOutstandingReports = getOutstandingReportsForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {}, false).sort(
        (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
    );
    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
    

    When a report has been submitted for approval, the workspace chat's iouReportID is cleared to undefined. ReportField falls back to availableOutstandingReports.at(0)?.reportID. getOutstandingReportsForUser(..., false) checks isReportOutstanding(report, ...) which in turn checks canAddTransaction(report). Because canAddTransaction returns true for reports awaiting first-level approval (isAwaitingFirstLevelApproval(report)), the submitted "9/21" report is included in availableOutstandingReports. Consequently, ReportField displays the submitted "9/21" report as the default destination.

  2. In src/libs/actions/IOU/MoneyRequestBuilder.ts (lines 1429-1435 & 1453):

    if (!iouReport && isPolicyExpenseChat) {
        iouReport = getNewestOutstandingReportForUser(chatReport.policyID, payeeAccountID, rules, getAllReportNameValuePairs(), allReports);
    }
    

    When the user saves without manually interacting with the report picker, moneyRequestReportID is undefined. MoneyRequestBuilder therefore falls back to getNewestOutstandingReportForUser(...). Inside getNewestOutstandingReportForUser (src/libs/ReportUtils.ts:12457), it explicitly applies:

    const openReports = getOutstandingReportsForUser(policyID, reportOwnerAccountID, rules, reportNameValuePairs, reports, false).filter(isOpenExpenseReport);
    

    Because the "9/21" report is in SUBMITTED state (not open), isOpenExpenseReport filters it out. getNewestOutstandingReportForUser returns null. Line 1453 then triggers:

    if (!iouReport || shouldCreateNewMoneyRequestReport) {
        iouReport = isPolicyExpenseChat ? buildOptimisticExpenseReport(...) : ...
    }
    

    This creates a brand new report, directly conflicting with the "9/21" report shown to the user on the confirmation screen.

What changes do you think we should make in order to solve the problem?

Align the fallback report resolution in ReportField.tsx with MoneyRequestBuilder.ts by using getNewestOutstandingReportForUser:

  1. In src/components/MoneyRequestConfirmationList/sections/ReportField.tsx:

    • Import getNewestOutstandingReportForUser from @libs/ReportUtils.
    • Update outstandingReportID resolution so that the automatic fallback in policy expense chats selects newestOutstandingReport?.reportID rather than availableOutstandingReports.at(0)?.reportID:
    --- a/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
    +++ b/src/components/MoneyRequestConfirmationList/sections/ReportField.tsx
    @@ -9,7 +9,7 @@ import Navigation from '@libs/Navigation/Navigation';
     import Parser from '@libs/Parser';
     import {getReportName} from '@libs/ReportNameUtils';
    -import {generateReportID, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
    +import {generateReportID, getNewestOutstandingReportForUser, getOutstandingReportsForUser, isMoneyRequestReport, isReportOutstanding, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
    
     import CONST from '@src/CONST';
    @@ -78,7 +78,9 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
             (report1, report2) => sortOutstandingReportsBySelected(report1, report2, undefined, localeCompare),
         );
    
    -    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID) : reportID;
    +    const newestOutstandingReport = getNewestOutstandingReportForUser(policyID, ownerAccountID, rules, reportNameValuePairs, outstandingReportsForPolicy ?? {});
    +    const outstandingReportID = isPolicyExpenseChat ? (iouReportIDFromMain ?? newestOutstandingReport?.reportID) : reportID;
    
         const [selectedReportID, selectedReport] = (() => {
             const reportIDToUse = shouldUseTransactionReport ? transactionReportID : outstandingReportID;
    @@ -95,6 +97,8 @@ function ReportField({iouType, reportID, reportActionID, action, transactionID,
                 reportToUse = mainReport;
             } else if (reportIDToUse === iouReportIDFromMain) {
                 reportToUse = iouReport;
    +        } else if (reportIDToUse === newestOutstandingReport?.reportID) {
    +            reportToUse = newestOutstandingReport;
             } else {
                 reportToUse = availableOutstandingReports.find((r) => r?.reportID === reportIDToUse);
             }
    
  2. User Experience & Contract Consistency:

    • When a user only has submitted reports awaiting approval, newestOutstandingReport returns undefined. outstandingReportID is undefined, so ReportField displays translate('iou.newReport') ("New report"). The UI accurately informs the user that a new report will be created upon save.
    • If the user explicitly wishes to add the expense to the submitted report awaiting approval, they can tap the Report field to open the report picker (where availableOutstandingReports still presents the submitted report because canAddTransaction(report) is true). Selecting it sets transaction.reportID, making shouldUseTransactionReport true, which both displays and successfully persists to the chosen submitted report.

Automated Tests

  1. Unit Test in ReportField.test.tsx:
    • Verify that when the user has only submitted expense reports awaiting approval and no open reports, ReportField renders "New report" as the default destination.
    • Verify that when an open expense report exists alongside submitted reports, ReportField defaults to the newest open report.
  2. Integration Test in MoneyRequestBuilderTest.ts:
    • Verify that saving an expense without an explicit report selection when only submitted reports exist generates a new report and matches what was presented on the confirmation screen.

What alternative solutions did you explore? (Optional)

Changing MoneyRequestBuilder (and getNewestOutstandingReportForUser) to automatically dump expenses into submitted reports awaiting approval. This was rejected because automatically injecting unexpected expenses into a report that was already submitted for manager approval violates the approval workflow lifecycle and contradicts established test suites in ReportUtilsTest ("creates a new report when the submitter only has a report that is awaiting approval").

camesenin avatar Oct 04 '26 03:10 camesenin

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Oct 04 '26 03:10 melvin-bot[bot]

Contributor Details

  • Contributor: @tuananh4865
  • Email: [email protected]
  • Upwork: https://www.upwork.com/freelancers/~0146a4de0ca9fff602?companyReference=2103308912547901160&mp_source=share

Proposal

Please re-state the problem that we are trying to solve in this issue.

When a user creates an expense or scans a receipt from the Inbox tab after submitting an expense report (e.g. "9/21"), the confirmation screen shows that submitted report as the destination (Report: 9/21). When the user taps Create expense, the app silently creates a new report instead of adding the expense to "9/21".

The same flow from Spend, Workspaces or Account correctly shows Report: New report.

The confirmation UI therefore promises one destination and the save logic delivers another, and the behavior differs by entry point.

What is the root cause of that problem?

The confirmation UI and the save path use different rules to pick the default report, and the Inbox tab feeds the UI a value that only the UI trusts.

1. The Inbox tab hydrates iouReportID in the background. On mobile and narrow layouts, usePreMountDestination (getSubmitExpensePreMountDestinationRoute.ts:65-78) pre-mounts the workspace chat. That triggers OpenReport and populates mainReport.iouReportID in Onyx with the most recently submitted report's ID. The other tabs don't pre-mount the chat, so the value isn't populated there and they fall through to "New report".

2. ReportField trusts that value without validating it. In ReportField.tsx (~line 81):

const outstandingReportID = isPolicyExpenseChat
    ? (iouReportIDFromMain ?? availableOutstandingReports.at(0)?.reportID)
    : reportID;

iouReportIDFromMain (mainReport?.iouReportID) is used as the default without checking that the report is still open (isOpenExpenseReport(iouReport)) or that it can take a new default expense. A submitted report awaiting approval passes through.

The fallback has the same flaw. availableOutstandingReports comes from getOutstandingReportsForUser(..., false), and isReportOutstanding returns true whenever canAddTransaction(iouReport) is true. A submitted report awaiting approval is therefore still in the list, and .at(0) can pick it. That is correct for the manual picker but wrong as an automatic default.

3. The save path applies stricter rules. In MoneyRequestBuilder.ts (~lines 1428-1453):

  • For scan requests with the ASAP-submit beta, or when there is no iouReport, shouldCreateNewMoneyRequestReport is true, so a new report is created.
  • For non-scan requests, the default comes from getNewestOutstandingReportForUser, which filters with .filter(isOpenExpenseReport). A submitted report is never chosen.

So the save logic never defaults to a submitted report, while ReportField can display one. The UI should derive its default with the same rule the save path uses.

What changes do you think we should make in order to solve the problem?

The change is confined to src/components/MoneyRequestConfirmationList/sections/ReportField.tsx. It makes the displayed default follow the same eligibility rule as the save path.

1. Imports

import {getNewestOutstandingReportForUser, isOpenExpenseReport, /* existing imports */} from '@libs/ReportUtils';

2. Validate iouReport before using iouReportIDFromMain, and fall back to the same helper the save path uses

const isIouReportEligible = !!iouReport && isOpenExpenseReport(iouReport);

const newestOutstandingReport = getNewestOutstandingReportForUser(
    policyID,
    ownerAccountID,
    rules,
    reportNameValuePairs,
    outstandingReportsForPolicy ?? {},
);

const fallbackReportID = isIouReportEligible ? iouReportIDFromMain : newestOutstandingReport?.reportID;
const outstandingReportID = isPolicyExpenseChat ? fallbackReportID : reportID;

The arguments mirror what MoneyRequestBuilder passes to getNewestOutstandingReportForUser. I'll reuse the data already available in the component, or the same useOnyx selectors the builder relies on, rather than introducing new sources. Replacing availableOutstandingReports.at(0) with getNewestOutstandingReportForUser is what guarantees display and save agree.

3. Resolve selectedReport for the new fallback source

const [selectedReportID, selectedReport] = (() => {
    const reportIDToUse = shouldUseTransactionReport ? transactionReportID : outstandingReportID;

    if (!reportIDToUse) {
        return [generateReportID(), undefined] as const;
    }

    let reportToUse: OnyxEntry<OnyxTypes.Report> | undefined;
    if (reportIDToUse === transactionReportID) {
        reportToUse = transactionReportEntry;
    } else if (reportIDToUse === reportID) {
        reportToUse = mainReport;
    } else if (reportIDToUse === iouReportIDFromMain) {
        reportToUse = iouReport;
    } else if (reportIDToUse === newestOutstandingReport?.reportID) {
        reportToUse = newestOutstandingReport;
    } else {
        reportToUse = availableOutstandingReports.find((r) => r?.reportID === reportIDToUse);
    }

    return [reportIDToUse, reportToUse ?? undefined] as const;
})();

Resulting behavior

Scenario Displayed Report Saved to
Only a submitted report (e.g. 9/21), no open report, from any tab New report New report
An open report and a submitted report The open report The open report
Non-policy-expense chat (1:1) Unchanged (reportID branch) Unchanged
User taps the field and picks the submitted report The picked report The picked report
  • When the user only has submitted reports awaiting approval, outstandingReportID is undefined. reportName then renders translate('iou.newReport'), which is identical across Inbox, Spend, Workspaces and Account.
  • Manual selection is preserved. The picker still lists the submitted report because availableOutstandingReports is unchanged and canAddTransaction(report) is true. Selecting it sets transaction.reportID, which makes shouldUseTransactionReport true, so the field both displays and persists the chosen submitted report.
  • isPolicyExpenseChat is false for 1:1 chats, so that branch still uses reportID and is untouched.

Test cases

  1. Create and submit report "9/21". From the Inbox tab, tap the camera and scan a receipt. The Report field shows New report, and saving creates a new report without touching "9/21".
  2. Repeat step 1 with manual expense creation from Inbox, and from Spend, Workspaces and Account. All four show New report.
  3. With an open report and a submitted report in the same workspace, the Report field defaults to the open report and saving adds the expense to it.
  4. In a non-policy expense chat (1:1), confirmation behavior is unchanged.
  5. Tap the Report field, select the submitted report "9/21", and save. The expense is added to "9/21".
  6. Add unit tests for ReportField's default resolution in two cases. In the first, iouReport is submitted and no open report exists, so the result is undefined. In the second, iouReport is open, so the result is its ID.

What alternative solutions did you explore? (Optional)

  1. Make the save path attach the expense to the submitted report to match the UI. Rejected. Submitted reports awaiting approval are not valid automatic destinations, and the existing isOpenExpenseReport filter in getNewestOutstandingReportForUser is intentional. Silently adding expenses to a report under review would be a worse product behavior than the current display bug.

  2. Stop pre-mounting the workspace chat in usePreMountDestination on Inbox. Rejected. The pre-mount exists for navigation performance. Removing it regresses that and only hides the symptom for this one entry point, so any other path that hydrates iouReportID would show the same bug.

  3. Filter submitted reports out of availableOutstandingReports globally. Rejected. It would remove the user's ability to deliberately choose a submitted report in the picker, which is a supported flow. The fix should only change the automatic default.

  4. Clear or ignore mainReport.iouReportID on the Inbox path. Rejected. The field is valid data used elsewhere, and the real defect is that ReportField consumes it without the eligibility check.

Follow-up (out of scope): the ASAP-submit scan path in MoneyRequestBuilder forces shouldCreateNewMoneyRequestReport to true. If the UI should show "New report" for that beta even when an open report exists, that is a separate parity item. It would be worth verifying with the beta enabled, but it is not required to fix the reported issue.

tuananh4865 avatar Oct 04 '26 07:10 tuananh4865

@ZhenjaHorbach Eep! 4 days overdue now. Issues have feelings too...

melvin-bot[bot] avatar Oct 05 '26 22:10 melvin-bot[bot]

Please Don't post proposals when an issue doesn't have Help Wanted label

ZhenjaHorbach avatar Oct 06 '26 11:10 ZhenjaHorbach

@MelvinBot Implement your proposal

ZhenjaHorbach avatar Oct 07 '26 16:10 ZhenjaHorbach

🤖 Draft PR created: Make the confirmation Report field show the report the expense is saved to Please mark it as "Ready for review" or ask me to do that for you when it is ready for review.

The Report field now picks its default with the same helpers the save path uses (getNewestOutstandingReportForUser and shouldCreateNewMoneyRequestReport). When saving would create a new report, the field shows "New report" instead of the submitted report. This also covers the Inbox case: whatever the background OpenReport loads, the field can only show a report that saving would use.

Details
  • Changed: ReportField.tsx and its transaction selector, which now passes isScanRequest. A manual pick of a submitted report still works.
  • Tests: a new ReportField.test.tsx has 4 cases. Three fail without the fix, and all four pass with it. The related confirmation-list and GetMoneyRequestInformationTest suites pass (66 tests). Typecheck, lint, spelling, and React Compiler checks pass.
  • Not done: I didn't reproduce the Inbox flow in the app. The Tests and QA sections of the PR are left for the human co-author.

MelvinBot avatar Oct 07 '26 17:10 MelvinBot

Deploy Blocker https://github.com/Expensify/App/issues/105073 was identified to be related to the PR https://github.com/Expensify/App/pull/103361 that attempted to fix this issue

lanitochka17 avatar Oct 26 '26 21:10 lanitochka17

⚠️ Looks like this issue was linked to a Deploy Blocker here

If you are the assigned CME please investigate whether the linked PR caused a regression and leave a comment with the results.

If a regression has occurred and you are the assigned CM follow the instructions here.

If this regression could have been avoided please consider also proposing a recommendation to the PR checklist so that we can avoid it in the future.

melvin-bot[bot] avatar Oct 26 '26 21:10 melvin-bot[bot]

Deploy Blocker https://github.com/Expensify/App/issues/105074 was identified to be related to the PR https://github.com/Expensify/App/pull/103361 that attempted to fix this issue.

lanitochka17 avatar Oct 26 '26 21:10 lanitochka17