Server script (API) not working to store client script data into database

Hi all,

Problem Statement: I want to store location coordinates of user closing a task in the ToDo doctype. Client script I have created for the same is working fine and I can see the results in the console. But corresponding server script is not working.

Server Script (Type: API)
def fetch_location(todo_id, latitude, longitude):
# Force an error for debugging
frappe.throw(“Intentional Error: This is for debugging purposes”)

# Log the received data for debugging purposes
frappe.log_error(f"Received Latitude: {latitude}, Longitude: {longitude} for ToDo: {todo_id}", "Location Update")

# Fetch the ToDo document by its name (id)
todo = frappe.get_doc("ToDo", todo_id)

# Update the latitude and longitude fields
todo.custom_latitude = latitude
todo.custom_longitude = longitude

# Save the updated document
todo.save()

# Return a success message
return {"message": "Location updated successfully", "latitude": latitude, "longitude": longitude}

Even a simple test script such as following is also throwing undefined response in console:
def test_script():
return {“message”: “Test successful”}

What I have already done:

Please guide if this kind of customization via server script is feasible without doing any code setup and just by updating client/server scripts via UI. If yes, where am I going wrong?

Sometimes a larger level script doesn’t work in the server script doctype. You have to write the server script in your custom app.

Is there a whitelist method?

You defined the function but forgot to execute it. Add the following to the bottom after your function definition then try again.

todo_id = frappe.form_dict.get('todo_id')
latitude = frappe.form_dict.get('latitude')
longitude = frappe.form_dict.get('longitude')
fetch_location(todo_id, latitude, longitude)

Also, since this is an API Server Script, your response should be

frappe.response['message'] = 'Success'

Try the following

  • Server Script
  • Type: API
  • Method: fetch_location

Script

todo_id = frappe.form_dict.get('todo_id')
latitude = frappe.form_dict.get('latitude')
longitude = frappe.form_dict.get('longitude')

todo = frappe.get_doc("ToDo", todo_id)
if todo:
   todo.custom_latitude = latitude
   todo.custom_longitude = longitude
   todo.save(ignore_permissions=True)
   frappe.db.commit()
   frappe.response['message'] = 'Success'
1 Like

@Yamen_Zakhour It worked perfectly. Thanks a ton. This was my first attempt at using scripts and also taking the help from the community. Amazed and excited to see the responsiveness here.

1 Like

No whitelist method.