When Leave Workflow state rejected, mark reason for rejection

I am playing with Workflow and configured Leave Application as erpnext doc elaborated. Workflow is working as expected. But, to enrich the experience of Leave Workflow, I need to notify Employee, while rejecting their Leave Application and have to leave some remarks there. So, Employee at least get to know, why my Leave Application rejected.

I tried but not get succeed. After I get two community written script on the forum, I tried this custom script written by @max_morais_dmm. I replace {doctype} with {Leave Application} and used it.

No diolog box opened, with no console log. Can anyone help with this concern?

@asharamseervi remove the brackets, it should be Leave Application, instead of {Leave Application}

1 Like

Thank you so much @max_morais_dmm !!

Yes, earlier I have {Leave Application} instead of “Leave Application”. After your advice, I just removed brackets and tried in all possible way, clearing cache, bench update, bench migrate. But, still there is something we missing there.

At present, there is no console logs, no typo, still no dialog opening. Below is in Custom Script Section, by selecting document “Leave Application”. Do we need to update/upgrade script?

frappe.ui.form.on("Leave Application", "after_save", function(frm, cdt, cdn){
		if (frm.doc.workflow_state && frm.doc.workflow_state.indexOf("Rejected") > 0){
			frappe.prompt([
				{
					fieldtype: 'Small Text',
					reqd: true,
					fieldname: 'reason'
				}],
				function(args){
					validated = true;
					frappe.call({
						method: 'frappe.core.doctype.communication.email.make',
						args: {
							doctype: frm.doctype,
							name: frm.docname,
							subject: format(__('Reason for {0}'), [frm.doc.workflow_state]),
							content: args.reason,
							send_mail: false,
							send_me_a_copy: false,
							communication_medium: 'Other',
							sent_or_received: 'Sent'
						},
						callback: function(res){
							if (res && !res.exc){
								frappe.call({
									method: 'frappe.client.set_value',
									args: {
										doctype: frm.doctype,
										name: frm.docname,
										fieldname: 'rejection_reason',
										value: frm.doc.rejection_reason ?
											[frm.doc.rejection_reason, frm.doc.workflow_state].join('\n') : frm.doc.workflow_state
									},
									callback: function(res){
										if (res && !res.exc){
											frm.reload_doc();
										}
									}
								});
							}
						}
					});
				},
				__('Reason for ') + frm.doc.workflow_state,
				__('End as Rejected')
			)
		}
	});

@asharamseervi use before_save or onsave events

I replaced after_save with before_save and tried, also tried with onsave. But, still no help. Do I need to do some extra stuff?? Will you help me to understand it deep?

@asharamseervi after some investigation, I conclused that, before the intruction of async methods in JavaScript in frappe, is more hard, to take over the control of features based on workflow, what I’ll suggest for you do is:

  • Use the validate method:
  • Use a hidden checkbox or a variable, that you can control that the user wont filled the info that do you want
  • On validate reject the validation, passing frappe.validated = false, until the user wont fill up the information on your dialog.
  • On your dialog, after get the data, update the status of your variable of control, and ensure that the validation rejection will pass!

With these steps in mind, do you will be able to achieve this.

2 Likes

Thanks a lot @max_morais_dmm for helping me to understand. I’ll try to achieve it, will let you know here. :slight_smile:

Hi @max_morais_dmm - I am trying to do the same thing again! Can you help me with the custom code? I am not a developer! Thank yoU!