How to insert date in date field type from frappeclient?

I’m trying to insert date through frappeclient but getting Internal Server Error in response. What is the proper way to insert date in date field type?

Hi @Ramit_Panangat,

Please share your code.

Thanks.

@NCP

def lambda_handler(event, context):
    
    client = FrappeClient("https://asc360-prod.frappe.cloud")
    client.authenticate(environ.get("api"), environ.get("api_secret"))
    
    data = json.loads(event["body"])
    
    doc = client.insert({
        "doctype": "Operator Policy Issued",
        "operator": data["operator"],
        "name1": data["name1"],
        "gender": data["gender"],
        "nationality": data["nationality"],
        "start_date": datetime.strfdate('23-04-2022', '%d/%m/%Y')
        })

Hi @Ramit_Panangat,

can you try this like only ‘‘2022-04-23’’ without datetime.strfdate
Thanks.

@NCP
Tried that too…but didn’t worked

hi @Ramit_Panangat

datetime.strptime('23-04-2022', '%d/%m/%Y')

please try this.

Thanks.

@NCP
I just tried and still not working

Last option like frappe.utils.data.get_datetime()

Thanks.

Hi @Ramit_Panangat,

What error show in insert time?

@NCP
I’m trying to run frappeclient in AWS lambda. Can’t import frappe there.
This is the error I get while using datetime module.

Test Event Name
event

Response
{
  "errorMessage": "'body'",
  "errorType": "KeyError",
  "requestId": "cc50821d-d0b8-45e2-a44b-2c6acdad4c13",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 11, in lambda_handler\n    data = json.loads(event[\"body\"])\n"
  ]
}

Function Logs
START RequestId: cc50821d-d0b8-45e2-a44b-2c6acdad4c13 Version: $LATEST
[ERROR] KeyError: 'body'
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 11, in lambda_handler
    data = json.loads(event["body"])END RequestId: cc50821d-d0b8-45e2-a44b-2c6acdad4c13
REPORT RequestId: cc50821d-d0b8-45e2-a44b-2c6acdad4c13	Duration: 18.78 ms	Billed Duration: 19 ms	Memory Size: 128 MB	Max Memory Used: 49 MB	Init Duration: 319.54 ms

Request ID
cc50821d-d0b8-45e2-a44b-2c6acdad4c13

Hi @Ramit_Panangat,

If you have not checked then check it, please.
https://frappeframework.com/docs/v13/user/en/api/utils#add_to_date

Maybe it helps you.

Thanks.

@NCP
Actually problem with frappe.utils is that I can’t import that in AWS lambda.

Hi @Ramit_Panangat,

Sorry for disturbing you.
Please again try this if you have not tried it.

datetime.datetime.strptime('23-04-2022', '%d/%m/%Y')

Otherwise, change the format and then checks it.

Thank You!

@NCP
No no. You are helping me.
I already tried datetime.datetime.strptime('23-04-2022', '%d/%m/%Y') but not working.

Hi @Ramit_Panangat,

date = "2022-04-23"
frappe.utils.datetime.datetime.strptime(date, "%d-%m-%Y")

OR

frappe.utils.datetime.datetime.strptime('23-04-2022', "%d-%m-%Y")

Please set your according to.

Thanks.