Sales Order Item Custom Script

I’m trying to populate a field with a value based on the value from another field in Sales Order Item.

So when a user chooses item_code == “Artwork” I want job_type to populate automatically with “New”.

This is the script code I’m trying:

<input type="Link" name="item_code" onChange="fixField();">
<input type="Select" name="job_type">

<script language="javascript">
	function fixField() {
		if (doc.item_code=="Artwork") {
			doc.job_type="New";
		}
	}
</script>

I’ve tried a few variations but none work - input type = “text”, using doc.item_code.value syntaxt, etc.

All to no avail.

Can anyone help this javascript n00b get this working please?

Thanks :smile:

@monojoker Looks like you are editing a table in the screenshot. If yes, create a custom script for your doc type or even better write a js file for your doctype and key in the following.

cur_frm.cscript.item_code = function(doc, cdt, cdn) {
    var job = frappe.get_doc(cdt, cdn);
    if (job.item_code == "Artwork") {
        job.job_type="New";
        refresh_field("job_type", job.name, job.parentfield);
    }
}
1 Like

Hi Bhupesh,

Thankyou that’s a great help. Sorry for the really stupid question but do I need to modify that code or will it work simply copying and pasting into my Sales Order Item custom script? I tried doing that but it doesn’t work - still not filling the job_type field with “New” when I add item_code “Artwork”.

I really am completely new to javascript so if you can let me know what I need to customise at my end in that script it’d be great.

Thanks again.
Liam.

Copy paste it in sales order instead of sales order item

You should always use

frappe.ui.form.on("Sales Order", "item_code", function(frm) { 
  ..
});

overriding cur_frm.cscript.item_code can be dangerous.

1 Like

Whoa, you’re going to have to elaborate on that. When I simply replace line 1 with that code I get just the headings on the Sales Order and no fields at all!

What I found with Bhupesh’s code is it worked great and populated the Job_type field but it prevented the automatic fill of the other fields like pricing, item description, etc.

@rmehta I assume your variant might stop that from happening but I cannot get it to work at all.

    frappe.ui.form.on("Sales Order", "item_code", function(frm) {
    var job = frappe.get_doc(frm);
    if (job.item_code == "Artwork") {
        job.job_type="New";
        refresh_field("job_type", job.name, job.parentfield);
    }
}

Try this one:

frappe.ui.form.on("Sales Order Item", "item_code", function(frm, cdt, cdn) {
    var job = frappe.get_doc(cdt, cdn);
    if (job.item_code == "Artwork") {
        job.job_type="New";
        refresh_field("job_type", job.name, job.parentfield);
    }
}

I actually tried that variant first before editing it down further and it had the same effect. I get the “blank” sales order with no fields just the headings. I might try a different solution - perhaps just not enforcing mandatory fields but using Depends On to display the data entry fields we need per Item Group.

Thanks.

frappe.ui.form.on("Sales Order", "item_code", function(frm, cdt, cdn) {
    var job = frappe.get_doc(cdt, cdn);
    if (job.item_code == "Artwork") {
        job.job_type="New";
        refresh_field("job_type", job.name, job.parentfield);
    }
}

Thanks Bhupesh, unfortunately it’s the same result - a blank Sales Order form :frowning:

It would be great to get it working but I will stop for now in case I break the form!

put ) in the last of the script and your problem will be resolved

see the below example.

frappe.ui.form.on(“Sales Order”, “item_code”, function(frm, cdt, cdn) {
var job = frappe.get_doc(cdt, cdn);
if (job.item_code == “Artwork”) {
job.job_type=“New”;
refresh_field(“job_type”, job.name, job.parentfield);
}
})