If you build Microsoft Power Apps Model-Driven Apps on Dataverse, you will eventually hit the same “why is this not built-in?” requirement: when a user selects a record in a Dataverse lookup field, you want to copy a value from the related record and auto-populate a field on the current form. This post shows a simple, reusable Power Apps Model-Driven App JavaScript template that uses Xrm.WebApi to read a field from a lookup record and set a field on the current record.
This is a practical pattern for Dynamics 365, Power Platform, Dataverse, and Model-Driven App form scripting: lookup-driven auto-fill, lookup copy, lookup mapping, and “populate field from related record” automation.
What this is for?
Use this template when you need any of the following in Microsoft Power Apps Model-Driven Apps (Dataverse):
- Auto-populate a field from a lookup (copy value from related record to current form)
- Set a field when a lookup changes (OnChange lookup automation)
- Read a Dataverse field via Xrm.WebApi and write it into another field on the form
- Default values based on lookup selection (example: Material → Cost, Account → Credit Limit, Product → Price)
- A reusable low-code-ish JavaScript form script for Dynamics 365 / Dataverse form customization
Under the hood, the script attaches an OnChange handler to a lookup field on form OnLoad, retrieves the selected lookup record via Xrm.WebApi.online.retrieveRecord, and sets the target field value on the current record. lookupCopyValue lookupCopyValue
Why is this needed? How does it provide quick value?
In real-world Power Apps Model-Driven Apps and Dataverse implementations, users expect the app to behave like a “smart form”:
- They pick a record in a lookup, and related data should auto-fill.
- They should not have to open the lookup record just to copy a value manually.
- You want consistent values, fewer typos, fewer missed steps, and faster data entry.
This pattern delivers quick value because it is:
- Fast to deploy (one JavaScript web resource + one OnLoad handler)
- Reusable across tables and forms by editing a small configuration block lookupCopyValue
- Controlled (option to only set the target field if it is blank, so you don’t overwrite user edits) lookupCopyValue
- Standard Power Platform approach using supported client APIs (Xrm.WebApi, formContext, attributes) lookupCopyValue
How to Use the Reusable Template Below
Download the JavaScript template:
If it did not send the code to your email (check spam), follow this link: lookupCopyValue.js
This file contains a configuration block you edit and the logic you leave alone. The configuration lives at the top and includes:
lookupFieldName(lookup field on the current form)targetFieldName(field on the current form to set)relatedTableName(table the lookup points to)relatedFieldName(field on the related record to copy)onlySetWhenEmpty(true/false behavior) lookupCopyValue
How the template works:
- On form load, the script attaches an event handler to the lookup field. lookupCopyValue
- When the lookup changes, it checks whether the target field is empty (if
onlySetWhenEmptyis true). lookupCopyValue - If it should proceed, it retrieves the selected lookup record using Xrm.WebApi and reads the configured related field. lookupCopyValue
- It sets the target field on the current form and optionally fires OnChange for downstream logic. lookupCopyValue
Step-by-step instructions
(assume zero coding knowledge)
Step 1: Identify the “logical names” (schema names)
In Dataverse, fields and tables have “logical names” (often like prefix_fieldname).
You need four logical names:
- Lookup field logical name (on your current form/table)
- Target field logical name (on your current form/table)
- Related table logical name (table the lookup points to)
- Related field logical name (field you want to copy from the related record)
Tip for beginners: in the Maker portal / table designer, look for logical name, not Display Name.
Step 2: Download the JS template and edit only the config
Open the downloaded JS file and edit only this section:
lookupFieldNametargetFieldNamerelatedTableNamerelatedFieldNameonlySetWhenEmpty
Those are explicitly documented in the script comments. If you want it to never overwrite what a user typed into the target field, keep: onlySetWhenEmpty: true
If you want it to always overwrite the target field whenever the lookup changes, set: onlySetWhenEmpty: false
Step 3: Create a JavaScript web resource in your solution
- Go to your solution in Power Apps (Solutions area).
- Create a new Web resource.
- Type: JavaScript (JS)
- Upload your edited
lookupCopyValue.js. - Save and publish.
Step 4: Add the library to your form
- Open the table and the form you want to customize.
- Go to Form properties (or “Form libraries” depending on the editor).
- Add your new JavaScript web resource as a library.
- Save.
Step 5: Register the OnLoad handler (this is the “hook”)
Add a form OnLoad event handler that points to:
window.mls.lookupCopyValue.onLoad
This is called out in the script itself. Also ensure: “Pass execution context” = Yes (checked)
Save and publish.
Step 6: Test it on the form
- Open a record (or create a new one).
- Set the lookup field.
- Confirm the target field auto-populates.
If it does not work:
- Confirm you used logical names, not display names.
- Open browser dev tools console: the script intentionally logs warnings if it can’t find the field names on the form. lookupCopyValue
Conclusion
By following these steps, you can effectively add some custom code to take your Model-Driven Apps to the next level! Having values map from related tables through lookups is a super common requirement and helps tackle numerous limitations within the Power Platform.
Happy coding as you implement these enhancements to your Power Platform projects!