Workflow Condition of Child Table

Good Day All

I’m trying to make a condition in one of the Workflow Actions, the condition is about the Payment Terms child table.
For Example, In the PO when an advanced payment is part of the process, the Payments table consists of more than 1 row.

How can I specify the number of rows in the child table in the condition code ?


P.S: the code in the image is incorrect.

Thank you

1 Like

do you have a working sample of the condition for the child table field?

I do not know whether len(doc.payment_schedule) > 1 works in condition or not, but you can try this workaround

doc.payment_schedule[0] != doc.payment_schedule[-1]

@szufisher got this, Thank you. will try it on the flowchart. But for my requirement, I will try it on the margin and discount field of the purchase invoice item :slight_smile: Atleast, I know that workflow works for child table fields

@szufisher thank you for the workaround code.
However, the code only works only if the Child Table has more that 1 row. Moreover, I tinkered with the code and this what I found:

1- This Code only works if the Child Table has only 1 row:
doc.payment_schedule[0] == doc.payment_schedule[-1]

2- This Code only works if the Child Table has only 2 rows:
doc.payment_schedule[0] != doc.payment_schedule[-1] and doc.payment_schedule[1] == doc.payment_schedule[-1]

3- This Code only works if the Child Table has only 3 rows:
doc.payment_schedule[1] != doc.payment_schedule[-1] and doc.payment_schedule[2] == doc.payment_schedule[-1]

4- This Code only works if the Child Table has only 4 rows:
doc.payment_schedule[2] != doc.payment_schedule[-1] and doc.payment_schedule[3] == doc.payment_schedule[-1]

and so on…

Important Note: I’ve yet to test the Code on more than 4 rows, or on a Child Table other than Payment Schedule table in Purchase Order.

Thanks again :slight_smile:

Hi I am trying to create a workflow condition for a child table and below is the code I used.

doc.items[0].discount_percentage < 4

I need to validate all discounts encoded on the item’s row of an invoice. However, I am getting the below error when I save the new transaction.

Can anyone check and help me if the condition code is correct?

(doc.items[0].discount_percentage or 0) < 4

I am tried using the above new solution however I am getting the below error. I am currently trying to search on how to change the use of the brackets

App Versions

{
	"erpnext": "13.23.3",
	"frappe": "13.23.0",
	"pdf_on_submit": "13.2"
}

Route

Form/Quotation/SAL-QTN-2022-00004

Trackeback

Traceback (most recent call last):
  File "apps/frappe/frappe/app.py", line 68, in application
    response = frappe.api.handle()
  File "apps/frappe/frappe/api.py", line 55, in handle
    return frappe.handler.handle()
  File "apps/frappe/frappe/handler.py", line 31, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 68, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1213, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/model/workflow.py", line 50, in get_transitions
    if not is_transition_condition_satisfied(transition, doc):
  File "apps/frappe/frappe/model/workflow.py", line 74, in is_transition_condition_satisfied
    return frappe.safe_eval(transition.condition, get_workflow_safe_globals(), dict(doc=doc.as_dict()))
  File "apps/frappe/frappe/__init__.py", line 1759, in safe_eval
    return eval(code, eval_globals, eval_locals)
  File "<string>", line 1, in <module>
TypeError: 'builtin_function_or_method' object is not subscriptable

Response Data

{
	"exception": "TypeError: 'builtin_function_or_method' object is not subscriptable"
}

looks like condition on the workflow does not work if you use the child table. I keep on getting a pop up message of “object is not subscriptable” :weary:

To factor item discounts in a workflow, we normally sum all/any line discounts, and then in the workflow we reference the line discount total field(this field is on the parent doctype).

Not sure it would work for your use case.

The script we use to sum the item discounts

frappe.ui.form.on('Quotation', {
	validate: function (frm) {
		// calculate total discount for each line item
		var total_discount = 0;
		$.each(frm.doc.items, function (i, d) {
			// calculate total discount            
			total_discount += flt(d.discount_amount);
		});
		frm.doc.line_discount_total = total_discount;
	}
})

Does this mean that scripts are not applicable to encode on the condition field of the workflow? is it correct?

So I need to create a custom script that validates if each row of items will have the discount_percentage < 4? how can I create a custom script that validates if the value of the percentage field was not manually encoded? if it was triggered from the pricing rules which the system checked after saving the document? :frowning:

“Does this mean that scripts are not applicable to encode on the condition field of the workflow? is it correct?” For fields on the parent doctype, you can use the condition section on the transition rules section for the workflow. Never tested for child doctype fields.

“So I need to create a custom script that validates if each row of items will have the discount_percentage < 4?” Yeah, or at least one way to approach it. We have a similar case, max discount is per salesperson, i.e. different sales people can give different allowable discounts, then anything above allowable discount, goes for approval. We sum the line discounts into a custom field on the parent doctype, and use this field as our condition field in the workflow.

“if it was triggered from the pricing rules which the system checked after saving the document?” Think you can reference doc.pricing_rules[0], in the workflow condition, since this would be probably one row, and you’re only checking for existence or a pricing rule or not.