Course Content
Validation for Transaction
Faris Ansari
May 19, 2021

When an Article is issued, we should verify whether the Library Member has an active membership. We should also check whether the Article is available for Issue. Let's write the code for these validations.

from __future__ import unicode_literals

import frappe
from frappe.model.document import Document

class LibraryTransaction(Document):
    def before_submit(self):
        if self.type == 'Issue':
            # set the article status to be Issued
            article = frappe.get_doc('Article', self.article)
            article.status = 'Issued'

        elif self.type == 'Return':
            # set the article status to be Available
            article = frappe.get_doc('Article', self.article)
            article.status = 'Available'

    def validate_issue(self):
        article = frappe.get_doc('Article', self.article)
        # article cannot be issued if it is already issued
        if article.status == 'Issued':
            frappe.throw('Article is already issued by another member')

    def validate_return(self):
        article = frappe.get_doc('Article', self.article)
        # article cannot be returned if it is not issued first
        if article.status == 'Available':
            frappe.throw('Article cannot be returned without being issued first')

    def validate_membership(self):
        # check if a valid membership exist for this library member
        valid_membership = frappe.db.exists(
            'Library Membership',
                'library_member': self.library_member,
                'docstatus': 1,
                'from_date': ('<',,
                'to_date': ('>',,
        if not valid_membership:
            frappe.throw('The member does not have a valid membership')

There is a lot of code here but it should be self explanatory. There are inline code comments for more explanation.

Getting Error message on Submit
IS Indic Software
5 months ago

I am trying out the code as given but when I try to submit a transaction I get following error: Status cannot be "Issued". It should be one of "Issue", "Available"

How to resolve this?

Want to discuss?
Post it here, our mentors will help you out.