How to bulk re-fetch link field

Hello, is there a way to update / re-fetch a specific link field for a doctype ? i need to bulk refetch the customer adress field in all sales orders so that the address_display updates. Best regards.

Create a tenporary custom field to store the current link field value, just in case something unexpected happens.

Then through system console / server script clear the value of the link field of all docs and db commit.

Then through the same, set the value of the link field to be the value of the temporary field we created at the first step.

Once everything is working, delete your temporary custom field

This is my Solution for others needing the same, just paste it into the system console → change the doctype and link_fieldname as needed → execute.

# --- adjustable parameters ---
doctype         = "Sales Order"
link_fieldname  = "customer_address"

def update_doc(d):
    val = d.get(link_fieldname)
    if val:
        d.set(link_fieldname, val)      # re-assign same value
        d.save()
        frappe.db.commit()
        print(f"Updated {d.doctype} {d.name}")
    else:
        print(f"Skipped (empty) {d.name}")


names = frappe.get_all(doctype, pluck="name")
print(f"Processing {len(names)} {doctype}s …")
for n in names:
    try:
        update_doc(frappe.get_doc(doctype, n))
    except Exception as e:
        print(f"Error processing {n}: {e}")
print("Done!")

this is the output from my testing:

Error processing AB-00021: [Sales Order, AB-00021]: company_contact_person
Updated Sales Order AB-00018
Updated Sales Order AB-00009
Updated Sales Order AB-00012
Updated Sales Order AB-00014
Updated Sales Order AB-00006
Updated Sales Order AB-00002
Error processing AB-00020: [Sales Order, AB-00020]: company_contact_person
Error processing AB-00019: [Sales Order, AB-00019]: company_contact_person
Error processing AB-00015: [Sales Order, AB-00015]: company_contact_person
Updated Sales Order AB-00017

As you can see it handles errors nicely and outputs them so you can fix the problem. (If you are wondering In my case the missing company_contact_person causes an error because i made this field mandatory after creating the sales orders so now they can only be saved after adding the mandatory field to the document)