For this tutorial in video form, click here

In this article, we’ll show you how to work with the line items in sales orders. Let’s say you wanted a way to be able to generate purchase orders based on the data in a sales order. In a sales order, the data there is kept pretty minimal. Consider this example product:

We’ve got all of these pack details with information about the type of solar panels that they are going to use for that particular pack. When it comes to creating a purchase order, we need to actually list all of those line items. This is because the customer only sees the pack but we need to purchase all of the parts in the pack from suppliers.

The Process

We’ve set up a new custom button called “Create Purchase Order”:

The button runs a custom function which generates the purchase order and then opens it in a new tab. Check out all the line items our example has:

They represent the products within the pack. You might be wondering how this works. This is the custom function:

You can see that we first get the sales order data. On line 3, the product details. This is essentially an array containing all of the line items. In the rest of the custom function, we’re getting the account details:

Also, (and perhaps most importantly) we go through each of the line items in the sales order. From that, we take the product ID. We then look up that product and get the pack details:

We process the JSON that’s inside the pack.

You can see it’s a JSON object with some information about the type of product. It also includes the ID of the product and the quantity. We iterate through each of the components of the pack using pack_details_map.keys().

For each item in the pack, we get the product ID, quantity, and tax type. Then we construct a new line item.

If there’s tax applicable, we can add that too:

Adding It Up

Finally, we add everything together and calculate the totals with and without tax:

After that, we construct the purchase order object.

Notice how we have product details to set the line items for that purchase order. After creating the purchase order record, we get the ID and open the new record in a new window. Hopefully this tutorial made sense and was helpful. Good luck!

For this tutorial in video form, click here