Script not setting the value

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

 

This is what I have so far and it is not working:

JoshuaComeau_0-1721132127014.png

 

1 ACCEPTED SOLUTION

MackI
Kilo Sage

hi @Joshua Comeau 

 

Problems with your Code
1. The condition checks if the quantity in stock (qtystock) is less than the minimum (min). If it is, it sets the u_need_to_order field to 'no' and the u_quantity_to_order field to '0'. However, the logic should be to set u_need_to_order to 'yes' and calculate the u_quantity_to_order if the quantity is below the minimum.
2. The condition should be setting values correctly based on the scenarios provided.

 

Correct Code 

function onLoad() {
    var min = g_form.getValue('u_quantity_min');
    var max = g_form.getValue('u_quantity_max');
    var qtystock = g_form.getValue('quantity');
    var needorder = 'no';
    var qtyorder = 0;

    // Ensure the values are treated as integers for comparison
    min = parseInt(min, 10);
    max = parseInt(max, 10);
    qtystock = parseInt(qtystock, 10);

    if (qtystock < min) {
        needorder = 'yes';
        qtyorder = max - qtystock;
    }

    g_form.setValue('u_need_to_order', needorder);
    g_form.setValue('u_quantity_to_order', qtyorder.toString());
}

 

 

Explanation
* Variables: The variables min, max, and qtystock are retrieved using g_form.getValue to ensure they are fetched from the form fields.
* Type Casting: The values are converted to integers using parseInt to perform numeric comparisons and calculations.
* Logic: If qtystock is less than min, it sets needorder to 'yes' and calculates qtyorder as max - qtystock.
* Setting Values: Finally, it sets the u_need_to_order and u_quantity_to_order fields accordingly.

 

 

A small request from my end, If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌(Accepted Solution) 🌠‌ OR  mark it  Helpful ‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

View solution in original post

4 REPLIES 4

AnirudhKumar
Mega Sage
Mega Sage

Line 4 and 6 are incorrect. 

They are missing g_form.getValue('');

Brad Bowman
Kilo Patron
Kilo Patron

Hi Joshua,

For starters, you need to use g_form.getValue('field_name') to retrieve values from fields displayed on the record in a client script. 

var min = g_form.getValue('u_quantity_min');

If you don't want/need to see this populate on the record as it loads, use a Business Rule on the table, triggered before Update when Min, Max, and/or Quantity in stock changes.

MackI
Kilo Sage

hi @Joshua Comeau 

 

Problems with your Code
1. The condition checks if the quantity in stock (qtystock) is less than the minimum (min). If it is, it sets the u_need_to_order field to 'no' and the u_quantity_to_order field to '0'. However, the logic should be to set u_need_to_order to 'yes' and calculate the u_quantity_to_order if the quantity is below the minimum.
2. The condition should be setting values correctly based on the scenarios provided.

 

Correct Code 

function onLoad() {
    var min = g_form.getValue('u_quantity_min');
    var max = g_form.getValue('u_quantity_max');
    var qtystock = g_form.getValue('quantity');
    var needorder = 'no';
    var qtyorder = 0;

    // Ensure the values are treated as integers for comparison
    min = parseInt(min, 10);
    max = parseInt(max, 10);
    qtystock = parseInt(qtystock, 10);

    if (qtystock < min) {
        needorder = 'yes';
        qtyorder = max - qtystock;
    }

    g_form.setValue('u_need_to_order', needorder);
    g_form.setValue('u_quantity_to_order', qtyorder.toString());
}

 

 

Explanation
* Variables: The variables min, max, and qtystock are retrieved using g_form.getValue to ensure they are fetched from the form fields.
* Type Casting: The values are converted to integers using parseInt to perform numeric comparisons and calculations.
* Logic: If qtystock is less than min, it sets needorder to 'yes' and calculates qtyorder as max - qtystock.
* Setting Values: Finally, it sets the u_need_to_order and u_quantity_to_order fields accordingly.

 

 

A small request from my end, If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌(Accepted Solution) 🌠‌ OR  mark it  Helpful ‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia

MackI
Kilo Sage

hi @Joshua Comeau 

Just want to add up regarding my last reply ----

for float numbers use 

parseFloat: This method is used to convert string values to floating-point numbers, ensuring that numeric comparisons and calculations are performed correctly.

Adjust your script accordingly if you want float number handling

 

If you like this opinion and your problem is resolved after reviewing and applying it. Please kindly mark this your best answer‌(Accepted Solution) 🌠‌ OR  mark it  Helpful ‌ if you think that you get some insight from this content relevant to your problem and help me to contribute more to this community

MackI | ServiceNow Developer | 2 *Mainline Certification | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia