Tuesday, May 14, 2013

How to update Unit Price into a subform based on Product selection



The sequence to execute the logic you are looking to accomplish is exactly as adding a script on a regular form.  On Add, On Delete, On User Input, but there is a subtle difference in the code and there is also a different way to embed the script into a subform, so lets tackle the way we embed the script.
On a regular form we have several options: Script Builder, Free Flow and Form Definition; where you can actually write the code manually if you know what you are doing. Inside a subform there is NO Script Builder that would guide you through the options of data you can access and a user is left with writing the code manually on a Free Flow approach style. The solution is to write the code using the Script Builder on a regular form save it from a free flow screen and take it into the subform corresponding action.
In the image below you will see an Order Management Form displaying regular fields about a Product and its Unit Price along with a subform that aggregates products in the same way. For the purpose of this example, we have created the script to update the Unit Price based on Product selection.
Now, notice how subtle the difference in the code is once we compare the lines of code that would update the fields on the right ( regular form ) to the ones on the left ( subform ) and you will see that they are nearly identical which provides us with a great opportunity to quickly incorporate scripts int any subform. Write the script on a regular form then paste it into the subform and correct the subtle difference.
SCRIPT FOR REGULAR FORM ON USER INPUT FOR ORDER ITEM.
//Fetches the information from the "Products" table
selected_Product = Products [Code = input.Order_Items];
//Updates the value of Unit Price using the stored value from "Products" table
input.Unit_Price = selected_Product.Price;
SCRIPT FOR SUBFORM ON USER INPUT FOR ORDER ITEM.
//Fetches the information from the "Products" table
selected_Product = Products [Code = row.Order_Items];
//Updates the value of Unit Price using the stored value from "Products" table
row.Unit_Price = selected_Product.Price;
Notice that the only difference between the 2 is the word row. before the field value of the subform. With this knowledge you can now write the script you need on a regular form by simulating the subform fields, import the code and modify with the word row. as needed.

No comments:

Post a Comment