How to write a business rule or script for quantity field

Joshua Comeau
Kilo Sage

Looking to know how to write the code/rule for the following:

 

Need to order is a formula field that describes the need to order Assets to the maximum level once they have dropped below the min. 

 

Scenario 1:

“Min” = 5, “Max” = 10, “quantity in stock” = 6, “need to order” = no, “quantity to order” = 0 – This is because the quantity in stock is above the Min

 

Scenario 2:

“Min” = 5, “Max” = 10, “quantity in stock” = 4, “need to order” = yes, “quantity to order” = 6 – This is because the quantity in stock dropped below the min and we need to order to the maximum level

 

EX:

JoshuaComeau_0-1721128336404.png

 

1 ACCEPTED SOLUTION

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Joshua Comeau 

 

You can try the below script :

On change script : with onchange of field : Quantity max

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var qinstock = g_form.getValue('quantity_in_stock');
    alert(qinstock);
    var qmin = g_form.getValue('quantity_min');
    alert(qmin);
    var qmax = g_form.getValue('quantity_max');
    alert(qmax);
    var quantitytoorder = qmax - qinstock;
    alert(quantitytoorder);

    if (qinstock < qmin) {
        alert(quantitytoorder);
        g_form.setValue('need_to_order', 'yes');
        g_form.setValue('quantity_to_order', quantitytoorder);
    }
	else{
		g_form.setValue('need_to_order', 'no');
        g_form.setValue('quantity_to_order', 0);

	}

    //Type appropriate comment here, and begin script below

}

 

Thanks and Regards

Sai Venkatesh

View solution in original post

1 REPLY 1

SAI VENKATESH
Tera Sage
Tera Sage

Hi @Joshua Comeau 

 

You can try the below script :

On change script : with onchange of field : Quantity max

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var qinstock = g_form.getValue('quantity_in_stock');
    alert(qinstock);
    var qmin = g_form.getValue('quantity_min');
    alert(qmin);
    var qmax = g_form.getValue('quantity_max');
    alert(qmax);
    var quantitytoorder = qmax - qinstock;
    alert(quantitytoorder);

    if (qinstock < qmin) {
        alert(quantitytoorder);
        g_form.setValue('need_to_order', 'yes');
        g_form.setValue('quantity_to_order', quantitytoorder);
    }
	else{
		g_form.setValue('need_to_order', 'no');
        g_form.setValue('quantity_to_order', 0);

	}

    //Type appropriate comment here, and begin script below

}

 

Thanks and Regards

Sai Venkatesh