Auto populate text field with people picker value

Hello,
I have a form with a SharePoint people picker lookup field (EmployeeName). What i would like to accomplish is auto populate a non-people picker field (just a text field) with the value that's in the EmployeeName (people picker) field, which is just their name (Doe, John).

I'm new to JS so if someone could assist i would appreciate...thanks!

Hello @ParAvion,

You can use the below script to set the field value with the User Name selected in the people picker field. Just replace Title and PeoplePicker with internal field names.

fd.spRendered(function() {

     fd.field('PeoplePicker ').$on('change', function () {
                fd.field('Title').value = fd.field('PeoplePicker ').value.DisplayText;
     });
});

Hi @mnikitina thanks again!

1 Like

@mnikitina since updating the app package and re-saving my form with the new forms designer, this script no longer works. Any thoughts?

Blockquote
image

Hey @ParAvion,

Could you share more on the problem?
I just tested the script and it works fine for me. Does it put something you don't expect into the text field, or does it not put anything there at all?

All I can suggest without that information is double-checking that all field names are the same in the Form Builder and the code.

The 'Title' field is blank. It should be auto populating from the people picker field titled 'Name' but since updating the app package and forms designer, it has stopped working.

Does your browser console have any errors?

It'd be great if you opened it by pressing F12, opened the form, and shared screenshots of all messages in the console.

@IliaLazarevskii sent you a PM with the console info. Please let me know thanks.

@IliaLazarevskii did you get my PM?

Hey @ParAvion,

I did.
I can't tell much by looking at it though, please export your form (or both forms) and send it to me in a PM.

@IliaLazarevskii I sent them to you...thanks.

@IliaLazarevskii Hi, any update on this? I sent you all the things you asked for.

Hi @ParAvion,

I couldn't replicate the issue so far, everything works fine on my end. Try using the debugger statement: debugger - JavaScript | MDN

Another good call is to check if you're getting all your fields correctly:

console.log(fd.field('Name'));

This statement should print a bunch of information about an element in the console. If it prints null or undefined, you need to check the field name and maybe try using control() instead of field().

There's a great chance that the problem turns out to be with the "Person" field. If that's the case, then put some of your code inside a

fd.field('PersonField').ready(function(field) {
});