Need a script to split the value and send to body
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 08:40 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:03 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 01:45 PM - edited 11-27-2023 02:20 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 05:27 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 07:38 PM
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]);
}
For better understanding, can you share the use case behind your question?
Cheers,
Tai Vu`