how to get a recently inserted value of a docType in ERPNext and update another DocType by that value using frappe.call and client script?
Assistant
To get the recently inserted value of a doctype in ERPNext and update another doctype using frappe.call and client script, you can follow these steps:
Write a client script in JavaScript and attach it to the appropriate doctype.
In the client script, use the frappe.call function to send a request to the server-side and get the recently inserted value.
In the server-side script, use the function to retrieve the recently inserted document.
User
why this code is not working in ERPNext?
frappe.ui.form.on('Attribute Value', {
refresh(frm) {
console.log("I'm Here")
frappe.call({
method: "frappe.db.get_value",
args: {
doctype: "Attribute Value",
filters: {
field: "value"
},
fieldname: "name"
},
callback: function(response) {
var recentlyInsertedValue = response.message.name;
console.log(recentlyInsertedValue)
}
});
}
})
Assistant
The code you provided seems to have a few issues that may be preventing it from working correctly. Here are the corrections:
Ensure that the script is attached to the appropriate DocType. In your case, it should be attached to the "Attribute Value" doctype.
Use frappe.db.get_list instead of frappe.db.get_value to retrieve multiple records. Since you mentioned getting the recently inserted value, it implies that you may have multiple records with the same value in the "value" field.
User
how to use frappe.db.get_value with frappe.call in client script?
Assistant
To use frappe.db.get_value with frappe.call in a client script, you can follow these steps:
Define a callback function to handle the response from the server and update the desired fields.
Use frappe.call to make the server-side request and pass the appropriate arguments.
In the server-side script, use frappe.db.get_value to retrieve the required value from the database.
Here's an example of how you can use frappe.db.get_value with frappe.call in a client script:
javascript
frappe.get_doc
Extract the required value from the document and send it back as a response.
In the client script, handle the response and use the retrieved value to update the desired doctype.
Here's an example implementation:
Client-side script:
code
frappe.ui.form.on("YourDocType", {
refresh: function(frm) {
if (!frm.doc.__islocal) {
frappe.call({
method: "yourapp.yourmodule.doctype1.get_recently_inserted_value",
args: {
// include any required arguments for the server-side method
},
callback: function(response) {
var recentlyInsertedValue = response.message;
// update another DocType using the retrieved value
frm.set_value("field_name", recentlyInsertedValue);
}
});
}
}
});
Server-side script (yourmodule/doctype1.py):
python
import frappe
@frappe.whitelist()
def get_recently_inserted_value():
# retrieve the recently inserted document
doc = frappe.get_doc("YourDocType", {"your_filter_field": "your_filter_value"})
# extract the required value from the document
recentlyInsertedValue = doc.your_value_field
return recentlyInsertedValue
Make sure to replace "YourDocType", "yourapp.yourmodule.doctype1.get_recently_inserted_value", "yourmodule/doctype1.py", "field_name", "your_filter_field", and "your_value_field" with the actual names relevant to your implementation.
Remember to reload the browser cache after making changes to the client script or server-side Python script.
Modify the filters parameter to match the desired condition for retrieving the recently inserted value. You can use filters like { "fieldname": "value", "value": "your_value" } to specify the conditions.
Add the function frm.reload_doc() after updating the field value to refresh the form and display the updated field value.
Make sure to replace "Attribute Value" and "field_name" with the actual names relevant to your implementation. Also, ensure that the filter conditions are set correctly in the filters parameter.
Reload the browser cache and try running the code again.
frappe.ui.form.on('YourDocType', {
refresh: function(frm) {
if (!frm.doc.__islocal) {
frappe.call({
method: "frappe.client.get_value",
args: {
doctype: "YourDocType",
filters: {
field: "value"
},
fieldname: "name"
},
callback: function(response) {
var recentlyInsertedValue = response.message.name;
// Perform any additional actions with the value
console.log(recentlyInsertedValue);
}
});
}
}
});
Make sure to replace "YourDocType", "field", and "value" with the actual names relevant to your implementation.
In the frappe.client.get_value method, doctype parameter should be set to the Doctype you want to query, filters is an object specifying the desired conditions, and fieldname is the field name you want to retrieve.
Ensure that the server-side API endpoint is accessible by the client-side script and returns the required data.
Remember to reload the browser cache after making changes to the client script.