cancel
Showing results for 
Search instead for 
Did you mean: 
Disclaimer
JUMPCLOUD EXPRESSLY DISCLAIMS ALL REPRESENTATIONS, WARRANTIES, CONDITIONS, AND LIABILITIES OF ANY KIND ARISING FROM OR RELATED TO THIRD-PARTY SOFTWARE, SCRIPTS, REPOSITORIES, AND APIS. JUMPCLOUD IS NOT REQUIRED TO SUPPORT ANY SUCH THIRD-PARTY MATERIALS AND ALL RISKS RELATED TO THIRD-PARTY MATERIALS ARE YOUR RESPONSIBILITY. PLEASE ALSO REVIEW THE JUMPCLOUD TOS.

Google Form to automate user creation.

Fulgubbe
Novitiate III

Hi all,

Thought I share this Google script i made to give our HR the power to create Jumpcloud users when new employees joins our company. In its current form it's very basic but the plan is to build further on it to automate more processes involving a new employee. I have tried the Jira Multiplier function and also an existing Zapier solution to accomplish this but they did not really do what I wanted in the end so I ended up with this. 

At current state it will create in Jumpcloud (can easily be modified if you take a look at the code):

First Name, Last Name, Username (firstname.lastname), Company Email, Personal Email (for invitations), make user active, and Department

It will also send a notification to Slack that a new user is created.

How to get it going:

1. Create a Google Form with the following questions in this order:

Firstname

Lastname

Personal Email

Department

Like this:

form.png

2. Open script editor on that google form and paste in this code. Don't forget to edit your variables in the top!

 

/* #####################################################
  Enter Your Environment Variables
/ ##################################################### */

// Update your API Key. This is in your Jumpcloud Dashboard profile.
var API_KEY = 'YOUR JUMPCLOUD API'; //Put your Jumpcloud API key here
var EMAIL_DOMAIN = '@example.com'; //Put your email domain here including @
var SLACK_WEBHOOK = 'YOUR SLACK WEBBHOOK URL'; //Put your Slack Webbhook url here

// *************
// API CALLS TO Jumpcloud
// *************

// Add Jumpcloud User
function addJumpcloudUser(apiKey, payload){
  var headers = {
    "x-api-key": apiKey
  };
  var options =
      {
        "method" : "post",
        "payload": payload,
        "headers": headers,
        "content-type": "application/json",
        "followRedirects": true,
        "muteHttpExceptions":true
      };
  response = UrlFetchApp.fetch('https://console.jumpcloud.com/api/systemusers', options);
}

// *************
// Form Handler
// *************

// Collects input from form submition
// (must run this via form or an error will occur)

function onFormSubmit(e) {
  var form = FormApp.getActiveForm();
  var formResponse = e.response;
  var itemResponses = formResponse.getItemResponses();

  var user = {};
  user.firstname = itemResponses[0].getResponse();
  user.lastname = itemResponses[1].getResponse();
  user.email = itemResponses[2].getResponse();
  user.department = itemResponses[3].getResponse();
  

  user.username = user.firstname.toLowerCase() + "." + user.lastname.toLowerCase();
  user.username = user.username 
  .replace(/å/g, 'a')
  .replace(/Å/g, 'a')
  .replace(/ä/g, 'a')
  .replace(/Ä/g, 'a')
  .replace(/ö/g, 'o')
  .replace(/Ö/g, 'o');

  Logger.log("onFormSubmit - username: "+user.username);
  Logger.log("onFormSubmit - email: "+user.username + EMAIL_DOMAIN);
  Logger.log("onFormSubmit - department: "+user.department);
  Logger.log("onFormSubmit - firstname: "+user.firstname);
  Logger.log("onFormSubmit - lastname: "+user.lastname);
  Logger.log("onFormSubmit - alternateEmail: "+user.email);
  
  
  // Create user account
  var payload = {
    "username":user.username,
    "firstname":user.firstname,
    "lastname":user.lastname,
    "alternateEmail":user.email,
    "email":user.username + EMAIL_DOMAIN,
    "department":user.department,
    "activate":"true"
  };
  addJumpcloudUser(API_KEY,payload);
}


// *************
// Webbhook call to Slack
// *************

function postToSlack() {
  var payload = {
    "text": "New Jumpcloud User Added!.\nMake sure to check that all is correct!.",
  }
 
  var options =
      {
    "method" : "post",
    "content-type": "application/json",
    "payload" : JSON.stringify(payload)
  };
 
  return UrlFetchApp.fetch(SLACK_WEBHOOK, options)
}

 

3. Create 2 separate triggers for the script looking like this:

script trigger.png

Finally make sure that you only share the form internally with HR or whoever handles new employees.

I will continue to build on this code the next weeks and first up is off boarding (in a separate form and function). After that plan is to add groups on submit to automate other functions internally but I really would like to see what you folks can add on to this!

1 REPLY 1

Fulgubbe
Novitiate III

Updated 30/8/22 | Added some sanitize for ÅÄÖåäö in username and company email creation.

You Might Like

New to the site? Take a look at these additional resources:

Community created scripts:

Our new Radical Admin blog:

Keep up with Product News:

Read our community guidelines

Ready to join us? You can register here.