cancel
Showing results for 
Search instead for 
Did you mean: 

Get Manager with connected People fields using JavaScript

redpanda
Nintex Newbie
1 1 480

I was trying to find a simple way to get an Employee's Manager in a People field on a form.  You can use a Calculated Field to display the Manager but I couldn't connect that directly to a People column.  Also, in some cases, the Manager a person reports to is not always the Manager in their User Profile, so I wanted to give users the ability to select the appropriate manager as an alternative option. I found a few posts that used JavaScript to do this, but they seemed needlessly complicated.  I used People Picker Extensions and JavaScript events in Nintex Forms as references and came up with a simpler solution (also, I'm using Nintex Forms Enterprise version 1.12.2.20). 

First, I added a People Field for the Employee:

  • Formula: Current User
  • Store Client ID in JavaScript variable: Yes
  • Client ID JavaScript variable name: Employee

Employee People Field

Second, I added a People Field for the Manager:

  • Default Value Source: Use connected field's default value
  • Store Client ID in JavaScript variable: Yes
  • Client ID JavaScript variable name: Manager

Manager People Field

Then, I added a Calculated Field to the form with the following attributes:

  • Formula: userProfileLookup(Employee,'Manager')
  • Store Client ID in JavaScript variable: Yes
  • Client ID JavaScript variable name: ManagerLan

Calculated Field

Finally, I added the code into the Custom JavaScript in the form Settings:

NWF.FormFiller.Events.RegisterAfterReady(function() {
   NWF$(document).ready(function(){
      GetManager();
   });
   var emp = new NF.PeoplePickerApi('#'+Employee);
   emp.added(function(){
      var people = NWF$('#' + Employee).val().split(';');
      if(people.length > 2){
         emp.remove(people[0]);
      }
      GetManager();
   });
});

function GetManager(){
   setTimeout(function(){
      var val = NWF$('#' + ManagerLan).val();
      var mgr = new NF.PeoplePickerApi('#'+Manager);
      if(val.length > 0){
         mgr.clear();
         mgr.search(val).done(function(data){
            mgr.add(data[0]);
         });
     }
   }, 1000);
}

The Manager people picker gets populated:

Form Excerpt

1 Comment
ms7
Nintex Newbie

Great work!