Issue with script based on multiple criteria

Hi All,
Hoping to get some assistance - any would be greatly appreciated!

I have the below script which does not seem to work as expected. I have multiple workflow states (Stage One, Stage Two, Stage Three etc) and I need to ensure a specific checkbox is selected before the document is saved and allowed to move to the next state. I have tried a few variations to no avail.

The below (tested in Chrome) produces the following:

  1. A new document is created OK.
  2. If no checkbox is ticked, and I select to save, it produces the Stage Two msgprint?
  3. I copy each line of criteria into Chrome console - all returns as expected where Stage One should have been invoked…

frappe.ui.form.on("Stages", "before_save", function(frm) {
if (!cur_frm.doc.__islocal && cur_frm.doc.workflow_state && cur_frm.doc.workflow_state.indexOf("Stage One") == 0 && !cur_frm.doc.stage_one_check) {	
		msgprint('Please confirm Stage One is completed by selecting the completion checkbox');
		validated = false;
	}
if (!cur_frm.doc.__islocal && cur_frm.doc.workflow_state && cur_frm.doc.workflow_state.indexOf("Stage Two") == 0 && !cur_frm.doc.stage_two_check) {	
		msgprint('Please confirm Stage Two is completed by selecting the completion checkbox');
		validated = false;
	}});

Why not use validate to check if selected?

Hi,
Just tried per below - same results…

frappe.ui.form.on("Stages", "validate", function(frm)

Try like this

frappe.ui.form.on(“Stages”, {
validate: function(frm,cdn,cdt){

  },

})

Unfortunately same thing. See below. Copying the if statements into console still produces the correct results…weird.

Any other ideas?

Thanks

frappe.ui.form.on('Stages', {
validate: function(frm,cdn,cdt) {
if (!cur_frm.doc.stage_one_check && !cur_frm.doc.__islocal && cur_frm.doc.workflow_state.indexOf("Stage One") == 0 ) {	
		frappe.msgprint('Please confirm Stage One is completed by selecting the completion checkbox');
		frappe.validated = false;
	}
if (!cur_frm.doc.stage_two_check && !cur_frm.doc.__islocal && cur_frm.doc.workflow_state.indexOf("Stage Two") == 0 ) {	
		frappe.msgprint('Please confirm Stage Two is completed by selecting the completion checkbox');
		frappe.validated = false;
	}

}
});

So further testing revealed that it is to do with the statement for cur_frm.doc.workflow_state / cur_frm.doc.workflow_state.indexOf, as removing this everything was working as expected.

I then proceeded to test changing the workflow ‘Next State’ of Stage One (shown below) from Stage Two to Stage Three. Once I did this, it started to work as expected.

So this leads to the question why cur_frm.doc.workflow_state is picking up the next workflow state when selecting to go to that state? I have tested with before_save and validate triggers, both being the same

Any comments?

I ended up taking a different approach as an acceptable workaround - below is now in place and I removed the transitions from the active workflow.

frappe.ui.form.on('Stages', {
stage_one_check: function(frm) {
if (cur_frm.doc.stage_one_check==1) {
	cur_frm.set_df_property("stage_one_check", "read_only", cur_frm.doc.__islocal ? 0 : 1);
	cur_frm.set_value('workflow_state', 'Stage Two');
	cur_frm.timeline.insert_comment("Workflow", "Stage One completed")
	cur_frm.save();
	}

}	
});
frappe.ui.form.on('Stages',	{
stage_two_check: function(frm) {
if (cur_frm.doc.stage_two_check==1) {
	cur_frm.set_df_property("stage_two_check", "read_only", cur_frm.doc.__islocal ? 0 : 1);
	cur_frm.set_value('workflow_state', 'Stage Three');
	cur_frm.timeline.insert_comment("Workflow", "Stage Two completed")
	cur_frm.save();
	}
}
});

Question still stands above though as to why the next workflow state was affecting the script…