Populated field Department

Hi,

i have a field named with internal name Person_x0020_received_x0020_keys in which type based on sharepoint is Person or group. When i try to use this field to popolate second field named Recipient department in which i want to get Department based on sharepoint profile (Populate SharePoint form fields with profile information — SharePoint forms).

function updateUserInfo() {
    var employee = fd.field('Person_x0020_received_x0020_keys').value;

    if (employee && employee.Key){
        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) {
                     case 'Department':
                        fd.field('Recipient_x0020_department').value = props[i].Value;
                        break;                    
                }
            }
        });
    }
}

fd.spRendered(function() {
    //executes updateUserInfo on field change
    fd.field('Person_x0020_received_x0020_keys').$on('change', updateUserInfo);
});

Dear @Miroslav_Gapa,
Okay, so what's the issue? Does it not work? Do you have any errors in browser's console? Does the user in question have a Department?

You don't really need switch with just one case, you can simply use an if block, and you can add some console.log messages to see where the issue comes from:

function updateUserInfo() {
    var employee = fd.field('Person_x0020_received_x0020_keys').value;
    if (employee && employee.Key){
        console.log(employee);
        console.log(employee.Key);
        pnp.sp.profiles.getPropertiesFor(employee.Key).then(function(result) {
            var props = result.UserProfileProperties;
            console.log(props);
            for (var i = 0; i < props.length; i++) {
                if(props[i].Key ==  'Department'){
                        fd.field('Recipient_x0020_department').value = props[i].Value;
                        break;                    
                }
            }
        });
    }
}

fd.spRendered(function() {
    //executes updateUserInfo on field change
    fd.field('Person_x0020_received_x0020_keys').$on('change', updateUserInfo);
});

This way, you'll see if the properties are retrieved or not and what properties are available, in browser's console.

Hi thats the problem it doesnt work - here is log from console

Uncaught ReferenceError: FD is not defined
at HTMLDocument. (fd_Key%20management_New.aspx:569:19)
at j (plumsail.fd.jquery.js:2:27309)
at Object.fireWith [as resolveWith] (plumsail.fd.jquery.js:2:28122)
at Function.ready (plumsail.fd.jquery.js:2:29956)
at HTMLDocument.J (plumsail.fd.jquery.js:2:30322)

Here is the code from Javascript editor:

window.fd=FD;
Windows.$=$;
function updateUserInfo() {
    var employee = fd.field('Person_x0020_received_x0020_keys').value;
    if (employee && employee.Key){
        console.log(employee);
        console.log(employee.Key);
        pnp.sp.profiles.getPropertiesFor(employee.Key).then(function(result) {
            var props = result.UserProfileProperties;
            console.log(props);
            for (var i = 0; i < props.length; i++) {
                if(props[i].Key ==  'Department'){
                        fd.field('Recipient_x0020_department').value = props[i].Value;
                        break;                    
                }
            }
        });
    }
}

fd.spRendered(function() {
    //executes updateUserInfo on field change
    fd.field('Person_x0020_received_x0020_keys').$on('change', updateUserInfo);

});

Dear @Miroslav_Gapa,
Yes, in JavaScript the case of the letters is important. Please, replace the first line with:
window.fd=fd;

No capital letters. This should at least resolve the error you're getting.

Hi,

still some error in
fd.spRendered(function () {XUncaught TypeError: fd.spRendered is not a function
//executes updateUserInfo on field change
fd.field('Person_x0020_received_x0020_keys').$on('change', updateUserInfo);
});

Dear @Miroslav_Gapa,
That's because you're using code for Modern UI forms, this will not work with classic forms. fd.spRendered() is not an event that's available in classic forms, and it's not needed. There is also no pnpjs library included, so the code wouldn't work, but also the field values are set differently in classic designer, pretty much everything is different.

You can find how to work with fields in classic Forms Designer on this page, including events and how to set values - Getting and setting SharePoint form field values - SharePoint Forms Designer

For retrieving user profile properties, you'll also need to find a different approach. The method described here should work, after some tuning - javascript - Get Current Login User Profile Properties SharePoint - SharePoint Stack Exchange