Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How-to create multiple customers in Jira Service Management (Cloud) with one batch run

Your Requirement

You have a lot of customers in a CSV file and you don't want to add them manually to Jira Service Management as customers.

Your skills

  • you know how to create and edit CSV files
  • you can edit configuration files
  • you have Jira Administrator Global permission, app scope ADMIN and OAuth scopes manage:servicedesk-customer
  • you have PHP installed
    • for Linux: there a packages for most distributions available (OpenSUSE: zypper install php php-curl)
    • for Win: get a package here https://windows.php.net/download
      • enable curl in php.ini: extension=C:\<path to>\php\ext\php_curl.dll
      • enable ssl in php.ini,: extension=C:\<path to>\php\ext\php_openssl.dll
  • (optional) PHP coding skills

Before you run the script you should know

  • creating new customers is 'silent'. They don't know that a new account exists. The script is sending emails to them, instructing how to reset the password.
  • to deactivate sending of emails, leave SENDER empty.
  • a mail relay must be available for sending mails. Read this.
  • the created customer need to set the password. This cannot be done with the REST API.

Prepare the CSV file

  • one line per new customer
  • the delimiter is ";"
  • column 1 = email
  • column 2 = displayed user name
  • no header row is required
  • save the file as customers.csv

Adapt the script to your needs

Copy & paste the script to a file called create_customers.php and modify the "define" values to your needs. You will require an API-Token.

<?php

define('JIRA_URL', 'https://mydomain.atlassian.net');
define('EMAIL', 'admin@mydomain.com');
define('APITOKEN', 'rahHWQ61rElyFHmsz9H6ABv');
define('CSVFILE', 'customers.csv');
define('FILELENGTH', 2048);
define('SENDER', 'no-reply@mydomain.com');
define('PORTAL', '/servicedesk/customer/portal/1');
define('SUBJECT', 'New account for Jira Service Management Portal');
define('BODY', 'we have just registered you at our JSM portal. Please follow the link below, login with your email and press the link "Forgot your password?" to get a reset email mail sent from the cloud.' . PHP_EOL . PHP_EOL);


function create_customer($json) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => JIRA_URL . '/rest/servicedeskapi/customer',
CURLOPT_USERPWD => EMAIL . ':' . APITOKEN,
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array('Content-type: application/json','User-Agent: x'),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => 0
));

$result = curl_exec($ch);

if(!$result) {
trigger_error(curl_error($ch));
exit(1);
}

if (!curl_errno($ch)) {
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 201:
break;
default:
echo "Request failed with HTTP-Code: ", $http_code, "\n";
var_dump($result);
exit(1);
}
}

curl_close($ch);
}


$header = 'From: '. SENDER . "\r\n" . 'X-Mailer: PHP';

$handle = fopen(CSVFILE, "r");
if ($handle) {
while (($data = fgetcsv($handle, FILELENGTH, ";")) !== FALSE) {

$uemail = utf8_decode($data[0]);
$udname = utf8_decode($data[1]);

$request = '{"displayName": "' . $udname . '", "email": "'. $uemail . '"}';

echo "New customer " . $uemail ;
create_customer($request);
echo " created.";

if (strlen(SENDER) > 1) {
mail($uemail, SUBJECT, 'Dear ' . $udname . ',' . PHP_EOL . PHP_EOL . BODY . JIRA_URL . PORTAL, $header);
echo " info-mail sent.";
}
echo PHP_EOL;
}
fclose($handle);
} else
echo "The file " . CSVFILE . " could not be opened!\n";

?>

What the script does:

  1. open the csv file, read line by line
  2. per line, one REST request is sent to create the customer
  3. (optional) per line, an email is sent to the customer

Run the script on Linux

php -f /path/to/create_customers.php

Run the script on Win PowerShell

.\php.exe -f \path\to\create_customers.php

(tested with PHP 8.0.5 on OpenSUSE Tumbleweed)

Follow this link for How-to create multiple users in Jira with one batch run (Server).

1 comment

Mohammed Amine Community Leader Jun 09, 2021

Interesting, nice !

Comment

Log in or Sign up to comment
TAGS
Community showcase
Published in Jira Service Management

JSM Jira Automation: How to Send SLA Breached Notifications

Hi Everyone, In   this tutorial,  we will show you how you can monitor an SLA, and send notifications before or after the SLA has been breached.   SLA Threshold Trigger The SLA t...

731 views 5 13
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you