Rename records with rest api?

Hello,

I’m working on an external script that uses REST Api to import and change records (Leads for now), and I might need to rename some records. So I’ve configured the Lead doctype to allow renaming, and I can successfully rename records with the form on the Lead record itself, or by using the bulk rename tool.

But when I try to use the Rest Api, the ‘name’ field sent in the body is used to find the record, and not to update the field itself, so I have an error. This works if I change other fields. I really want to change the ‘name’ field, not just the ‘lead_name’, as the script needs to update things after changing naming serie.

I’m using a PUT request to /api/resource/Lead/<name of the lead, CRM-LEAD-YYY-XXXXX>, with the updated ‘name’ field in the request body, such as ‘CRM-NEWNAME-YYYY-XXXXX’.

The error is that the record is not found, I think because the API use the new name field in the body to search for the record to modify.

Is there any way to update record’s names through the rest api ?

Thanks for your help. And thanks to the whole Frappe/ERPNext team for such nice softwares :slight_smile:

You can create a new function.

@frappe.whitelist(allow_guest=True)
def rename(curr_lead_name, new_lead_name):
    frappe.rename_doc("Lead", curr_lead_name, new_lead_name, ignore_permissions=True)
    return 'OK'

And then you can call /api/method/{dotted.path.to.function} based on documentation ( https://frappe.io/docs/user/en/guides/integration/rest_api )

Try it with POST .

Note: ignore_permissions=True & allow_guest=True are optional.

2 Likes

Thanks for your quick & useful answer :slight_smile:

I created a custom app and added my own function, and it works just fine from my external scripts using rest api. Took less than 5 minutes …

After digging around, I’ve found that the method rename_doc is already whitelisted in frappe, so I can also achieve the rename using rest api by calling /api/method/frappe.client.rename_doc with its parameters (doctype, old_name, new_name, merge that is not required and defaults to False). So I don’t even have to bother adding my own function. Both works fine.

But I’ll keep my custom app and add a few functions to reduce complexity of my external script.

Thanks to Frappe/ERPNext team for such great tools :slight_smile:

4 Likes

Glad to help. :slight_smile: