On Invoice there is a child table called : Services Support List
Services Support list has 7 fields:
Select Price type ( A select field with Strings as options e.g “Price Limit
National Non Remote MMM 1 to 5”)
Enter Quantity (Float type)
Total (Currency Type)
Select Service (Link type field - Linked to a doctype “Service” with Price
Types)
Price Limit National Non Remote MMM 1 to 5 ( A currency field fetched from Doctype “Service” - Fetches Perfectly fine)
Price Limit National Non Remote MMM 1 to 6 (similar to field 5 - a currency field fetched from doctype “Service”)
There are multiple similar fields like 5 and 6 of currency types which are being fetched from the doctype “Service”. They all fetch perfectly fine.
Now what I wanted to do was to add a script, which would choose a rate based on what the user selects Price type i.e (a basic algo - not the actual code)
if (doc.price_type == “Price Limit National Non Remote MMM 1 to 5”){
doc.rate = doc.price_limit_nt_satas_wa_mmm_1_to_5;
}
What this would do is make rate = price_limit_nt_satas_wa_mmm_1_to_5 (the fetched price).
However my code does not work, Any help is appreciated. Here is my code:
frappe.ui.form.on("Services Support List", {
rate:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
var total = 0;
frappe.model.set_value(d.doctype, d.name, "total_delivered", d.quantity * d.rate);
frm.doc.services_support_list.forEach(function(d) {
if (d.price_limit_type == "Price Limit NT SATAS WA MMM 1 to 5"){
d.rate = d.price_limit_nt_satas_wa_mmm_1_to_5;
}
else if (d.price_limit_type == "Price Limit ACT NSWQLD VIC MMM 1 to 5"){
d.rate = d.price_limit_act_nswqld_vic_mmm_1_to_5;
}
else if (d.price_limit_type == "Price Limit National Non Remote MMM 1 to 5"){
d.rate = d.price_limit_national_non_remote_mmm_1_to_5;
}
else if (d.price_limit_type == "Price Limit National Remote MMM 6"){
d.rate = d.price_limit_national_remote_mmm_6;
}
else{
d.rate = d.price_limit_national_very_remote_mmm_7;}
total += d.total_delivered;
});
frm.set_value('grand_total', total);
rate : rate field in child table
total_delivered: field in child table (found by rate * quantity)
grand_total: field in parent table which is the sum of all total_delivered from child table
The problem lies elsewhere. I made a simple doctype with two fields:
select_type (a select type field with 3 options (A,B,C)
selected_type (a data field to show select_type in)
I wrote this custom script:
frappe.ui.form.on("Script Test Doctype", {
refresh(frm) {
if (doc.select_type == "A" ){
doc.selected_type = "Type A selected";
}
else if (doc.select_type == "B" ){
doc.selected_type = "Type B selected";
}
else if (doc.select_type == "C" ){
doc.selected_type = "Type C selected";
}
else{
doc.selected_type = "No type selected";
}
}
});
This doesn’t work either. I’m guessing the way it’s validating the IF condition is flawed. It does not validate the IF condition. Here’s how I know this, I wrote the following code to check if the IF condition validates i.e : if (doc.select ==“A”)
code:
frappe.ui.form.on("Script Test Doctype", {
refresh(frm) {
if (doc.select_type == "A" ){
frappe.msgprint(__("Field A Selected"));
}
else if (doc.select_type == "B" ){
frappe.msgprint(__("Field B Selected"));
}
else if (doc.select_type == "C" ){
frappe.msgprint(__("Field C Selected"));
}
else{
frappe.msgprint(__("No Field Selected"));
}
}
});
This does not work either, implying that the fault lies in the IF condition validation . Kinda wanna bash my laptop in the wall
I read the whole thread. According to that logic I can define the IF conditions in the parent doctype and fetch into the other doctype from there. Here is what I did:
I created a doctype with the name “Delete” with the following fields:
select_type (select field with 3 options (A,B,C)
selected_type (data field to set value to - made as read only)
chosen1 (a data field)
chosen2 (another data field)
chosen3 (another data field)
nonechosen (another data field)
This is the code I wrote for the doctype :
frappe.ui.form.on("Delete", {
refresh(frm) {
if (frm.doc.select_type == "A" ){
cur_frm.set_value("selected_type", chosen1);
}
else if (frm.doc.select_type == "B" ){
cur_frm.set_value("selected_type", chosen2);
}
else if (frm.doc.select_type == "C" ){
cur_frm.set_value("selected_type", chosen3);
}
else{
cur_frm.set_value("selected_type", nonechosen);
}
}
});
Now even after creating a document and populating the fields, it was not setting the value of selected_type to any of the fields. The issue lies in the condition validation as i’ve shown in examples before this.
A workaround i’ve figured is to use Switches instead of IF conditions. The switches work for a parent doctype i.e:
frappe.ui.form.on("Delete", {
refresh(frm) {
switch(frm.doc.select_type) {
case 'A':
frm.set_value('selected_type', frm.doc.chosen1);
break;
case 'B':
frm.set_value('selected_type', frm.doc.chosen2);
break;
case 'C':
frm.set_value('selected_type', frm.doc.chosen3);
}
}
});
Doctype is called delete
select_type is the select field (options: A,B,C)
chosen1, chosen2, chosen3 are three currency fields (I populated the data)
selected_type is the field the values are set based on select_type.
This works for a parent doctype, however as soon as create a child table(which links to Doctype “Delete” and fetches all necessary fields) and add it to another Parent the code doesn’t work.
Here is my code for the Parent doctype called (Delete Switch Test), child table which links to Delete is (Switch Test Delete) :
frappe.ui.form.on("Delete Switch Test", {
refresh(frm) {
// your code here
}
});
frappe.ui.form.on("Switch Test Child", {
rate:function(frm, cdt, cdn){
var d = locals[cdt][cdn];
frm.doc.switch_test_child.forEach(function(d) {
switch(d.select_type) {
case 'A':
frappe.model.set_value(d.doctype, d.name, "money_selected", d.type_1);
break;
case 'B':
frappe.model.set_value(d.doctype, d.name, "money_selected", d.type_2);
break;
case 'C':
frappe.model.set_value(d.doctype, d.name, "money_selected", d.type_3);
break;
}
});
}
});
Not much but this is something to work with. Is there any way I can get this to work with a child table ?