Person field doesn't return mail

Hey all, I've created a roombooking system where I have a field "Verantwortlich" (=responsible Person) which I want to check against the current user to make sure that only the person who created the item or is responsible for the item can delete it.
It worked before but now suddenly it stopped working and I always get "undefined" from the following lines:

var value = fd.field('Verantwortlich').value.email;
console.log("VerantwortlichMail: " + value);

My whole code:

pnp.sp.web.lists.getByTitle('Raumbuchungen').items.getById(fd.itemId).get().then(function(item){
    console.clear();
    console.log("Item: " + item);
    var CreatedBy = item.AuthorId;
    var userID = _spPageContextInfo.userId;
    console.log("CreatedBy: " + CreatedBy);
    console.log("UserID: " + userID);
    /* Check, ob Verantwortlich ID = currentUser, dann darf er auch */
    var value = fd.field('Verantwortlich').value.email;
    console.log("VerantwortlichMail: " + value);
    sp.web.siteUsers.getByEmail(value).get().then(function(result) {
    var verantwortlichUserID = result.Id;
    //console.log("verantwortlichUserID: " + verantwortlichUserID);
        if (CreatedBy == userID || verantwortlichUserID == userID){
            //User verifiziert              pnp.sp.web.lists.getByTitle('Raumbuchungen').items.getById(fd.itemId).delete().then(function(){
                window.location.href = "http://xxx/websites/raumbuchungen/SitePages/PlumsailForms/Raumbuchungen/Ereignis/NewForm.aspx";
            });          
        }
        else{
            //User nicht verifiziert, abbrechen
            alert("Nur der Ersteller kann die Raumbuchung verschieben.");
        }
    });
});

Does somebody know why it stopped working? I haven't changed anything.

Hello @JonHebbe,

What SharePoint version are you using? And what version of the desktop designer are you using? You can find it in the top left of the application:
image

I'm using SP 2019 on Prem with the designer 1.8.7.

I just remembered one thing I changed: first I had the responsible person field in read-only because it was just there to be displayed but then I've changed it to edit mode. I guess that is where it broke.

Hello @JonHebbe,

You can try out the code below to get the email of the selected user:

fd.field('Verantwortlich').value.EntityData.Email
1 Like

Thanks for your fast help, that works.

1 Like

@mnikitina @Nikita_Kurguzov,

Good Day,

I have a scenario where I have a Manager field which is a people/group field.
I want to be able to stop the user who is the Author/creator of the form to input their own name into the Manager field as they cannot select themselves.

At the moment the author field is not bringing up any value before it is saved (meaning on load/new form) it only gives the author value after save so the manger field validation is not working as it cannot check whether the author and manager field is = to each other. Can you please advise/help with this in JS somehow

Kind regards,

Hello @Dina_Louw,

You can add a custom validation in the Person or Group field to make sure that the selected user is not the current user:

fd.field('FieldName').validators.push({
    name: 'Custom validation',
    error: "Error message",
    validate: function(value) {
        if (value && value.Key.includes(_spPageContextInfo.userEmail)) {
            return false;
        }
        return true;
    }
});
1 Like

Thank you that worked better than the code I had.
Much appreciated

1 Like