To auto-populate fields on a Power Pages form based on a selected Contact from a lookup field, you can achieve this using JavaScript and Web API calls within Power Pages. Here's a step-by-step guide to help you implement this:
Goal:
When a user selects a Contact from a lookup field, the form should automatically populate fields like email, phone, etc., from the selected Contact's record in Dataverse.
Steps to Implement:
1. Enable JavaScript on the Form
In Power Pages:
- Go to your form in the Design Studio.
- Under Advanced, enable Custom JavaScript.
2. Add JavaScript to Fetch Contact Details
Here’s a sample script you can use:
// Replace 'contactid' with your actual lookup field name
// Replace 'email', 'telephone1', etc., with your actual field names on the form
function onContactChange(executionContext) {
var formContext = executionContext.getFormContext();
var contactLookup = formContext.getAttribute("contactid").getValue();
if (contactLookup && contactLookup.length > 0) {
var contactId = contactLookup[0].id.replace("{", "").replace("}", "");
Xrm.WebApi.retrieveRecord("contact", contactId, "?$select=emailaddress1,telephone1,firstname,lastname").then(
function success(result) {
formContext.getAttribute("email").setValue(result.emailaddress1);
formContext.getAttribute("phone").setValue(result.telephone1);
formContext.getAttribute("firstname").setValue(result.firstname);
formContext.getAttribute("lastname").setValue(result.lastname);
},
function (error) {
console.log("Error retrieving contact: " + error.message);
}
);
}
}
🏷️ Tag me if you have any further questions or if the issue persists.
✅ Click "Accept as Solution" if my post helped resolve your issue—it helps others facing similar problems.
❤️ Give it a Like if you found the approach useful in any way.