Need a script to split the value and send to body

Saib1
Tera Guru

Hi All

 

I am sending these values - user_id = test@gmail.com,test1@gmail.com,test3@gmail.com

 

var r = new sn_ws.RESTMessageV2('usermange', 'dev instance);
var body = {
'user_id': current.variables.user_id.getDisplayValue(),
};
var obj = JSON.stringify(body);
r.setRequestBody(obj);
var response = r.execute();

How to split the user_id with comma separated and pass to body?

8 REPLIES 8

Anand Kumar P
Giga Patron
Giga Patron

Hi @Saib1,

Use below script to split values 

var user_ids = current.variables.user_id.getDisplayValue().split(',');
var body = {
'user_id': user_ids,
};
var obj = JSON.stringify(body);

Please mark this as helpful and solution proposed if its serves your purpose.

 

Thanks,

Anand

Jim Coyne
Kilo Patron

What do you mean by needing to "split" the email addresses?  "Splitting" a string will return an Array.  What is the  goal of splitting the data?  What is the endpoint expecting to see?

 

And also, what have you tried?  What results are you getting?  Your post is pretty vague.

And you might consider using this format:

 

 

var obj = {};  //setup the object variable
obj.user_id = userId;   //add the "user_id" property to the object
var body = JSON.stringify(obj);  //convert the object to a string

The body string variable will end up with a value of:
{"user_id":"test@gmail.com,test1@gmail.com,test3@gmail.com"}

 

 

This way you don't have to worry about having the proper syntax.  And notice how I've reversed the variable names.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Saib1 

To split a string which separated by comma, you can use split() method. Then it will return an array, you'll need to loop through the array to get the value of each user_id.

Sample.

var user_id = "test@gmail.com,test1@gmail.com,test3@gmail.com";
var arrUser = user_id.split(',');
for (var i in arrUser){
    gs.log(arrUser[i]);
}

Screenshot 2023-11-28 at 10.37.26.png

For better understanding, can you share the use case behind your question?

 

Cheers,

Tai Vu`