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

M Amine
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 9, 2021

Interesting, nice !

Comment

Log in or Sign up to comment
TAGS
AUG Leaders

Atlassian Community Events