How to fill the detail into child table

Actually, I have a requirement that I want to show the parent BOM in the BOM Doctype, therefore I created a new child doctype name BOM Parent and attached it to BOM.
Now, What I want is when the BOM get loaded it should pull all the parent records of the selected BOM as it shows the child record of the BOM.

JS Code:

frappe.call({
                           method: "library_management.library_management.paste_files.get_bom_detail",
					//method: "frappe.core.doctype.file.file.create_new_folder",
					args: data2,
                                        callback: function(r) { 
                             if(!r.exc && r.message) {
					for ( var i=0; i< r.message.length; i++ ) {
						var d = frm.add_child("bom_parent");
						var item = r.message[i];
						for ( var key in  item) {
							if ( !is_null(item[key]) ) {
								d[key] = item[key];
							}
						}
					}
					frm.refresh_field("bom_parent");
				}
		}
		})

Python Code:
@frappe.whitelist
def get_bom_detail(item_code):
return frappe.db.sql(""“select name from tabBOM where item=%s”"", item_code, as_dict=1)

Any help in this context will be appreciated.

Edit: I tried using above code but I receive a message "Not Permitted"
Regards
Ruchin Sharma

I found some issue in this code that if a BOM is submitted then system will not allow us to add record in the BOM Parent Table, therefore I am looking for a way where any new BOM is get saved its parent details should also get saved in the BOM Parent Table.

Is there any way to do it like some trigger of by some code?

Regards
Ruchin Sharma

@ruchin78, this is the code that populate the Sales BOM Detail in Sales Order

1 Like

But, what if the BOM is already submitted, will this code work in that case?

Regards
Ruchin Sharma

@ruchin78, do you dont will save this information
Do you only will load

Do you dont need keep it in DB, it’s only for conference aspects!

Sorry max I didn’t understand what you are trying to say?

do you need hook a function to the method onload of the class to populate the children, I give to you a reference abou how do that!

and in the before_save method do you need put another hook to set the field as [] to clear the table and prevent to save it in DB

Ah -ok, but I really don’t have any idea about using hooks even I tried to use it once but didn’t get much to use that.

Regards
Ruchin Sharma

Hi @max_morais_dmm
I tried to do it by when the code runs it says Not Permitted:

below is my code:
Python Code:

@frappe.whitelist 
def get_bom_detail(doc,item):
	pi = doc.append('parent_bom', {})
	for i in frappe.db.sql("""select parent,bom_no,parent as parent_bom from `tabBOM Item` where item_code=%s and parent is null""", item, as_dict=1):
		pi.parent = i.parent
		pi.bom_no = i.bom_no
		pi.parent_bom = i.parent_bom	

JS Code:

frappe.ui.form.on("BOM","onload", function(frm, doctype, name) {
		      	var data =  {
					 "doc": frm.doc,
                                                "Item": frm.doc.item
				};
				frappe.call({
                           method: "library_management.folder_scriptss.get_bom_detail",
			   args: data,
                           callback: function(r) { }
				})
});

Regards
Ruchin Sharma

@ruchin78, @frappe.whitelist() instead of @frappe.whitelist

HI Max,
I have tried that too after your reply but the getting the same error.

Regards
Ruchin Sharma

@ruchin78, dont forget of use bench restart to instances in prodution mode or CTRL+C and bench start to instances in development!

Thanks @max_morais_dmm
I did a bench restart and I got the below error:

Traceback (innermost last):
File “/home/sam/frappe-bench/apps/frappe/frappe/app.py”, line 57, in application
response = frappe.handler.handle()
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 36, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/sam/frappe-bench/apps/frappe/frappe/init.py”, line 805, in call
return fn(*args, **newargs)
File “/home/sam/frappe-bench/apps/library_management/library_management/folder_scriptss.py”, line 31, in get_bom_detail
pi = doc.append(‘parent_bom’, {})
AttributeError: ‘unicode’ object has no attribute ‘append’

Screenshots of the BOM Doctype Structure:

Screenshot of BOM Parent Doctype Strcture:

Regards
Ruchin Sharma

@max_morais_dmm

There was a mistake in the code I did change it from pi = doc.append(‘parent_bom’, {}) to pi = doc.append(‘bom_parent’, {})

and after changing the code, I got this error:

