Japanese Characters

Someone recently asked me how to send Japanese characters as field values to an incident when using the ServiceNow Perl API.

I created a script based off of examples in the ServiceNow Perl API documentation and found that my field value was converted to a Base64 string by the time it arrived into ServiceNow.

I did some poking around and found out that the Perl module, SOAP::Lite, which is used by the ServiceNow API, will automatically encode a web service field to base64 if it doesn’t understand the data type it is using. In order to submit the real UTF-8 value of the field, you have to explicitly set the data type to string and decode the string to UTF.

In order to do this, I had to make sure to include the following libraries in my script: “SOAP::Lite”, and “Encode”.

Then, when defining my field that had the Japanese characters, I set the field in the following manner:

1
2
use SOAP::Lite;
use Encode;
1
my $subject = SOAP::Data->type(string => decode_utf8('Test Test 漢字仮名交じり文'));

Once I did this, I was able to create and incident where the subject contained Japanese characters.

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/perl -w
#
use ServiceNow;
use ServiceNow::Configuration;
use ServiceNow::GlideRecord;
use SOAP::Lite;
use Encode;

my $CONFIG = ServiceNow::Configuration->new();
my $subject = SOAP::Data->type(string => decode_utf8('Test Test 漢字仮名交じり文'));

print"\nSUBJECT: ".$subject."\n";

$CONFIG->setSoapEndPoint("https://myinstance.service-now.com/");
$CONFIG->setUserName("admin");
$CONFIG->setUserPassword("admin");

print "############### create/insert\n";

print "######## using GlideRecord object\n\n";

my $gr = ServiceNow::GlideRecord->new($CONFIG, "incident");
$gr->setValue("short_description", $subject);
$gr->setValue("category", "hardware");
my $sysid = $gr->insert();
print "sys_id = " . $sysid . "\n";
print "number = " . $gr->getValue("number") . "\n";
1;