Skip to content

fix: link deal addresses to customer on auto-creation - #2293

Open
chinmaybhatk wants to merge 1 commit into
frappe:developfrom
chinmaybhatk:fix/link-deal-addresses-to-customer
Open

fix: link deal addresses to customer on auto-creation#2293
chinmaybhatk wants to merge 1 commit into
frappe:developfrom
chinmaybhatk:fix/link-deal-addresses-to-customer

Conversation

@chinmaybhatk

Copy link
Copy Markdown

Problem

When a CRM Deal moves to "Won" and a Customer is auto-created (via ERPNext CRM Settings > Create Customer on Status Change), addresses linked to the deal are not re-linked to the new Customer.

This causes ERPNext to throw:

"Billing Address does not belong to {Customer}"

when creating Sales Orders or Invoices against the auto-created Customer.

Root Cause

In create_customer_from_deal(), only the organization's address (via get_organization_address()) is passed to ERPNext's create_customer API. Addresses linked directly to the CRM Deal via Dynamic Links are completely ignored.

After customer creation, those deal-linked addresses still only have:

link_doctype: "CRM Deal", link_name: "CRM-DEAL-xxxx"

They're missing:

link_doctype: "Customer", link_name: "<new_customer>"

Steps to Reproduce

  1. Create a CRM Deal with an address (via FCRM Data tab)
  2. Move the deal to Won → Customer is auto-created
  3. Go to ERPNext → Create Sales Order for that Customer
  4. Select the billing address → Error: address does not belong to Customer

Solution

Added link_deal_addresses_to_customer() which runs immediately after customer creation in create_customer_from_deal(). It:

  1. Queries all addresses linked to the CRM Deal via Dynamic Links
  2. For each address, checks if a Customer link already exists (idempotent)
  3. Appends a Customer Dynamic Link entry if missing
  4. Wraps each address update in try/except to avoid breaking the main flow

Scope

  • Same-site deployments: Fully handled (addresses are on the same DB)
  • Remote ERPNext sites: Skipped (organization address is already handled via the create_address API call; deal-level addresses on remote sites would require ERPNext API changes)

Change Summary

File Change
erpnext_crm_settings.py Added link_deal_addresses_to_customer() function
erpnext_crm_settings.py Called it from create_customer_from_deal() after customer creation

Closes #2292

When a CRM Deal moves to Won and a Customer is auto-created,
addresses linked to the deal via Dynamic Links are not re-linked
to the new Customer. This causes ERPNext to reject the address
in Sales Orders/Invoices with "Billing Address does not belong
to {Customer}".

Added `link_deal_addresses_to_customer()` which runs after customer
creation and adds a Dynamic Link entry for the new Customer on
every address that references the CRM Deal.

Closes frappe#2292
@codecov-commenter

codecov-commenter commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 7.14286% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 61.26%. Comparing base (3ccc397) to head (6d38b8a).
⚠️ Report is 259 commits behind head on develop.

Files with missing lines Patch % Lines
...ctype/erpnext_crm_settings/erpnext_crm_settings.py 7.14% 13 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2293      +/-   ##
===========================================
- Coverage    61.62%   61.26%   -0.36%     
===========================================
  Files          141      137       -4     
  Lines         8315     8205     -110     
===========================================
- Hits          5124     5027      -97     
+ Misses        3191     3178      -13     
Files with missing lines Coverage Δ
...ctype/erpnext_crm_settings/erpnext_crm_settings.py 19.72% <7.14%> (-0.63%) ⬇️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@chinmaybhatk

Copy link
Copy Markdown
Author

@Mergifyio queue

@mergify

mergify Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

queue

☑️ Command disallowed due to command restrictions in the Mergify configuration.

Details
  • sender-permission >= write

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Address not linked to Customer when auto-created on Deal "Won"

2 participants