Field accepting value but won't save

Odd problem here.

I have users who have permissions to work with and create documents for a few employees.

I also have a few documents that those same users should only be able to create for themselves. To do this I made the “employee” link field for those documents read-only. If a user is only permitted one employee number then it auto fills that field with thier name and its fine. However it doesn’t auot-fill is you have more than 1 permission to an employee.

To deal with this I added this custom script to that doctype:
cur_frm.set_value("employee", frappe.defaults.get_user_default("Employee"));

It works, and it will add the default logged in user to the field, EXCEPT, when I save the document it tells me the field is empty. I can see that it’s not empty just before I try to save:

Any thoughts?

@Dbone let me try to reproduce at my end. Also, is there any error on console while setting value to field?

Found this in the console-logging section of Firefox: thank you for looking in to this!

Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 242, in _save
self.insert()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 190, in insert
self._validate()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 384, in _validate
self._validate_mandatory()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 604, in _validate_mandatory
name=self.name))
MandatoryError: [Expense Claim, EXP00012]: employee
desk.min.js:151:95
Traceback (most recent call last):
File “/home/frappe/frappe-bench/apps/frappe/frappe/app.py”, line 55, in application
response = frappe.handler.handle()
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 19, in handle
execute_cmd(cmd)
File “/home/frappe/frappe-bench/apps/frappe/frappe/handler.py”, line 40, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File “/home/frappe/frappe-bench/apps/frappe/frappe/init.py”, line 896, in call
return fn(*args, **newargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/desk/form/save.py”, line 22, in savedocs
doc.save()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 223, in save
return self._save(*args, **kwargs)
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 242, in _save
self.insert()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 190, in insert
self._validate()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 384, in _validate
self._validate_mandatory()
File “/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py”, line 604, in _validate_mandatory
name=self.name))
MandatoryError: [Expense Claim, EXP00012]: employee
desk.min.js:151:95

@saurabh6790

I think this is a problem with permissions. I’ve run MANY tests to narrow things down:

  • if a user is only permitted to one employee doctype, and they have “read-only” permission only on the employee field, then ERPNext will automatically put their employee number in the field, the employee can see it only, and the document will save.
  • if a user is permitted to multiple employee doctypes, and they have “write” permission on the employee field, then my custom script will populate the field, because ERPNext won’t automatically, and the document will save.
  • if a user is permitted to multiple employee doctypes, and the employee field is “read-only” and everyone has permission for it, then my custom script will populate the field, because ERPNext won’t automatically, and the document will save.

It looks like if the user has no permission or read-only permission to a field then customs scripts cannot add anything to it either, even though it looks like it does. Whatever ERPNext itself does to add a default user seems to work though.