Server script problem to create datetime

i create a custom field in lead doctype name = ‘custom_ref’ i want this field use for creating refrence no
I want that when new lead create from webform or indiamart etc . then in this field current date time show
i use server script for this then lead not create (web form not submit)

if not doc.custom_ref:
        doc.custom_ref = frappe.utils.now_datetime().strftime('%Y%m%d%H%M%S')

when i remove this line (.strftime(‘%Y%m%d%H%M%S’))

if not doc.custom_ref:
        doc.custom_ref = frappe.utils.now_datetime()

then lead create from webform or other source

i want date and time like this 20240716165543

@Mack can you tell me what is fieldtype of custom_ref

1 Like

the script worked properly but it did not work in the server script doctype because when you save the doc then faced the ImporrError.

Main issue is:

the field type = data

Now how can i use this to create value in custom_ref field in (‘%Y%m%d%H%M%S’) format
i want that value use as refrence no

Possible, but you have to develop it in the custom app using the doc_events.

how it develope
guide me

Please click on doc_events.

You have to learn to the hooks concept.

i have create a custom app which name is demo

shuold i put this code in hook.py

import frappe
from frappe import _

def before_insert(doc, method):
    if not doc.creation:
        doc.creation = frappe.utils.now_datetime().strftime('%Y%m%d%H%M%S')

or create a demo.py file and write this code in this and hook in hook.py file provide path

right :wink:

create a file and set the path in the hooks.py

1 Like

Ok thank u

More details for check this video.

my code is right or need to modify

code is right, but also check the video.