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

Extract values from string separated by comma and push to table

Geeky
Kilo Guru

Hi All,

I have a situation where I will be getting two field values into a staging import table;

Field 1: a,b,c

Field 2: x,y,x

Now I have to extra the values separated by comma and push into two different tables; Like below;

a==>x

a==>y

a==>z

b==>x

b==y

b==z

c==x

c==y

c==z

4 REPLIES 4

Chaitanya Redd5
Tera Guru

Hi,

You could refer to the below link for reference:

https://community.servicenow.com/community?id=community_question&sys_id=96c183a9db98dbc01dcaf3231f96...

Kindly mark my answer as Correct and Helpful based on the Impact.

Regards,

Chaitanya

Sateesh Kumar 2
Kilo Guru

Hello,

One thing I couldn't understand is, you are saying you need to put into two different tables but couldn't find what should go into table 1 and what should go into table 2.

Let me answer your other questions.

Write two different transform map one with the target table as table 1 and another with the target table as table 2 both should point to the same import set table.

No field mapping needed as we use would use a script to do that.

write a onbefore script as below

var set1 = source.field1.split(",");

var set2 = source.field2.split(",");

//validation

if(set1.length==set2.length && set1.length>0)

{

//insert 1 record using transform map

target.field1 = set1[0];

target.field2=set2[0];

//insert other record using script

for(i=1;i<set1.length;i++)

{

var trGr = new GlideRecord("<target table>");

trGr.initialize();

trGr.field1 = set1[i];

trGr.field2 = set2[i];

trGr.insert();

}

}

 

Use the same logic in another transform map too.

If you have to check for existing records and update add logic to check for unique values. 

Regards,

Sateesh Kumar Devadoss.

asifnoor
Kilo Patron

Hi,

How are you inserting data into these tables? Are you using transform maps, if yes, then do you have 2 transform maps or only one?

Also, the data that you are getting as a string with comma separated, do you have a correct sequence all the time? For example, the 1st field goes into column a of table1 and 2nd field goes into column a of table 2, liek that.

at the end, to get the data it is simple, write a source field script and do like this

var data = source.field1.split(",");
//data is in the array, so you can access like below
target.field1=data[0];

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

im_atrey
Tera Contributor

Hi Geeky,

 

You can leverage the .split() method to extract comma-separated values.

var ext1,ext2,ext3;//define variables to store extract values
var field 1= x,y,z;

var extract=field.split(",");// split method 

ext1=extract[0]; // contains x 
ext2=extract[1]; // contains y
ext3=extract[3];// contains z

 

It's totally up to you how you wanna initialize variables, whether in JSON or in a separate variable.