(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.
I tried this code with the before_workflow_action hook, BUT the document status is showing as ‘Not Saved’ after execution of the workflow. The fields are populated, but as the document is not saved, when I refresh the page, the values go away.
Here even for a system user (with full access), the document is not saved after workflow action and values go away as soon as page is reloaded.
I don’t have access to a frappe instance at the moment to test, but it sounds like you’re just running into a race condition. I’m pretty sure that the set_value function is asynchronous, but in your code it doesn’t look like you’re waiting for it to return.
You’ll have to do some debugging with console.log statements. I still think it’s a race condition. It may not be possible to edit the document on the client side while it’s already in a save cycle. That makes sense, actually. The server is the source of truth, and client interaction is all via remote procedure calls. It would be challenging to allow client-side changes to data while a workflow transition is in process.
FWIW, this kind of logic probably should be executed on the server side, anyway. As you’re trying to do it, it would be pretty simple for any authorized user to set these values arbitrarily.
Great, glad it’s working. If you want to make it a separate step, that’s fine. Because this is a client-side operation, it will be possible for users with a small amount of javascript knowledge to falsify the user id, but that may not be an issue at your organization.
Hi @rps49 ,
Facing same problem
before_workflow_action(frm){
console.log(“Action”,frm.selected_workflow_action);
action = frm.selected_workflow_action
if(action===“Submit” && frm.doc.workflow_state==“Draft”){
let kra_kpi = frm.doc.kra_kpi_mapping
kra_kpi.forEach((ele)=>{
if(ele.self_ratings===0){
frappe.throw(“Please enter Self Ratings for KRA KPI”)
}
})
}
else if(action=="Approve" && frm.doc.workflow_state=="Pending with RO for Ratings"){
let kra_kpi = frm.doc.kra_kpi_mapping
kra_kpi.forEach((ele)=>{
if(ele.self_ratings===0){
frappe.throw("Please enter Self Ratings for KRA KPI")
}
})
let goal_kra = frm.doc.goal_and_kra
goal_kra.forEach((ele)=>{
if(ele.ro_ratings===0){
frappe.throw("Please enter RO Ratings for Goal and KRA")
}
})
}
else if(action=="Approve" && self.dro_required && frm.doc.workflow_state=="Pending with DRO for Ratings"){
let kra_kpi = frm.doc.kra_kpi_mapping
kra_kpi.forEach((ele)=>{
if(ele.dro_ratings===0){
frappe.throw("Please enter RO Ratings for KRA KPI")
}
})
let goal_kra = frm.doc.goal_and_kra
goal_kra.forEach((ele)=>{
if(ele.dro_ratings===0){
frappe.throw("Please enter DLO Ratings for Goal And KRA")
}
})
}
},
Working code but document gets freezed and need to refresh it.