Update user weblogin

Not Yet Reviewed
Can any body helps to update one field for all user in xMatters by looping one by one user. 
For getting all user we are using below script:
Please help to see the body of response and need guidance to looping each user. 
we tried something like this. Please suggest whether the script is correct.

    var getPeople = http.request({
        'endpoint': 'xMAPI',
        'method': 'GET',
        'path': '/people',
        'headers': {
            'Content-Type': 'application/json'
        }
    });
 
    var response = getPeople.write(requestBody);
    if (response.statusCode != 200 ) {
        console.log('\nThere was an error updating the external system.\n');
        return;
    }
    /** Write the response from the external system to the activity stream. **/
    console.log(JSON.stringify(response));
    var responseBody = JSON.parse(response.body);
    		var obj = new global.JSON().decode(responseBody);
	for(var i=0;i<obj.people.length;i++){
			var people_id=obj.people[i].id.toString();
			var email_version=obj.people[i].emailAddress;
		
    var json = {};
    json.id = people_id;
    json.webLogin = email_version;
    var update = http.request({
        'endpoint': 'xMAPI',
        'method': 'POST',
        'path': '/people',
        'headers': {
            'Content-Type': 'application/json'
        }
    });
    
    var response2 = update.write(json);
    if (response2.statusCode != 200 ) {
        console.log('\nThere was an error updating the external system.\n');
        return;
    }
    /** Write the response from the external system to the activity stream. **/
    console.log(JSON.stringify(response));
	}
}
0

Comments

5 comments
Date Votes

Please sign in to leave a comment.

  • Hello! That looks ok. The API calls are correct and the loop seems to make sense. 

    However, the people response is paginated so you will need to get "more" when you reach the end of your current batch. 

    Depending on how many users you are trying to update, you might run into the 60s max execution time of the script. If you script runs for more than 60 seconds, it will be terminated. So maybe the best way to do the loop is to pass the offset value to the script. Then the script can deal with one page at a time, then call itself again with the offset value and deal with the next page. 

    The integration builder wasn't really designed to do mass updates like this, but in theory this should work. If you have any other script environments, this kind of update might be done better there. 

    Let me know how you get on or if you have further questions!

    0
  • Here is some sample code the illustrious Tom Kouhsari had laying around. 

    function getPaginatedResults(endpoint, initialPath) {
    var results = [];
    var path = initialPath;

    for (i = 0; path != null; i++) {
    var request = http.request({
    'endpoint': endpoint,
    'path': path,
    'method': 'GET'
    });

    var response = request.write();
    if (response.statusCode < 200 || response.statusCode > 299) {
    return results;
    }

    var json = JSON.parse(response.body);
    var results = results.concat(json.data);

    path = json.links.next;
    }

    return results;
    }

    people = getPaginatedResults("xMatters", "api/xm/1/people")
    console.log(people.length);
    0
  • I get this code from Xmatter help, I need to update all user record (i.e need to copy the email address value to webLogin field.) this is not working. Can you please help to update the "email" value to "WebLogin" for all User.  
    
        var getPeople = http.request({
            'endpoint': 'xMAPI',
            'method': 'GET',
            'path': '/people',
            'headers': {
                'Content-Type': 'application/json'
            }
        });
     var response = request.write();
    
    if (response.statusCode == 200 ) {
       json = JSON.parse(response.body);
       console.log("Retrieved: " + json.count + " of " + json.total + " responses");
       for (var i in json.data)
       {
         var people_id=json.data[i].id.toString();
         var email_version=json.data[i].emailAddress;
    var json = {};
        json.id = people_id;
        json.webLogin = email_version;
        var update = http.request({
            'endpoint': 'xMAPI',
            'method': 'POST',
            'path': '/people',
            'headers': {
                'Content-Type': 'application/json'
            }
        });
        
        var response2 = update.write(json);
        if (response2.statusCode != 200 ) {
            console.log('\nThere was an error updating the external system.\n');
            return;
        }
        /** Write the response from the external system to the activity stream. **/
        console.log(JSON.stringify(response));
       }
    }
    0
  • Sorry, I'm not clear if this is working as you want it to or not. On first look it seems that it should work. Are you getting an error or is it working?

    0
  • I got something like this working this week.  My path was /api/xm/1/people instead of just people, so you just have to confirm your endpoint is pointing to the right place.

    Also, the email address doesn't come back with my people call.  I need to do another call to get it, and specifically get it from the Work Email device.  I think you need to make another call to get that device to have the value, and set it as the user's weblogin.

     

    0

Didn't find what you were looking for?

New post