Mite87
July 29, 2024, 11:34am
1
Under the Sales order Doctype I have created two Custom Fields
BillingAmount
Job Type (FABC, SUPG)
I want to create a client script that if JobType is FABG update the item table with following
Item Code = MS PIPE (item already created under Items doctype)
Rate (INR) = BillingAmount (custom field of Sales Order doctype)
I have tried below script but it doesn’t work (I am still new to scripting)
frappe.ui.form.on("Sales Order Item", {
custom_ BillingAmount: function(frm, cdt, cdn) {
const row = locals[cdt][cdn];
const rate = row.custom_ BillingAmount; // Assuming custom_cylinderamount is a numeric field
row.rate = rate;
frm.refresh_field("items"); // Refresh the child table
// Set item_code to "MS Pipe" if custom_Jobtype is "FABG"
if (frm.doc. custom_Jobtype === "FABG") {
row.item_code = "MS Pipe";
frm.refresh_field("items.item_code"); // Refresh the item_code field
}
}
});
Meet
July 29, 2024, 11:36am
3
@Mite87 Please write this code and then try it
frappe.ui.form.on("Sales Order", {
custom_ BillingAmount: function(frm, cdt, cdn) {
const row = locals[cdt][cdn];
frm.refresh_field("items"); // Refresh the child table
// Set item_code to "MS Pipe" if custom_Jobtype is "FABG"
if (frm.doc. custom_Jobtype === "FABG") {
row.item_code = "MS Pipe";
const rate = frn.doc.custom_ BillingAmount; // Assuming custom_cylinderamount is a numeric field
row.rate = rate; frm.refresh_field("items"); // Refresh the item_code field
}
}
});
1 Like
Mite87
July 29, 2024, 5:57pm
4
Hi Meet,
Thank you for your quick response. I have tried your script with some console.log thing but looks the script is not working. it doesnt add the item into the sales order item table. Its still empty field under Item_code
Jeel
July 29, 2024, 6:33pm
5
@Mite87 try this
frappe.ui.form.on(“Sales Order”, {
custom_billingamount: function(frm) {
if (frm.doc.custom_jobtype === "FABG") {
const new_item = frm.add_child("items");
new_item.item_code = "MS PIPE";
new_item.rate = frm.doc.custom_billingamount;
frm.refresh_field("items");
}
}
});
1 Like
Mite87
July 30, 2024, 2:25am
6
I tried this code but it still wont add new row to the items table. to simplify even further i tried to remove the jobtype check and hard coded the custom_billinbamount but still the script will not add new items.
frappe.ui.form.on("Sales Order", {
custom_BillingAmount: function(frm) {
console.log("Custom Billing Amount:", frm.doc.custom_BillingAmount);
if (frm.doc.custom_BillingAmount === "2000") {
const new_item = frm.add_child("items");
new_item.item_code = "MS Pipe";
new_item.rate = frm.doc.custom_BillingAmount;
frm.refresh_field("items");
}
}
});
Jeel
July 30, 2024, 2:30am
7
@Mite87 check bowser console can you share screenshot of your fieldnames I think we are using wrong fieldname
Mite87
July 30, 2024, 2:45am
8
Sure here are the screenshots of it the custom_billingamount field type is Currency
Jeel
July 30, 2024, 2:48am
9
@Mite87 try my script I updated my script we are using wrong fieldname custom_BillingAmount instead of custom_billingamount so change event is not working custom_Jobype is also wrong fieldname instead of custom_jobtype
Mite87
July 30, 2024, 3:46am
10
Tried your updated code and also added some console log to troubleshoot
frappe.ui.form.on("Sales Order", {
custom_billingamount: function(frm) {
console.log("custom_BillingAmount:", frm.doc.custom_billingamount);
if (frm.doc.custom_jobtype === "FABG") {
console.log("custom_jobtype:", frm.doc.custom_jobtype);
const new_item = frm.add_child("items");
new_item.item_code = "MS Pipe";
new_item.rate = frm.doc.custom_billingamount;
// Log the values for debugging
console.log("custom_billingAmount:", rate);
console.log("item_code:", item_code);
frm.refresh_field("items");
}
}
});
it looks like somehow its not going to the if condition for the jobtype.
in console i only see log for the custom_BillingAmount amount.
Jeel
July 30, 2024, 3:48am
11
@Mite87 can you share me screenshot of fieldname jobtype and please select FABG in jobtype
Jeel
July 30, 2024, 3:53am
13
@Mite87 can you share me ss of sales order with value
Jeel
July 30, 2024, 3:55am
15
@Mite87 remove this line console.log(“custom_billingAmount:”, rate);
Jeel
July 30, 2024, 3:58am
16
@Mite87 i think its working now
Mite87
July 30, 2024, 4:06am
17
Really Appreciate your help @Jeel .
It added new item now.
1 Like