Help with the custom script

Hi ,

Thank you all for the continued support . Could anyone guide me with the below script ?

The below script works like a charm for me , but this code should get activated only if the item_group is a Raw material , what should I do ?

{

cur_frm.cscript.custom_item_group = compute;
function compute(doc, cdt, cdn)
{
//Find out the first two digits of the code
temp_code = "";
switch(doc.item_group) 
{
default:
temp_code = doc.item_group.charAt(0);
}
//Get a list of all the exisiting codes with the same first two digits
frappe.call({ method:"frappe.client.get_list",
args:{
doctype: "Item",
filters: {"item_group": ["like", temp_code + "%"]},
fields: ["name"],
limit_page_length: 500
},
callback: function(r) {
//Count the number of elements in the list (which will decide what customer code to try)
if (r.message) {
temp_code += zeroPad(r.message.length, 5);
console.log(temp_code );
}
else{
temp_code += "0000";
console.log(temp_code );
}
// You probably want to add a section of code here that checks to see if the new code is an element of the list, 
// and increment the code if that customer code already exists (this only comes up if a customer is deleted)
// Otherwise, you could create multiple customers with the same code.
//Submit the customer code and refresh the field
doc.item_code= temp_code;
refresh_field("item_code");
}
});
}	
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
}

Any help ? .Thanks

Can you explain a bit more?

What do you mean by the “Item Group is a raw material”?

What document is this for?

1 Like

@Ben_Cornwell_Mott . Many thanks for the response , what I actually want is the item_code in the item module should get automatically updated only if the item is a raw material , what happens with the above code is it updates raw material , products and consumables .

The above code should get triggered only if the item_group field in the item module is a Raw material

I tried with a if loop if(doc.item_group==“Raw material”) and enclose the code but it just bricks the system .

Any help ? Thanks

@Ben_Cornwell_Mott . I got it please close this thread . Thanks

Can you post your working code to help others in the future?

1 Like

Sure ,Thanks

{

cur_frm.cscript.custom_item_group = compute;
function compute(doc, cdt, cdn)
{
if(doc.item_group=="Raw Material")
{
//Find out the first two digits of the code
temp_code = "";
switch(doc.item_group) 
{
default:
temp_code = doc.item_group.charAt(0);
}
//Get a list of all the exisiting codes with the same first two digits
frappe.call({ method:"frappe.client.get_list",
args:{
doctype: "Item",
filters: {"item_group": ["like", temp_code + "%"]},
fields: ["name"],
limit_page_length: 500
},
callback: function(r) {
//Count the number of elements in the list (which will decide what customer code to try)
if (r.message) {
temp_code += zeroPad(r.message.length, 5);
console.log(temp_code );
}
else{
temp_code += "0000";
console.log(temp_code );
}
// You probably want to add a section of code here that checks to see if the new code is an element of the list, 
// and increment the code if that customer code already exists (this only comes up if a customer is deleted)
// Otherwise, you could create multiple customers with the same code.
//Submit the customer code and refresh the field
doc.item_code= temp_code;
refresh_field("item_code");
}
});
}	
function zeroPad(num, places) {
var zero = places - num.toString().length + 1;
return Array(+(zero > 0 && zero)).join("0") + num;
}
}
}