Set username on change of Workflow status

Hello,
I have a Doctype with Workflow, two levels of approval; Team Lead Approval and Manager’s Approval.


I am trying to capture which user Created the Doctype, let’s say Quotation and which Manager Approved the Quotation.
I am using the Update Value function in Workflow to update a custom field with the Workflow Status since I don’t want to override the default Status (Draft, Open,etc)
[Workflow screenshots]


Since I want to capture the Current user’s name on change of Workflow State, I think the username will have to be set on certain event. What event should I use to set the username? Also I am assuming this will have to be done via Server side custom script.

What is the best way to do it @tmatteson ? Thanks for your help.

Hi,
You can set current username in validate of .py code:

def validate(doc, method):
    if doc.workflow_state == 'Created':
        doc.quote_prepared_by = frappe.session.user

@asd’s solution is likely going to be the most simple, but it isn’t the only path. And to implement it as a customization (since this is a standard ERPNext doctype), you would want to do it with a document hook.

frappe.session.user is an available javascript object and would likely fit your needs. If you are running on ERPNext.com then this is easier to implement as a custom script.
I don’t think your setting of all the ‘Update Field’ and ‘Update Value’ columns are having the intended effect. I rarely use these fields.

frappe.ui.form.on("Quotation", {
  validate: (frm) => {
    if(frm.doc.workflow_state === 'Created'){
      frm.doc.quote_prepared_by = frappe.session.user
    } else if(frm.doc.workflow_state === 'Approved'){
      frm.doc.quote_reviewed_by = frappe.session.user
    }
  }
})

If you need to override the workflow action logic entirely, here’s a pattern for that, but this approach is surgery, not applying hand lotion; I would not recommend it for any but the most extreme cases.

2 Likes

Thanks @tmatteson and @asd for your help.
However, we have tried these solutions and the problem is validate event is not called when the workflow state changes since you are not saving the document per se.
If someone can point towards which event is triggered on change of workflow state that will help.

Thanks

Change your first State to Draft, don’t use ‘Created’

Well the state is “0” (draft) for the Created state.
You think Created is conflicting with some existing code/feature?

Hi @Manan_Shah, I used one trick to go through exact same situation

  1. Create different roles for both users (the one that approves and other that will review) in your case u already have it “purchase User & Purchase manager, so no need to create”
  2. Assign to each of them respectively

What u need now, is to create a custom script as showed here:

But instead of using the validate function u will use the “onload” and u will check if the user have that role or not to fill the field.

note: to permanently fill the field the user must save the doc

frappe.ui.form.on("Quotation", {
  onload: (frm) => {
    if(frm.doc.workflow_state === 'Created' && frappe.user.has_role(['Purchase User'])){
      frm.doc.quote_prepared_by = frappe.session.user

    } 
      else if(frm.doc.workflow_state === 'Approved' && frappe.user.has_role(['Purchase Manager'])){
      frm.doc.quote_reviewed_by = frappe.session.user
    }
  }
});

Hope it helps.

Dércio Bobo

2 Likes

Only because I made this mistake today (and well, many other days) you need to check if the quote_prepared_by already has a value. Using onload or validate without checking will overwrite and when you go to view the submitted document again you’ll have a “cannot change ‘quote_prepared_by’ after submission” problem.

Hi Mannan,

Is it possible to show the 1 status (WF status & default status) in 1 display?

I used WF for Sales Invoice, the WF is working well, but i struggle to show both status in Sales Invoice List table…

Thanks !

Go to Customize Form and select “In List View” for the custom field you want to show on the List view.
You may have to remove unwanted fields if you have too many fields on List View

Hi Manan,

Yesterday i already sort out the solution, i went to Custom Field - WF state field and choose In List View…

However thanks for your answer, appreciate it!

Thank you for the help ! It retrieves the user but not saving it! Can you help in this regard?

Its not saving the user name! please help, this help is badly needed!

Check this out

@Manan_Shah Did this solution work for you?
Or you used some other method to achieve the goal of adding quotation created by and approved by usernames in the doc?