I am trying to implement an after workflow action in client-side JS. What I want to achieve is to have the form automatically record the user and date where that triggered the workflow action. Here is my code below;
The values are getting set but I either get an error that the document has been refreshed before the values get the chance to save or the error does not show up but the document fails to save the set values after the workflow action.
Can someone please correct the code for me? Thank you
I have another way to do it, using a check field to trigger the username and date to populate before the workflow. However, there has to be a before_workflow_action to make sure that the approvers have checked the field before approving but I have not been able to figure this part in bold and italics.
frappe.ui.form.on(‘Expense Claim’, {
before_workflow_action: (frm) => {
if(frm.doc.workflow_state === ‘Advance Verified By Finance’){ if( the selected workflow action is ‘Approve’ and checkfield ‘is not checked’ )
frappe.throw(“Please check the approval checkfield before approving the payment request”);
frappe.validated = false;
}}});
I don’t have this in front of me to test, but I wouldn’t have thought you’d want a frm.refresh() event before the save. My guess would be that the doc refresh is still in progress while the save gets called. If you remove the refresh, does it work?
Thanks for the reply, but it shows this error if I remove frm.refresh(); If I leave it there, sometimes it shows, other times it executes the workflow but fails to save the fields
Hmm…what version are you using, and do you have any other custom event triggers for this doctype? I just tested on my staging site and it worked fine (without the refresh instruction)
Yes, I have three of these fields as there are three approvers. After each approval, the fields are to be populated and move to the next state where the next approver’s action will populate other fields, as shown in my first post.
I’m not totally following this part. Are you saying that the fields being populated depend on the current workflow state?
Regardless, I’m not encountering the problem you are with the code above. If you have more code active than what you’ve shown here, you might consider commenting it all out, starting with this, and adding the rest in one piece at a time. If you’ve got only and exactly the code listed above under after_workflow_action, I’m really not sure why it works fine for you but not for me.
The fields, “approved_by_f” and “approval_date_f” actually get populated, but the form is not saved before the workflow is executed. Take a look at the screenshot below:
You wouldn’t expect it to be saved before the workflow is executed. This trigger is getting called after it finishes being executed. That’s why you need to call frm.save() afterwards, to commit the changes made after the workflow transition.
…works absolutely fine for me on v13.11. I’m very surprised it’s not working for you. There must be something else going on interfering, creating a race condition of some sort.
To see which action has been called, you can check the variable frm.selected_workflow_action. This variable is only available during before_workflow_action event handlers, I believe.
(Friendly request: your code will be a lot easier to read if you use code fences (```) in the line above and the line below your code to preserve formatting)
Can you this with console.log(frm.selected_workflow_action)? What value do you see in output?
Sort of. Are you unfamiliar with using console.log to debug? You need to put this into your actual before_workflow_action event trigger, where the variable frm is defined. That will allow you to see what the value of selected_workflow_action is when it runs and, consequently, why your error message isn’t triggering.
You should see the value of frm.selected_workflow_action output to the console before the workflow action triggers. (I’ve also tidied your code up a bit, removing the nested if statements and the unindended bracket closures.)
Hey @Syd and @peterg , Did this issue “The form changes to “Not Saved” so that once the I refresh, I lose the contents of the fields.” get solved.
Cause I am facing the same situation. The fields get populated with username and time but the form changes to ‘Not Saved’ and hence the data is not saved in the database.
Comment: User and time being fetched perfectly. BUT, as workflow is executed, the normal user doesn’t have permission to edit document anymore, hence the values are not getting saved. Also not getting saved by the Manager in the final submit state.
I’m not fully understanding the differences between these three bits of code, but if the user doesn’t have permission to edit the document after the workflow change, you’re not going to be able to set those values after the workflow action has been completed. You might try using the before_workflow_action hook, or you might have to use a general save hook handler of some sort.