Calling multiple users in a loop

tanz
Tera Expert

Hi,

 

I have a requirement where single/multiple users will be given in a field (comma separated) and I have to take the users and execute all of them in a loop of rest message , each user output will give 200 result. I am passing the user from the BR(after insert) where in the my custom table only one string field is there - User to be added(ex:a.b@gmail.com,b.c@gmail.com). I am trying to call the script include by passing the user field as parameter. I want to print the segregated user and print 200 against each user.

 a.b@gmail.com 200

a.b@gmail.com 200

_getUser: function(email) {
        try {
            var token = this._getToken();
            if (JSUtil.nil(token)) {
                return;
            }
            var r = new sn_ws.RESTMessageV2('API', 'Get User ');
            r.setStringParameterNoEscape("token", token);
            r.setStringParameterNoEscape('userEmail', "'" + email + "'");
            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            var parsing = JSON.parse(responseBody);
            var jsonObj = {
                userSpecificID: '',
                httpStatus: '',
                responseBody: ''
            };
            jsonObj.userSpecificID = parsing.value[0].id + '';
            jsonObj.httpStatus = httpStatus;
            jsonObj.responseBody = parsing;
            return JSON.stringify(jsonObj);
        } catch (ex) {
            var message = ex.message;
        }
    },
 
I need some help in the BR on how to segregate the user and how to pass each user one by one and get the output of 200 in print format and return to BR
2 REPLIES 2

Vasantharajan N
Giga Sage
Giga Sage

@tanz - Please try with the below code 

_getUser: function (email) {
    try {
        // email variable carries a field value a.b@gmail.com,b.c@gmail.com
        // email = "a.b@gmail.com,b.c@gmail.com"

        var userEmails = email.split(",");

        var token = this._getToken();
        if (JSUtil.nil(token)) {
            return;
        }

        var resultArray = [];

        for (var userEmailIndex; userEmailIndex < userEmails.length; userEmailIndex++) {
            var userEmail = userEmails[userEmailIndex];

            var r = new sn_ws.RESTMessageV2('API', 'Get User ');
            r.setStringParameterNoEscape("token", token);
            r.setStringParameterNoEscape('userEmail', "'" + userEmail + "'");
            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            var parsing = JSON.parse(responseBody);
            var jsonObj = {
                userSpecificID: '',
                httpStatus: '',
                responseBody: ''
            };
            jsonObj.userSpecificID = parsing.value[0].id + '';
            jsonObj.httpStatus = httpStatus;
            jsonObj.responseBody = parsing;

            var result = userEmail + " " + httpStatus;
            gs.info(result);
            resultArray.push(result);
            //return JSON.stringify(jsonObj);
        }

        /*returns the value 
        a.b@gmail.com 200 
        a.b@gmail.com 200"
        Note: response code may not be 200 in case of failure in the result

        */
        return resultArray.join("\n");

    } catch (ex) {
        var message = ex.message;
    }
}

Thanks & Regards,
Vasanth

SANDEEP28
Mega Sage

@tanz Create script include and put "_getUser: function(email) " code into it. Write below code in BR and call script include

var emails = current.custom_email_field + '';  // put your custom field name here
var userEmails = emails.split(',');

userEmails.forEach(function(index){
  var output = new scriptIncludeName._getUser(index);
})

 

 If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!