Traceback (innermost last):
File “/home/sam/frappe-bench/apps/frappe/frappe/app.py”, line 57, in application
response = frappe.handler.handle()
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/sam/frappe-bench/apps/frappe/frappe/handler.py”, line 36, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/sam/frappe-bench/apps/frappe/frappe/init.py”, line 805, in call
return fn(*args, **newargs)
File “/home/sam/frappe-bench/apps/library_management/library_management/folder_scriptss.py”, line 31, in get_bom_detail
pi = doc.append(‘bom_parent’, {})
AttributeError: ‘unicode’ object has no attribute ‘append’

Regards
Ruchin Sharma

Hi @max_morais_dmm
Any idea about this?

@ruchin78 it’s occurs when you get a data from the browser without parse it

import json

@frappe.whitelist()
def get_bom_details(doc, item):
    if isinstance(doc, basestring):
        doc = frappe.get_doc(json.loads(doc))  

@max_morais_dmm

I tried your code, however it didn’t gave me the previous error but it says “Not Permitted”.

Below is my code:

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
import logging
import string
import datetime
import re
import json

from frappe.utils import getdate, flt,validate_email_add,cint
from frappe.model.naming import make_autoname
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc

_logger = logging.getLogger(frappe.__name__)


@frappe.whitelist
def get_bom_detail(doc, item):
    if isinstance(doc, basestring):
        doc = frappe.get_doc(json.loads(doc)) 
	pi = doc.append('parent_bom', {})
	for i in frappe.db.sql("""select parent,bom_no,parent as parent_bom from `tabBOM Item` where item_code=%s and parent is null""", item, as_dict=1):
		pi.parent = i.parent
		pi.bom_no = i.bom_no
		pi.parent_bom = i.parent_bom

I have tried this too

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from __future__ import unicode_literals
import frappe
import logging
import string
import datetime
import re
import json

from frappe.utils import getdate, flt,validate_email_add,cint
from frappe.model.naming import make_autoname
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc

_logger = logging.getLogger(frappe.__name__)


@frappe.whitelist
def get_bom_detail(doc, item):
    if isinstance(doc, basestring):
        doc = frappe.get_doc(json.loads(doc)) 
	pi = doc.append('parent_bom', {})
	for i in frappe.db.sql("""select bom_no,paren from `tabBOM Item` where item_code=%s and parent is null""", item, as_dict=1):
		pi.bom_no = i.bom_no
		pi.parent_bom = i.parent

Any help in this context will be appreciated.

Regards
Ruchin Sharma

@ruchin78, I’m sorry to told, but do you is failling in silly things! Like revise the thread! I started solving this problem too you! And are you reporting the same problem again! I’m sorry! but I dont will help you with this again!

@max_morais_dmm
I apologize for the same and I got my mistake that I have done.

Please see my revise code and error:
JS Code:

frappe.ui.form.on("BOM","onload", function(frm, doctype, name) {
		      	var data =  {
					 "doc": frm.doc,
                                                "Item": frm.doc.item
				};
				frappe.call({
                           method: "library_management.folder_scriptss.get_bom_detail",
			   args: data,
                           callback: function(r) { }
				})
});


Python Code:


# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

from future import unicode_literals
import frappe
import logging
import string
import datetime
import re
import json

from frappe.utils import getdate, flt,validate_email_add,cint
from frappe.model.naming import make_autoname
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc

_logger = logging.getLogger(frappe.name)

@frappe.whitelist()
def get_bom_detail(doc, item):
if isinstance(doc, basestring):
doc = frappe.get_doc(json.loads(doc))
pi = doc.append(‘parent_bom’, {})
for i in frappe.db.sql(""“select parent,bom_no,parent as parent_bom from tabBOM Item where item_code=%s and parent is null”"", item, as_dict=1):
pi.parent = i.parent
pi.bom_no = i.bom_no
pi.parent_bom = i.parent_bom


Error:
Traceback (innermost last):
  File "/home/sam/frappe-bench/apps/frappe/frappe/app.py", line 57, in application
    response = frappe.handler.handle()
  File "/home/sam/frappe-bench/apps/frappe/frappe/handler.py", line 19, in handle
    execute_cmd(cmd)
  File "/home/sam/frappe-bench/apps/frappe/frappe/handler.py", line 36, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/home/sam/frappe-bench/apps/frappe/frappe/__init__.py", line 805, in call
    return fn(*args, **newargs)
 TypeError: get_bom_detail() takes exactly 2 arguments (1 given)