Compare People Picker fields and display alert

In the code below I would like to add a condition where if the Requester (People Picker field) value name is equal to the ProcessOwner (People Picker field) value name then an alert will display the result of the Process Owner name. I can't seem to get this to work in the below code:

fd.spRendered(function(){
fd.field("Requester").ready().then(function(field) {
fd.field("Requester").value = _spPageContextInfo.userLoginName;
fd.field("Requester").disabled = true;

});
})

// Existing code for setting Requester as current user and disabling the field

fd.spRendered(() => {

function handleProcessOwnerChange() {
const processOwner = fd.field('ProcessOwner').value;
const requester = fd.field('Requester').value;

if (processOwner && requester) { // Check if both values exist
  const processOwnerName = processOwner.EntityData.DisplayName; // Get Display Name
  const requesterName = requester.EntityData.DisplayName; // Get Display Name

  if (processOwnerName === requesterName) {
    alert(`Process Owner: ${processOwnerName}`); // Display alert with name
  }
}

}

fd.field('ProcessOwner').$on('change', handleProcessOwnerChange);
handleProcessOwnerChange(); // Call the function initially
});

Hi @Adam_Reyes,

Try using fd.field('processOwner').value.DisplayText instead of fd.field('processOwner').value.EntityData.DisplayName, like described here.

Let me know if this helps.

This didn't seem to change anything. I modified the code as stated but no alert box pops up to confirm. Here is the current code that I've modified based on your feedback:

fd.spRendered(function(){
fd.field("Requester").ready().then(function(field) {
fd.field("Requester").value = _spPageContextInfo.userLoginName;
fd.field("Requester").disabled = true;

});
})

// Existing code for setting Requester as current user and disabling the field

fd.spRendered(() => {

function handleProcessOwnerChange() {
const processOwner = fd.field('ProcessOwner').value;
const requester = fd.field('Requester').value;

if (processOwner && requester) { // Check if both values exist
const processOwnerName = processOwner.value.DisplayText; // Get Display Name
const requesterName = requester.value.DisplayText; // Get Display Name

if (processOwnerName === requesterName) {
alert(Process Owner: ${processOwnerName}); // Display alert with name
}
}
}

fd.field('ProcessOwner').$on('change', handleProcessOwnerChange);
handleProcessOwnerChange(); // Call the function initially
});

Hi @Adam_Reyes,

The text inside the alert() function should be encased in backticks. Additionally, it's better to use the email of the user instead of their name, since when you set the requester on form load the field stores the email instead of a user object. Try this:

fd.spRendered(function(){
    fd.field('Requester').ready().then(function(field) {
        fd.field('Requester').value = _spPageContextInfo.userEmail;
        fd.field('Requester').disabled = true;
    });
});
    
// Existing code for setting Requester as current user and disabling the field
    
fd.spRendered(() => {
    function handleProcessOwnerChange(requester) {
        const processOwner = fd.field('ProcessOwner').value;
        
        if (processOwner && requester) { // Check if both values exist
            const processOwnerName = processOwner.value ? processOwner.value.EntityData.Email : processOwner; // Get email
            const requesterName = requester.EntityData.Email; // Get email
            
            if (processOwnerName.toLowerCase() == requesterName.toLowerCase()) {
                alert(`Process Owner: ${processOwnerName}`); // Display alert with name
            }
        }
    }
    
    fd.field('Testperson').$on('change', (value) => handleProcessOwnerChange(value));
    fd.field('Testperson').ready((field) => { // Call the function as soon as the field is ready
        handleProcessOwnerChange(field.value);
    });
});