Access/GET Dynamic Links/Child Table in REST API

Hi,

I am pulling the Customer Data through REST API into my external app. I am able to pull all the data of a customer, but not the Address and Contacts data.

I saw that it is a Link/Dynamic Link also called as child table.

How do I access Address & Contacts through the Customer API?

Or to put in in simpler terms, how can the Child table be accessed from parent table via REST API?

Hi.

Could you figure that out?

The typical approach to this, I think, would be to make a separate request, selecting documents from the child table doctype with a “parent” field that matches your root doctype.

1 Like

If you call the frappe.get_doc as an RPC call (not REST) it will return what get_doc returns.

1 Like

Thanks for your suggestion, @peterg.

-Which would be the “parent” field in “Quotation Item” (which is the “Child Table” of Quotation)?

I can’t see in the Quotation Item doctype any field related to its Parent Doctype (in this case Quotation). I mean a field like “quotation” which has the Foreign Key to Quotation.

In Quotation (and in the majority of the doctypes in ERPNext) the table is accessible with doc.items.
For the child document, the parent is referenced as child_doc.parent

2 Likes

Thanks Tyler!

But I can’t get items from the json object returned from the api, can I?

I have no problem console logging obj.name or any of the other properties of the Parent Doctype (Quotation in this case), but obj.items is undefined and I can’t see items in the property list of the json object.

I think I’m understanding your question correctly, but correct me if not. I haven’t played around much with the Quotation doctype specifically, but assuming it works like others (and at quick glance it looks like it does): quotation items aren’t actually referenced anywhere in the Quotation doctype itself.

Instead, you find them by running a query against the Quotation Item doctype itself, filtered by the field parent = whatever the id of your parent item is. When you look at the Quotation Item doctype, you’re right that there’s no “parent” field listed, but this is automatically created on the back-end by Frappe for any doctype designated as a child. I’m away from my desk for the next few days so I can’t easily generate test code, but hopefully that makes sense in the abstract at least. Let me know if not!

That’s correct.

Here is the answer I got from @rmehta:

and this was my conclusion:

I would appreciate if you give me your thoughts on my conclusion, @peterg and @tmatteson

You’re right; I was misremembering how this works in REST, conflating it with the approach you’d take in Python. It does not appear possible to poll child doctypes via REST.

However, on my v11 system at least, the API returns all child doctypes as part of the parent JSON. For example:

curl --header "Authorization: token xxx:xxx" https://example.com/api/resource/Quotation/QTN-00001

returns:

{
  "data": {
    "additional_discount_percentage": 0,
    "base_in_words": "NPR Twelve Thousand only.",
    "naming_series": "QTN-",
    "creation": "2017-06-28 11:19:43.407141",
    "doctype": "Quotation",
    "currency": "NPR",
    "conversion_rate": 1,
    "payment_schedule": [],
    "owner": "peter@example.com",
    "total_qty": 1,
    "total": 12000,
    "customer_name": "Generic Company",
    "language": "en",
    "modified_by": "peter@example.com",
    "order_type": "Sales",
    "title": "Generic Company",
    "base_total": 12000,
    "selling_price_list": "Standard Selling",
    "transaction_date": "2017-05-01",
    "docstatus": 1,
    "territory": "Nepal",
    "company": "My Company",
    "letter_head": "Generic",
    "group_same_items": 0,
    "ignore_pricing_rule": 0,
    "base_rounded_total": 12000,
    "quotation_to": "Customer",
    "total_taxes_and_charges": 0,
    "grand_total": 12000,
    "base_discount_amount": 0,
    "party_name": "Generic Company",
    "base_total_taxes_and_charges": 0,
    "items": [
      {
        "stock_qty": 1,
        "base_price_list_rate": 12000,
        "image": "",
        "creation": "2017-06-28 11:19:43.407141",
        "base_amount": 12000,
        "qty": 1,
        "margin_rate_or_amount": 0,
        "rate": 12000,
        "total_weight": 0,
        "owner": "peter@example.com",
        "stock_uom": "Qty",
        "base_net_amount": 12000,
        "page_break": 0,
        "modified_by": "peter@example.com",
        "base_net_rate": 12000,
        "discount_percentage": 0,
        "item_name": "Item for Sale",
        "amount": 12000,
        "actual_qty": 0,
        "net_rate": 12000,
        "conversion_factor": 1,
        "base_rate_with_margin": 0,
        "warehouse": "Stores - KC",
        "docstatus": 1,
        "uom": "Qty",
        "description": "Item description",
        "parent": "QTN-00001",
        "base_rate": 12000,
        "item_code": "10000",
        "projected_qty": 0,
        "margin_type": "",
        "doctype": "Quotation Item",
        "rate_with_margin": 0,
        "discount_amount": 0,
        "price_list_rate": 12000,
        "name": "QUOD/00003",
        "idx": 1,
        "item_tax_rate": "{}",
        "item_group": "Courses",
        "modified": "2017-06-28 11:32:56.607867",
        "weight_per_unit": 0,
        "parenttype": "Quotation",
        "net_amount": 12000,
        "parentfield": "items"
      }
    ],
    "base_grand_total": 12000,
    "discount_amount": 0,
    "status": "Submitted",
    "base_net_total": 12000,
    "name": "QTN-00001",
    "idx": 2,
    "total_net_weight": 0,
    "rounded_total": 12000,
    "price_list_currency": "NPR",
    "modified": "2017-06-28 11:32:56.607867",
    "taxes": [],
    "rounding_adjustment": 0,
    "plc_conversion_rate": 1,
    "apply_discount_on": "Grand Total",
    "net_total": 12000,
    "base_rounding_adjustment": 0,
    "in_words": "NPR Twelve Thousand only.",
    "customer_group": "Non Profit"
  }
}

You seem to be getting different results? Is it possible that there’s a permissions problem?

I got it, @peterg

The API returns all the Child Documents ONLY when you call it in the form of:

https://example.com/api/resource/Doctype/DoctypeName

and there is only one result, but NOT when you call the API with filters like this:

https://example.com/api/resource/Doctype/&filters=[["Doctype","fieldname","=","criteria"]]

5 Likes

Hello @CostaRica. Also having the same issue getting values from a child doctype via REST Api. I am trying to access Item’s child table “Item Supplier” with this:

Is there something wrong with my syntax? Thank you in advance

Hi Nier0.

Please specify the exact sintax you used (just replace your domain with example.com).

e.g https://example.com/api/resource/Quotation/QTN-00001 and the error thrown.

Also specify a sintax of another Doctype that does work.

Regards

1 Like

thank you very much ,it works with me

Hi, have you found a way to get ALL rows from a Child DocType? I mean the whole table

You can try this Server Script:

docType = frappe.form_dict.docType
if not docType:
    frappe.throw("Parameter docType missing")
    
frappe.response['data'] = frappe.db.get_list(docType, fields=["*"])

Usage: http://localhost:8000/api/method/get_list?docType=Employee%20Skill

Set it up like this and make sure that Server is enabled (see Server Script):