REST API: Querying related Records?

What’s the proper way to query related records via the REST API? If I have a Prospect that has Multiple Leads, how do I use REST to find the Prospect’s Lead?

How do I use a Lead record to find the Lead’s Prospect?

The leads will be a part of that Prospect stored in the child table.

It would be a simple GET query which looks like this:

Query:

curl --location --request GET 'https://yourfrappesite/api/resource/Prospect/{{prospect_name}}' \
--header 'Authorization: token API_key:secret' \
--header 'Content-Type: application/json' \

Response:


{
    "data": {
        "name": "Comp 1",
        "owner": "owner_name",
        "creation": "2022-10-06 12:34:29.177889",
        "modified": "2022-10-06 12:34:38.759008",
        "modified_by": "owner_name",
        "docstatus": 0,
        "idx": 0,
        "company_name": "Comp 1",
        "no_of_employees": "1-10",
        "annual_revenue": 0.0,
        "company": "Dunder Mifflin",
        "doctype": "Prospect",
        "opportunities": [],
        "notes": [],
        "leads": [
            {
                "name": "71390bf45d",
                "owner": "owner_name",
                "creation": "2022-10-06 12:34:29.177889",
                "modified": "2022-10-06 12:34:38.759008",
                "modified_by": "owner_name",
                "docstatus": 0,
                "idx": 1,
                "lead": "CRM-LEAD-2022-00001",
                "lead_name": "Lead 1",
                "lead_owner": "owner_name",
                "status": "Lead",
                "parent": "Comp 1",
                "parentfield": "leads",
                "parenttype": "Prospect",
                "doctype": "Prospect Lead"
            }
        ]
    }
}

Thanks for the input.

I was hoping to be able to Query all leads based on paramters (instead of querying a single lead at a time) and return the Child Tables as a column of data instead of having to query the leads one-by-one…

Got it.

There is some issue with Frappe returning child table data using the fields parameter.

Eg:

curl --location -g --request GET 'https://yourfrappesite/api/resource/Prospect?fields=["name", "prospect_owner"]' \
--header 'Authorization: token API key:secret \
--header 'Cookie: full_name=Guest; sid=Guest; system_user=no; user_id=Guest; user_image='

Response:


{
    "data": [
        {
            "name": "Comp 1",
            "prospect_owner": null
        },
        {
            "name": "Comp 2",
            "prospect_owner": "owner_name"
        }
    ]
}

When you try the field parameter with the child table (leads in this case) it returns an invalid error.

Trying to query the child table API returns a permission error.

Perhaps @ankush can help out a bit here :sweat_smile:

You can use client.py whitelisted methods which will let you run get_list like queries from API

These aren’t documented well but should have quite a lot of composability over regular REST APIs. These are unlikely to change/break cause they are assumed to be “Public” APIs.

1 Like

So, there’s no “Official” way to query child tables in ERPNext with the REST API? Even if I want to Query them by, for example, the parent name?

For example, how can we Query CRM Notes if we have the Lead’s Name?

1 Like

Something like this:

frappe.call({ 
    method:"frappe.client.get_list",
	args:{
		doctype: "Prospect Lead",
		fields: ["*"],
        parent: "Prospect",
        filters: {lead_name: "Jimmy Durante"}
	}
}).then(r => console.log(r))
1 Like

Is that an in-built whitelisted function? Or do I have to whitelist that somehow?

It’s a whitelisted funtion:

A set of notes on typical rest setup and operations would appreciated. When trying to learn I wonder if the problem is what I am doing or the way the system works or needs to be configured.

Even if the notes relate only to a particular version of Erpnext at least the reader is aware of the operating conditions eg if you use another version then changes may be required. Thanks.

That still doesn’t work with child tables…