The CreatorCon Call for Content is officially open! Get started here.

How to receive and store an Array in the User Form?

Robert_Anderson
Tera Expert

Hello,

 

I have received a requirement to receive information from OKTA that is stored as an Array into ServiceNow in the User form.

 

We are trying to avoid making a custom table for the data to flow into but I know ServiceNow does not have an Array field type.

 

Any help is appreciated, thank you!

1 ACCEPTED SOLUTION

Robert_Anderson
Tera Expert

We solved this internally, posting solution for others.

 

In OKTA we put Arrays.toCsvString(user.{field}) and mapped that to our ServiceNow field, OKTA sent the array as a comma separated value. 

View solution in original post

4 REPLIES 4

AshishKM
Kilo Patron
Kilo Patron

Hi @Robert_Anderson , 

You can create a list type field on user table and convert the incoming array into comma seperated value. 

 

For example, you can check the Type field on Group [sys_user_group] table.

AshishKMishra_0-1711674041361.png

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Maddysunil
Kilo Sage

@Robert_Anderson 

I think you can try something like below:

You can store JSON data in a text field. This allows you to represent arrays as strings and parse them when needed. For example, if your array data from OKTA looks like ["value1", "value2", "value3"], you can store it as a JSON string in a text field.

Let's say you have array-like data coming from OKTA, which includes a list of roles assigned to a user. This data is represented as follows: ["Role1", "Role2", "Role3"]

 

To store this data in a ServiceNow User form using a JSON field, follow these steps:

  1. Create a Text Field: First, create a text field on the User form in ServiceNow where you want to store the roles. Let's call this field okta_roles.

  2. Convert Array to JSON String: Before storing the array data, you need to convert it into a JSON string. You can use the JSON.stringify() method to convert an array to a JSON string. For example:

 

var rolesArray = ["Role1", "Role2", "Role3"];
var rolesJSONString = JSON.stringify(rolesArray);

 

This will give you a JSON string representation of the array:

 

["Role1", "Role2", "Role3"]

 

  1. Store JSON String in Text Field: Now, you can store this JSON string in the okta_roles field on the User form in ServiceNow.

  2. Parse JSON String When Needed: When you need to access the roles data, you can retrieve the JSON string from the okta_roles field and parse it back into an array using the JSON.parse() method. For example:

 

var rolesJSONString = userRecord.getValue('okta_roles'); // Assuming userRecord is the GlideRecord for the User
var rolesArray = JSON.parse(rolesJSONString);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Hello,

For 2. Convert Array to JSON String: Before storing the array data, you need to convert it into a JSON string. You can use the JSON.stringify() method to convert an array to a JSON string.

Where do I do that, would that be in OKTA or in the field settings in ServiceNow?

Robert_Anderson
Tera Expert

We solved this internally, posting solution for others.

 

In OKTA we put Arrays.toCsvString(user.{field}) and mapped that to our ServiceNow field, OKTA sent the array as a comma separated value.