Create Entry Programmatically

Hi,
I’ve two Doctypes named Project(built-in) & Complain Box (Custom in HR Module)
I’ve a field in Complain Box named ‘project_name’
I need whenever Complain Box is saved it should create new Project automatically,

I tried,
PY

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import frappe

from frappe.model.document import Document

class Complain():

@frappe.whitelist()
def create_project(project_name):
	doc = frappe.new_doc("Project")
	doc.save()
	frappe.db.commit()
	return doc.name 

JS

frappe.ui.form.on("Complain Box", {
  after_save: function(frm) {

    frappe.call({
      method: 'erpnext.project.project.create_project',
      args: {
          project_name: frm.doc.project_name
      },
      callback: function(r) {
        if(r.message) {
          frappe.msgprint("Project created.");
        } 
      }
    });
  }
});

But this giving an error when i am saving the Complain Box “The Resource you are looking for is not available”
I don’t know much how to code in python, but i am willing to learn, can someone please help me out to achieve this?

Seems, the path is not correct, please check and correct the path

Which path should i put here?
Currently i’ve given the where the form should be created, means i want to create a new project so i gave the Project’s path.
Second thing, i created the python file in Complain Box’s folder, is it correct? Or it should be created in Project folder?

This depends on where your “Complain” doctype is located.

Assuming you have your app “yourapp”, your python file should be located in /home/frappe/frappe-bench/apps/yourapp/yourapp/yourapp/doctype/compain/complain.py. With the method name being def create_project, that would give you a command

method: 'yourapp.yourapp.doctype.complain.complain.create_project'

The logic being (app).(module).doctype.(doctype_name).(python_file).(function_name), corresponding to the folder structure.

If you have really included it directly in ERPNext itself, make sure the path is correct. You may also troubleshoot using

$ cd /home/frappe/frappe-bench
$ bench execute yourapp.yourapp.doctype.complain.complain.create_project --args "['project name']"

Hope this helps.

5 Likes

@lasalesi thank you so much for your help,

I’ve successfully inserted data in Project using,

PY,

def create_project():

	doc = frappe.get_doc({
		"doctype": "Project",
		"project_name": "My Project New",
		"status": "Open"
	})
	doc.insert() 

JS.

frappe.ui.form.on("Complain Box", {
  after_save: function(frm) {
	frappe.msgprint("Working");
    frappe.call({
      method: 'erpnext.hr.doctype.complain_box.complain_box.create_project',
      args: {
          project_name: frm.doc.project_name
      },
      callback: function(r) {
        if(r.message) {
          frappe.msgprint("Project created.");
        } 
      }
    });
  }
});

But the above method is inserting Project name with giving name, “My New Project”,
Not with the value of project_name field in Complain box.
Can you please tell me how should i transfer value from complain box to Project like project_name etc?

And the second thing is, Project is inserting only when i execute method from bench,
not when i save Complain Box.
The old error is still there "The resource you are looking for is not available"

1 Like

Hi @shahid,

thanks for your feedback.

You will need the function parameter in the Python function as you had it initially. I have extended the parameter in the bench execute call above.

As for your second point, could it be that this is an access issue? With the logged in user, can you create projects?

@lasalesi
Yes that was parameter’s issue. (fixed)
& yes that was permission issue,
is there any way that my function could be able to ignore permissions?

This should be possible when you run insert with option ignore_permissions=True

doc.insert(ignore_permissions=True)
1 Like

@lasalesi Thank you so much. :slight_smile: