Workflow - Dialog on Action

Hi all,
I created workflow for Material Request.

In that if Purchase manager reject the material request, I need dialog over there for capture reason of rejection. Captured reason will be set to custom field.

I tryed this code but not working for me?!
use

frappe.ui.form.on("Material Request", "workflow_state", function(frm, cdt, cdn){
  if (frm.doc.worflow_state ==== "Reject"){
    // do something
  }
});

The problem with this code is the event is not trigger if I make any action!
It is only triggered onload.

@Mohammed_Redha it dont work, because the workflow handle the save event after the field update!
Usually i use the after_save event, to ensure that the user will post a comment

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')
			)
		}
	}
1 Like

thanks @max_morais_dmm for your reply , Could you tell me how I can use after_save event?
Is it client side or server side?

@Mohammed_Redha it’s in client side, is a event that occurs before a document is saved, in my case I should enforce a comment by the user, is because it, that I need save first and update after, if you try to insert a comment by js, and invoke te save event of the doc, frappe tells, that has a conflict in the doc.

could you give example because I don’t find after_save event?!

@Mohammed_Redha



frappe.ui.form.on('Lead', {
	refresh: function(frm, cdt, cdn){
		if (!frm.doc.__islocal && frm.doc.customer_type == "Individual"
				&& frm.doc.workflow_state && frm.doc.workflow_state.indexOf('Evaluated as Approved')!=0){
			frm.page.inner_toolbar.attr("style", "display: none");
		}
		frm.toggle_enable('status', false);
		frm.toggle_enable('customer_type', frm.doc.customer_type ? false : true);
		frm.toggle_enable('tax_id', frm.doc.tax_id ? false : true);
		if (frm.doc.customer_type == "Company"){
			frm.toggle_reqd('company_name', true);
		}

	},
	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')
			)
		}
	},
	customer_type: function(frm, cdt, cdn){
		frm.toggle_enable('company_name', frm.doc.customer_type == "Company");
		if (!frm.doc.customer_type) return;
	}
});

3 Likes

It is working, thanks

Hello @max_morais_dmm and @Mohammed_Redha
There is not events that are related to workflow (same as refresh, onload, save, … etc that we use it in the normal scripts) to be used with the workflow which helps to check on certain values and allow or disallow the process to complete?

Regards
Bilal

@bghayad the code and logic in after_save event

Hii @max_morais_dmm
how I can use before_submit event?

I tried with below code but its not working:

frappe.ui.form.on('Purchase Order', {
    before_submit: function(frm, cdt, cdn){
        console.log("Approved case-----------before_submit---------------"+ 
        cur_frm.doc.workflow_state);
    }
});
2 Likes

Hi @max_morais_dmm

Been trying to find some way around this issue. Using the “after_save” trigger works but it’s not effective because the document state remains as “Not Saved” and the calculations done on the form are lost if it is refreshed at this point

Could you (or anyone else) suggest an alternative solution pls?

Kind regards,

Any suggestions pls?

From V11, it appears this only works upon initial Saving of document. The actual transition of workflow states doesn’t work!

You can use if condition , docstatus==0

i need to send rejection reason to mail how it’s possible can you guide me…?

Hello Guys ,

I have faced issue. Dialog is not working when click on “Reject” button in workflow action dropdown. Still open refresh but i want open dialog in “Reject” button in workflow action.

frappe.ui.form.on(Doctype ::::', {
refresh: function (frm) {
if (frm.doc.workflow_state && cur_frm.doc.workflow_state == “Rejected”){
frappe.prompt([
{‘fieldname’: ‘rejection_reason’, ‘fieldtype’: ‘Small Text’, ‘label’: ‘Reason’, ‘reqd’: 1}
],
function(values){
show_alert(values, 5);
console.log(“::::::::::::::”,values.rejection_reason);
frm.set_value(“rejection_reason”, values.rejection_reason);
},
‘Reason for Rejection’,
‘Submit’
);
}

}

});

Anyone help me ?

Thanks