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.
Hi @rps49,
Managed to do it on validate event. Thanks for your time and reply.
if (self.workflow_state==“Approved by HR” or self.workflow_state==“Approved by RO” or self.workflow_state==“Pending with DRO for Ratings” or self.workflow_state==“Finalised”) and frappe.db.get_value(“PMS Appraisal”,self.name,“workflow_state”)==“Pending with RO for Ratings”:
for i in self.kra_kpi_mapping:
if i.ro_ratings==0:
frappe.throw(“Please Enter RO Ratings for KRA KPI”)
for i in self.goal_and_kra:
if i.ro_ratings==0:
frappe.throw(“Please Enter RO Ratings for Goal and KRA”)
if not self.ro_behavioural_ratings:
frappe.throw(“Please Submit Behavioural Feedback”)