cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
robertopolo
Frequent Visitor

Update field in real time

I have a custom entity with a N: 1 relationship with the product entity. In this same entity I have created a field that obtains the price of the related product. So far I have only managed to get the field value to update when the record is created (using calculated field or a process) but I don't know how to make the field value update the moment a product is selected and not have to wait to save to get the price. Is there a way for the field to get the price of the product by selecting it?

1 ACCEPTED SOLUTION

Accepted Solutions
EricRegnier
Super User II
Super User II

Hi @robertopolo,

I assume you are using a model-driven app? If so, the common methods to get the price at real-time before saving the record is with JavaScript or with a quick view form.

Quick View Form

Create a quick view form on the Product entity and add the price field to that form. Then on your custom entity add that quick view form. When the product is selected, you see the price right away. Here are the steps to create a quick view form: https://docs.microsoft.com/en-us/powerapps/maker/model-driven-apps/create-edit-quick-view-forms#:~:t...

 

JavaScript

You can have a on change event on the Product field and in the handler make a Web Api call to retrieve and set the price. When the record will save, both the Product and Price values will be updated. I would update your process to only update the price if the value is different so that the price is not updated again for nothing. Here's a snippet to get that working:

//function registered on load event of form
function OnFormLoad(executionContext) {
 let formContext = executionContext.getFormContext();
 formContext.getAttribute("new_product").addOnChange(OnProductChanged);
}

function OnProductChanged(executionContext){
 let formContext = executionContext.getFormContext();
 let product = formContext.getAttribute("new_product").getValue();
 if (product) {
  let id = product[0].id;
  Xrm.WebApi.retrieveRecord("new_product", id, "?$select=new_price").then(
   function success(result) {
    formContext.getAttribute("new_price").setValue(result.new_price);
   });
 }
}

 Hope this helps!

View solution in original post

1 REPLY 1
EricRegnier
Super User II
Super User II

Hi @robertopolo,

I assume you are using a model-driven app? If so, the common methods to get the price at real-time before saving the record is with JavaScript or with a quick view form.

Quick View Form

Create a quick view form on the Product entity and add the price field to that form. Then on your custom entity add that quick view form. When the product is selected, you see the price right away. Here are the steps to create a quick view form: https://docs.microsoft.com/en-us/powerapps/maker/model-driven-apps/create-edit-quick-view-forms#:~:t...

 

JavaScript

You can have a on change event on the Product field and in the handler make a Web Api call to retrieve and set the price. When the record will save, both the Product and Price values will be updated. I would update your process to only update the price if the value is different so that the price is not updated again for nothing. Here's a snippet to get that working:

//function registered on load event of form
function OnFormLoad(executionContext) {
 let formContext = executionContext.getFormContext();
 formContext.getAttribute("new_product").addOnChange(OnProductChanged);
}

function OnProductChanged(executionContext){
 let formContext = executionContext.getFormContext();
 let product = formContext.getAttribute("new_product").getValue();
 if (product) {
  let id = product[0].id;
  Xrm.WebApi.retrieveRecord("new_product", id, "?$select=new_price").then(
   function success(result) {
    formContext.getAttribute("new_price").setValue(result.new_price);
   });
 }
}

 Hope this helps!

View solution in original post

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

V3_PVA CAmpaign Carousel.png

Community Challenge - Giveaways!

Participate in the Power Virtual Agents Community Challenge

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (3,596)