Update Sales Order Item while creating sales order

Under the Sales order Doctype I have created two Custom Fields

  1. BillingAmount
  2. Job Type (FABC, SUPG)

I want to create a client script that if JobType is FABG update the item table with following

  1. Item Code = MS PIPE (item already created under Items doctype)
  2. 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
        }
    }
});

Reference:

@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

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

@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

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");
        }
    }
});

@Mite87 check bowser console can you share screenshot of your fieldnames I think we are using wrong fieldname

Sure here are the screenshots of it the custom_billingamount field type is Currency


@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

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.

@Mite87 can you share me screenshot of fieldname jobtype and please select FABG in jobtype


Here is the SS

@Mite87 can you share me ss of sales order with value


@Mite87 remove this line console.log(“custom_billingAmount:”, rate);

@Mite87 i think its working now

Really Appreciate your help @Jeel.
It added new item now.

1 Like