Meta? Pass document name, return doctype

Does a method exist that returns a doctype when passed a document name? Something with this sort of flavor?
frappe.meta.get_doctype("PINV-00001")
Which would return “Purchase Invoice” as a string?
I’m not entirely sure where to look if it’s not in model or meta or db or __init__ where it might be.

Nope, there is no such method
But you can check below method

We are using this method for global search, it will return the list of records from multiple doctypes, where the search keyword is present

1 Like

This makes lots of sense. I don’t think there’s a perfect way to get the doctype from a record’s name anyway, except to make the best guess and then try it. I’ll post the code here when I figure it out.

[edit]: I’ll leave this without a solution until I can post the code.

1 Like

Good challenge Tyler -

For code learning clues such as you seek, there’s this here to be mined for gems

apps/frappe/frappe/__init__.py

Dozens of handy well documented methods for the picking - just a few that I recognize

2 Likes

So after kicking this around with the team, we decided to also pull doctype along with any time we needed a name. Not that a function like this wouldn’t be useful, it’s just not as good as doing it the right way the first time.

@clarkej __init__.py is only surpassed in its utility and mystery by erpnext/hooks.py.

1 Like

Bumping into exactly the same issue, found this gem:

[frappe/standard_macros.html at develop · frappe/frappe · GitHub](http://Standard Macros)

Suggested by:

[Where Do I Find Standard Print Formats](http://Where Do I Find Standard Print Formats)

I will post any code I manage to use here.

Ok, so after some time doing trial and error I think I found a solution.
Personally I wanted to use this for Jinja Print template, but it works anywhere on the server:

doc.doctype

is your ticket.
I was inspired by this gem

For use in Jinja, simply call {{ doc.doctype }}and you will obtain the DocType type. For example, if working on an Asset Repair print format, this is what you would obtain in return: “Asset Repair”.

public/myapp.min.js
If using it in combination with a JavaScript callback function, make sure you pass it from the JavaScript args: back to the Python method:

frappe.ui.form.on("Asset Repair", {
    refresh: function (frm, cdt, cdn) {
        frm.add_custom_button(__('MyButton'), function () {
            frappe.call({
                method: "myapp.api.asset_repair",
                args: {
                    asset: frm.doc.asset_name,
                    name: frm.doc.name,
                    doctype: frm.doc.doctype
                },
                callback: function () {
                }
            });
        }).addClass("btn-primary");
    }
});

myapp/api.py

import frappe
from frappe import _

@frappe.whitelist()
def asset_repair(asset, name, doctype):
    frappe.msgprint(_(doctype))

I hope this helps.