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

@Tai Vu 

 

I used this in Runscript workflow editor

 

 

loop is not repeating at all

 


var user_id = current.variables.user_id.getDisplayValue();
var arrUser = user_id.split(',');
for (var i in arrUser) {
gs.info('arrUser' + arrUser[i]);
var r = new sn_ws.RESTMessageV2('useraccess', 'Dev Instance');
var body = {
'roles': current.variables.roles.getDisplayValue(),
'user_id': arrUser[i]
};
}

Hi @Saib1 

At this line below, what is the type of this variable "user_id"?

var user_id = current.variables.user_id.getDisplayValue();

 

Can you put a log after it, to verify if any value in return.

Like below

 

var user_id = current.variables.user_id.getDisplayValue();
gs.info('user_id: ' + user_id);

 

 

 

Cheers,

Tai Vu

Jim Coyne
Kilo Patron

OK, so it seems like you want to send a REST message for each email address found in the "user_id" catalog variable.  But what do you mean by the loop is not repeating?  Do you mean you are not getting anything in the logs from the "gs.info()"?

 

This simplified version of your script will show the email addresses in the logs:

var email = "1@test.com,2@test.com,3@test.com";  //list of addresses instead of from catalog variable
var user = email.split(',');  //split into an array
for (var i in user) {  //loop through each array member
	gs.info('JC - user[' + i + '] = ' + user[i]);  //spit it out to the logs
}

 

and the result is this:

JimCoyne_0-1701217845463.png

 

So the loop is working.  Is "current" a valid GlideRecord for the script you are running?  Is "roles" the proper catalog variable name?  Is "user_id"?

 

Now you are initializing a new RESTMessageV2 object during each loop as well as building the body but you are not actually sending the message: is that what you mean by it's not looping?  It's hard to understand your issue without more info on what you are really trying to do and what results you are getting.

@Jim Coyne 

Passing 2 id - test@gmail.com,test1@gmail.com

It is calling the rest message and adding only test@gmail.com.

 

Yes for each userid , restmessage should get looped, As of now it is adding for one user id test@gmail.com

 

Workflow Editor
var user_id = current.variables.user_id.getDisplayValue();
var arrUser = user_id.split(',');
for (var i in arrUser) {
gs.info('arrUser' + arrUser[i]);
var body = {
var r = new sn_ws.RESTMessageV2('User Access Mangement', 'Dev Instance');

'user_id': arrUser[i]

'action_required': current.variables.action_required.getDisplayValue(),

};
}

 

Scripted rest API 

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

 

var reqbody = request.body;
var reqdata = reqbody.data;
var objt = JSON.stringify(reqdata);
var objt1 = objt.toString();
var incomingData = JSON.parse(objt1);
var roles = incomingData.roles;
var action_required = incomingData.action_required;
var user_id = incomingData.user_id;
var group_s = incomingData.group_s;

var action_required = incomingData.action_required;

if (action_required == "Add a role to user") {
var role = new GlideRecord("sys_user_has_role");
// role.addEncodedQuery('user=' + userSysId + '^role=' + roleSysId);
role.addQuery("role", roleSysId);
role.addQuery("user", userSysId);
role.query();
if (role.next()) {
gs.log('Role Exsists', 'shohaib');
var respbody = {
"message": "Role already exists"
};
response.setContentType('application/json');
response.setStatus(400);
response.setBody(respbody);
} else {
role.initialize();
role.setDisplayValue('user', userSysId);
role.setDisplayValue('role', NameofRole);
role.state = "Active";
role.setWorkflow(false);
role.insert();
var respbody1 = {
"message": "Role added successfully"
};
response.setContentType('application/json');
response.setStatus(200);
response.setBody(respbody1);
}

 

}