- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:15 AM
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:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 06:01 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:41 AM
Line 4 and 6 are incorrect.
They are missing g_form.getValue('');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 05:43 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 06:01 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2024 06:12 AM
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