Call Server Script from another Server Script?

I’ve defined a new API-type Server Script used to update custom fields in the Issue doctype. I’d like to be able to call this script from various DocType-event-type Server Scripts. Is this possible?

I’ve tried playing around with the run_script method, but I can’t seem to pass parameters. Is there another way?

Any ideas or suggestions out there?

use frappe.flags. The safe_exec environment maintains its own set of flags.

2 Likes

Thanks Rushabh! That makes a lot of sense. I saw reference to flags in the documentation, but I’m realizing now that I misunderstood how they worked. This works perfectly.

Actually, I’m still failing to get this to work. I’m able to use flags to return values, but not to send values.

script_1 (doc event):

frappe.flags.my_key = "test flag"
run_script('script_2')

script_2 (api):

frappe.msgprint(frappe.flags.my_key)

…isn’t working for me. Am I misunderstanding?

Anyone know if this is possible?

I’m still trying to understand if I’m just not doing something correctly or if this functionality is not available.

Is it possible to send parameters or flags from one custom script to another?

Hi @peterg

Trust you’re doing great. Were you able to work this out ? If so, pls share how

Also, I’d really appreciate if you could share any sample(s) of working API Server Scripts

Thanks

Hi there,
This wasn’t possible last year, but I believe it might be now. As of the latest v13 release, frappe.call has been added the Script API safe execution roster. I haven’t had a chance to play with it yet, but I suspect it should work.

1 Like

Great! Any sample API script you can share ?

Nope, like I said, I haven’t had a chance to play with it yet.

Oh, sorry… thot you were referring specifically to calling the API script from another server script

Many thanks

Oh, yeah, I was talking about that. For regular api server scripts, we have dozens going on our company’s server. Most are pretty specialized to internal processes and integrations, so I’m not sure much would be useful to share, but if you have any questions feel free to make a thread and tag me.

1 Like

Script 1:
frappe.flags.my_key = ‘my value’
Script 2:
my_key = run_script(‘script_1’).get(‘my_key’)

found this in here

1 Like

unfortunately its saying The resource you are looking for is not available

you can try like this

Any idea on how to get the response back.
If we use the same api server script in client side we are able to get the response

but if we call the api script from another server script the script is getting executed. But not sure on how to get the response back.

Any Idea?

to get the response back set the below
frappe.flags.message = l_response inside the server script api

The flow goes like this

define a server script api say ‘my_api_server_script’

let’s assume the above script takes two arguments arg 1 and arg2

to fetch the args inside the api script

arg1 = frappe.form_dict.get('arg1') 
arg1 = frappe.form_dict.get('arg2')

perform rest of code
finally send the result in frappe.flags

#to get the response from the server side
frappe.flags.message = result

#if you want to use the script from the client side
frappe.response["message"] = result

now you can call the api script from another server script like below

response = frappe.call("my_api_server_script", arg1 = value1, arg2 = value2)
result = response["message"]

Hope this helps