Hi there,
We’ve built a Power Pages form to allow users to submit extension requests for our department. (Please find a screenshot of the form attached.)The intention is that when a user selects their Contact from the lookup field, the rest of their details (e.g., email, phone, etc.) should auto-populate from their existing record in our CRM. However, we haven't had any success getting this to work so far. Is there a recommended way to achieve this? Ideally, if a contact already exists in the system and the user selects them from the Customer lookup, the form should automatically fill in their associated details, allowing them to simply review and submit.
Any guidance or suggestions would be greatly appreciated.
Kind regards,
what we are trying to do is when the contact field is changed - the js looks up the contact & retreives the email & wsu id, & then populates the fields with them. Currently the wsu id is populated on form load & doesn't change. The code above is supposed to clear the fields when the contact is de-selected. At the moment the console log lines are not displaying in the browser dev tools console so hard to troubleshoot.
contact table email logical name
contact table wsuid logical name
case table form displaying in portal - contact field event on change
web resource settings
console.log("Custom JS loaded");
// 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();
console.log('Contact change triggered');
var contactLookup = formContext.getAttribute("primarycontactid").getValue();
if (contactLookup && contactLookup.length > 0) {
// Contact selected
var contactId = contactLookup[0].id.replace("{", "").replace("}", "");
Xrm.WebApi.retrieveRecord("contact", contactId, "?$select=emailaddress1,mobilephone,new_wsuidn").then(
function success(result) {
console.log("Retrieved contact:", result);
if (formContext.getAttribute("emailaddress")) {
formContext.getAttribute("emailaddress").setValue(result.emailaddress1);
}
// Only set WSUID if it's currently empty
var wsuidAttr = formContext.getAttribute("crdff_wsuid");
if (wsuidAttr && !wsuidAttr.getValue()) {
wsuidAttr.setValue(result.new_wsuidn);
}
},
function (error) {
console.error("Error retrieving contact: " + error.message);
}
);
else {
console.log("No contact selected. Clearing fields.");
const wsuidAttr = formContext.getAttribute("crdff_wsuid");
if (wsuidAttr) {
console.log("Before clear:", wsuidAttr.getValue());
wsuidAttr.setValue(null);
console.log("After clear:", wsuidAttr.getValue());
}
}
}
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:
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.
In Power Pages:
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);
}
);
}
}
Fubar
69
Super User 2025 Season 1
oliver.rodrigues
49
Most Valuable Professional
Jon Unzueta
43