Hi,
From a form ("My Custom Doctype"
) I want to get the field value ("field_one"
), sending them to server side which will then send them via requests to a remote site ("https://mydomain.com"
) to add the document in a doctype there ("Target Doctype"
).
In the js I use button to trigger the action and use frm.doc to get local values:
frm.add_custom_button(__("Trigger"),
function() {
frappe.call({
method: "customapp.api.my_trigger",
args: {
"doctype": "My Custom Doctype",
"name": frm.doc.title_field,
"fields": {
"field_one": "Name Of Field",
}
},
callback: function(r) {
if (!r.exc) {
frappe.show_alert("action done")
}
}
})
.fail(fail => console.log("failure", fail))
.done(success => console.log("success", success.message));
console.log(r.url);
});
and in server side py:
def my_trigger(doctype, name=None, fields=None):
url = "https://mydomain.com/api/resource/Target Doctype"
headers = {
'Authorization': 'token key:secret',
'Accept': 'application/json',
'Content-Type': 'application/json',
}
data = json.dumps({
"field_one_remote": fields.field_one,
})
r = requests.post(url, headers=headers, data=data)
But I get this error in the browser console and no document is created on the remote site:
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR) #pointing to the url of server side method
#also see that
responseText: "{}"
But if I use explicit value on the requests’ data the document is created on the remote site with field_one_remote
filled with value from requests data (instead of source form → from js):
data = json.dumps({
"field_one_remote": "Value One",
})
So I don’t think it is the issue with my requests code, but on the variable and/or passing data from form → js → py.
Can anyone please tell me how to do this?
Thank you.