How to update field asset's field value when assets is under workflow process

Is there anyone here who can help me? I need to update the asset’s “user_type” field value, when “Asset Movement” doc.docstatus is 0 and workflow_state is draft.

Thanks and Regards
Shubham

@Shubham_Prabhat1 can’t find the field you are talking about . and did not understand the event itself . can you re-explain?

I have manually added a field that is,

“user_type”

and condition is

when I do create a Asset Movement

then all selected asset’s “user_type” field value should be change to “User”
when doc.workflow_state is “draft” and doc.docstatus is 0

@bahaou

Thank you

import frappe

def execute(doc, method):
# Check if the document’s workflow state is “Draft” and docstatus is 0
if doc.workflow_state == “Draft” and doc.docstatus == 0:
# Update the “user_type” field in the document to “User”
doc.user_type = “User”
doc.save()

    # Display a success message
    frappe.msgprint("The 'user_type' field has been updated to 'User'.", alert=True)

@bahaou

it’s not working

@bahaou Please help me, it’s very urgent

@Shubham_Prabhat1 idk where this code is written but it should be a server script like this:

test is my custom field .
you can’t change the status to reserved so you have to find another way.

for a in doc.assets:
asset=frappe.get_doc(“Asset”,a.asset)
#my condition here
frappe.db.set_value(“Asset”,a.asset,“user_type”,“User”)
frappe.msgprint(“test changed”, alert=True)

builtins.NameError: name ‘doc’ is not defined
@bahaou

@Shubham_Prabhat1 where are you writing your code ? share a screenshot ?

VS Code

@bahaou

@Shubham_Prabhat1 you have to use a function called before submit. also , do not update in the core codes . don’t do it . use custom app or server script like I did. in the ui search server script and do what I did.

am just testing here, if it will work, will do use for custom app

@bahaou

@Shubham_Prabhat1 if you want to do so add the code under the function on_submit or add before_insert parallel to on_submit.

ok, am checking

@bahaou

image

didn’t work
@bahaou

The issue at hand is that once an Asset Movement document is created with the purpose set to ‘Issue’ and that document is in a draft state, the same assets should not be shown in the Assets table of the Asset Movement document for selecting assets. I am currently working on a solution for this issue.

Thank and Regards

Shubham

@Shubham_Prabhat1 it’s before_insert and should be under the class . and are you sure the doctype ‘asset movement’ has the field workflow ? it’s also pointless since you just created it

ImportError: import not found

@bahaou

@Shubham_Prabhat1 hello again. did you solved it ? you don’t have to write import and the def in server scripts . just start with the code in line 4

Thank you so much,

it’s working properly now