Link Doctype A with Doctype B on Creation

Hi mates,
Someone could help me? I’m stuck with Doctype Linking.

  • I have Doctype A, it has a field with a Link to Doctype B.
  • The user who creates Doctype A does not have access to Doctype B
  • I want it to be linked automatically on Doctype A creation.

Here is my actual controller:

class DoctypeA(Document):
	def on_submit(self):
		doctypeB = frappe.get_all("doctypeB",
			filters = {
				"manager" : frappe.session.user
			}
		)
		if doctypeB:
			self.doctypeA_link = doctypeB[0]["name"]
			self.flags.ignore_permissions = True
			self.save()
		else:
			frappe.throw(frappe._('Search error'))

I don’t know if I have to link doctypeB only with the name or the complete object, Anyways both of them fails.

add_fetch(link_fieldname, source_fieldname, target_fieldname)

https://frappe.github.io/erpnext/user/manual/en/customize-erpnext/custom-scripts/custom-script-examples/custom-script-fetch-values-from-master

Thank you, but, that script is for client scripting and the user who creates doctype A does not have access to doctype B, so I have to do it in a hook, Am I wrong?

Yes you have to do it in hook…

Whats Doctype A and whats doctype B? can you explain your use case.
Is doctype B value always the same?

You can try below method…

  1. Make link field read only.
    2,When user creates a record of doctype A ( inside “validate” or “before_insert”) fetch doctype B value and assign it to doctype A link field).

Doctype A is “Member”
Doctype B is “Association”

Association doesn’t change his name field

Actually the script is trying to do what you are saying. but it does not work.

Is there any reason to make the field read only?
Is there any reason to make it on validate or before_insert instead of on_submit?

Association doesn’t change his name field

Do you mean there is only one Association? There will never be another association?

Is there any reason to make the field read only?

If its not read only it will show up as a field to user and if he clicks on it then he will receive a not permitted error and that can be confusing to user. Since you do not expect the use to input the value you can make it read only.

Is there any reason to make it on validate or before_insert instead of on_submit?

on_submit should work too…
Just be aware that before_insert is triggered only once before record is insert in database… validate is triggered every time user clicks on save (also when submitted) and submit is triggered only once when submit button is clicked… You can use whichever event is appropriate.

Do you mean there is only one Association? There will never be another association?

No, there are many associations.

If its not read only it will show up as a field to user and if he clicks on it then he will receive a not permitted error and that can be confusing to user. Since you do not expect the use to input the value you can make it read only.

I have the field on perm level method, so it does not show to the user who creates Association.

I solved my problem

def before_insert(self):
		linked_aso = frappe.get_all("Association",
			filters = {
				"manager" : frappe.session.user
			}
		)
		if linked_aso:
			self.aso = linked_aso[0]["name"]
			self.flags.ignore_permissions = True
		else:
			frappe.throw(frappe._('No linked aso'))

David,
I want to understand your use case a little better. According to your business logic, it’s only required that the member belong to an association, but it doesn’t matter which association? Is there no association-specific logic? What kind of business/ app is this for?

And there’s no rule against verbose error messages, consider a first time user’s confusion.
-T

  • There are many associations with one manager each one assigned by me (super administrator).
  • The manager can add members to their respective associations, but manager can’t change his association info
  • I can see all Asociations and all members
1 Like

Interesting. Good luck!