Create your product list as you want, just add price column as number to it as well.
In Order List, create the desired columns, for price/quantity create a number column and for Sub-total create a calculate column with formula =[Price_Col_Name]*[Qty_Col_Name].
Now open the Order List new form. Now click on setting(gear) icon on top right corner and select edit page. Now follow steps:
- Click on link
Add a Web Part
.
- In Categories column select
Media and Content
and in Part column select Script editor
and click on Add
button.
- You'll see that the web part is added to your page. And inside web part there will be a hyperlink saying
Edit Snippet
. Click on edit snippet.
- A new popup will be open with a text area to write script. In this copy page the following script (make sure you change the variable where ever i mentioned, dont worry, I'll tell how you can find those as well).
Script
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script>
var OrgPriceId = "input[title='OrgPrice']";
var ProductDropDownId = "select[title='Product']";
var siteUrl = "http://site_url/";
$(document).ready(function(){
$(OrgPriceId).prop("readonly", "readonly");
$(ProductDropDownId).on('change', LoadPrice);
});
function LoadPrice(){
var productId = $(ProductDropDownId).val();
if(productId != null && productId != ""){
var url = siteUrl + "_api/web/lists/getbytitle('Product')/items(" + productId + ")";
$.ajax({
url: url,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json,odata=verbose",
},
success: function(data){
if(data.d !== undefined){
$(OrgPriceId).val(data.d.Price); // data.d will remain same you need to find Price column name.
}
}
});
}
}
</script>
In this script you need to edit following things:
1. "input[title='OrgPrice']"
to "input[title='Org Price Column Name']"
.
2. select[title='Product']
to select[title='Product column name']
.
3. http://site_url/
to your exact site URL. If you are unsure what is your site collection URL then open any of the list Product or Order and remove everything from List/Order........
. Take the first part.
4. And finally data.d.Price
to the data.d.Price_Column_Internal_Name
. If you dont know how to get this. Open Product List Setting, under columns section find Price Column and click on it, it will navigate to a new page. In the Url find &Field=Internal_Name
. Grab the internal name and replace it.
Repeat the same steps for Edit form as well.