Get Manager with connected People fields using JavaScript

  • 28 January 2019
  • 2 replies
  • 214 views

Badge +1

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


2 replies

Badge +4

Great work!

Is there a way to move the cursor to different field other than Manager text box. 


In my case I would populate employee as current user and manager as current users manager using this approach on form load.


But no matter whatever JS I would try to move the cursor to the first text box(in my case it is title), the cursor always ends up in Manager text box.

Reply