Try ERPNext Buy Support Partners Foundation

Frappe.get_doc returning null in web form python file

Hello!

I’ve created a “My Profile” web form referencing the Contact DocType with this settings:

I’m trying to create a page where the website user can update his or her information in the related Contact DocType. The user was created through the “Invite as User” button in the Contact page. One reason why the Contact DocType is used, is because adding of fields in the User DocType is restricted. Here’s the code in the my_profile.py:

from __future__ import unicode_literals

import frappe

import json
import datetime
from frappe.contacts.doctype.contact.contact import get_contact_name

def default(o):
    if isinstance(o, (datetime.date, datetime.datetime)):
        return o.isoformat()
def get_context(context):
    # frappe.clear_cache(doctype="Contact")
    contact_name = get_contact_name(frappe.session.user)
    # frappe.throw(contact_name)
    contact = frappe.get_doc('Contact', contact_name)
    # frappe.throw("hmmm")
    frappe.throw(json.dumps(
        contact,
        sort_keys=True,
        indent=1,
        default=default)
    )
    context.doc = contact
    frappe.form_dict.new = 0
    return context

I am able to frappe.throw the contact_name properly (it returns the correct value) and I could also access any Contact record associated with the user by manually changing the url to http://site/my-profile?name=[name] replacing the [name] with any existing name of Contact.

Anything else that I need to do to make the frappe.get_doc work? I’ve also tried the get_cached_doc method and it is returning null too. I’ve also tried the bench --site all clear-cache.

For reference, I followed this guide.


I think this one’s related to this issue: Mapping webform to doctype, get_context not working

Any suggestions are greatly appreciated!

Thanks!

Hello again.

I just tried the Personal Details web form from the Healthcare domain and it appears to not work also. As an Administrator I created a Patient with an email set to email1@domain.com. Also tried to use a normal non Administrator system user with another email (email3@domain.com) to create the patient record with email as email1@domain.com. This email1@domain.com has a User record with the Patient role. It just shows a blank record:

But if I created the patient using email2@domain.com (system user) and used the same email in the patient record (email2@domain.com), I would be able to get the right doc record in the context:

Since the user who accessed the record in the web form is the same user who created it, it allowed the user to open the record. It seems that frappe.get_doc could only get the document that it has permission on, i.e. the creator, even if “Apply Document Permissions” is unmarked in the web form definition.

Here’s the code of the personal_details.py:

Thank you!