Populate another saved user from user profile

Good day .
I have 2 fields(Author and Inlocuitor(people picker, that was introduced in a new form.)
Now in edit i need to get of this users job titles and department from user profile ..
i try this code for Inlocuitor:

fd.spRendered(function(){
function updateUserInfo1() {
    //ascund sau arat soldurile de concedii
    var employee = fd.field('Inlocuitor').value;
	var niv1 = "";
  var niv2 = "";
  var niv3 = "";
    var sec = "";
    var tip = "";
    //if (employee.length>0){
    //console.log(employee.length);
    // Popularea cimpului cu Nume Prenume
    pnp.sp.profiles.getPropertiesFor(employee.Key).then(function (result) {
       
        var props = result.UserProfileProperties;

        for (var i = 0; i < props.length; i++) {
            switch (props[i].Key) {
                // Sharepoint or AD attribute  
 case 'DENNIVEL1':
              //Form Field Internal name
              niv1 = props[i].Value;
              break;
          case 'DENNIVEL2':
              //Form Field Internal name
              niv2 = props[i].Value;
              break;
          case 'DENNIVEL3':
              //Form Field Internal name
              niv3 = props[i].Value;
              break;
                
                    break;
               
                case 'TIPSUBUNITATE':
                    //Form Field Internal name
                    tip = props[i].Value;
                    break;
               
                    // Sharepoint or AD attribute 
                /*case 'DEP':
                    //Form Field Internal name
                    fd.field('Subdiviziunea').value = props[i].Value;
                    break;*/
                case 'Title':
                    //Form Field Internal name
                    fd.field('FunctiaInitiator').value = props[i].Value;
                    break;
  
               
            } //switch
        } //for
					 var subdiv = niv1;
  if (niv2 != "") {
      subdiv += "/" + niv2
  }
  if (niv3 != "") {
      subdiv += "/" + niv3
  }
  fd.field('SubunitateaInitiator').value = subdiv;
 
    }); //pnp
    //}//if
} 
updateUserInfo1();
})


Error:
VM8288:68 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'length')
at eval (eval at e._executeCustomJavaScript (/_layouts/15/plumsail/forms/widget/spform.js:73), :68:35)

I tried in console to check - fd.field('Inlocuitor').value.Key - it return undefined
in other form i used this code and it work, but i manualy put user in field, and put function on change.

Hello @ixxxl,

And how do you change the Person or Group field value on this form? Are you setting the field value programmatically?

Additional information about the user is loaded only if the field is changed manually.

@mnikitina
on new form initiator set this field manualy. after few steps this is another view , with saved disabled people field
there is no way to retrieve user info at this step ?

@ixxxl,

The code you've shared, is that the code from the Edit form?

How do you disable the field? Is it in a read-only mode?
image

@mnikitina
Hi, yes it was read only from the form
i disable field form on load -
fd.field('Inlocuitor').disabled=true
and it works! thank you!
there is one bad with field Author. it doesn't have option read only in settings, and it is by default is readonly. can i achieve from Author info?

@ixxxl,

You can use this code to get the author's login name:

fd.field('Author').value.key

@mnikitina
That was the problem from begining..
image

@ixxxl,

Declare fd globally in the JS editor like this:

image

Make sure that the Author field is added to the form, and then run the code in the browser's console to see extra author information available:
image

@mnikitina
The author field have value . it's ok. but have no "key"
image

@ixxxl,

In this case, you can get author profile properties like this:

pnp.sp.web.getUserById(fd.field('Author').value.id).get().then(function (user) {
    return user.LoginName
}).then(function(key){
    pnp.sp.profiles.getPropertiesFor(key).then(function (result) {
        console.log(result.UserProfileProperties);
    });
})
1 Like

@mnikitina
Thank you!! Works perfect

1 Like