Integration REST Message to match users

Peter Przenniac
Tera Expert

Trying to match user from instance 1 on instance 2 where unique field is email address, and capture sys_id of this user, so incident can be created on instance 2 with matched user:

try {
        var r = new sn_ws.RESTMessageV2('V1 Integration', 'Post Incident');
       
        var target = new GlideRecord('sys_user');
        target.addQuery('u_on_behalf_of.email', current.requested_for.email);
        target.query();
        if (target.next()) {
        current.requested_for = target.sys_id;
}
        var body = {
        "variables": {
            "u_on_behalf_of": current.requested_for,
            "category": "application",
            "subcategory": "data_incorrect",
            "cmdb_ci": "123454",
            "comments": current.getValue('description')
        }
    };
1 ACCEPTED SOLUTION

Peter Przenniac
Tera Expert

Found solution - I replaced user email address '@' with '%40' and added to URL endpoint (get method), so could query sys_user table to find user by email in Instance 2, in response body I captured user sys_id and parse in Instance 1, then I could create INC record from instance1 to instance2 with correct sys_id.

By steps:

1. create GET method - with api table to sys_user endpoint

2. create POST method with captured data to create INC's - with record producer endpoint

3. create PUT method to update INC's - with incident table endpoint

View solution in original post

6 REPLIES 6

Tony Chatfield1
Kilo Patron

Hi, unfortunately your post does not make your issue or question clear.
You code seems to be looking up a sys_user record and then updating the current reference for requested_for , based on result returned from target query, but this will (should) always be the same 'current.requested_for' record.
 Note: if you are looking up a new value based on a current value, it would be better to assign it to a new variable, unless you were actually 'updating' the current value.

 

If the instances have records with different user sys_id's, but matched email addresses, then you will need to either

1/Pass the email address to the target as your 'user' and use a scripted rest API or transform map to lookup and set the correct sys_user sys_id.

2/Run a rest GET query to lookup and return the sys_id of the target instance sys_user record based on email, then use it in your POST payload.
3/ (not recommended) Create duplicate\sync a copy of the other instances sys_user table so that you can lookup the correct sys_id for the instance and then use it in your payload.

 

Personally, I would look at option 1, posting your rest message into a temp import table and then using OOB transform functions to ensure the data is correct before it is inserted in the intended/target table.

 

Can you update this thread to clarify, so that the community can better understand your issue and is in a better position to assist.

Thanks you for your ideas, definitely used opt 1 - and at the moment Ive got issue to match users email address to fetch user sys_id from instance 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @Peter Przenniac ,
I trust you are doing great.
Please find the updated script as given below :

try {
    var r = new sn_ws.RESTMessageV2('V1 Integration', 'Post Incident');

    // Search for the user in Instance 2 based on email
    var target = new GlideRecord('sys_user');
    target.addQuery('email', current.requested_for.email);
    target.query();
    if (target.next()) {
        // If a matching user is found, use their sys_id
        var matchedUserId = target.sys_id;
    } else {
        // Handle scenario where no matching user is found
        // You might want to use a default user ID or handle this differently
        var matchedUserId = 'default_sys_id'; // Replace with appropriate action
    }

    // Prepare the payload for the REST request
    var body = {
        "variables": {
            "u_on_behalf_of": matchedUserId,
            "category": "application",
            "subcategory": "data_incorrect",
            "cmdb_ci": "123454",
            "comments": current.getValue('description')
        }
    };

    // Set the request body
    r.setRequestBody(JSON.stringify(body));

    // Send the request
    var response = r.execute();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    
    // Additional error handling and response processing can be added here

} catch (ex) {
    // Handle exceptions
    var message = ex.message;
    // Add appropriate exception handling code here
}

This script takes the email address from the current record in instance 1, searches for a matching user in instance 2, and uses their sys_id to create an incident in instance 2 with a REST call.


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Same as my script, your is passing sys_id from instance 1, where should capture sys_id from user on instance 2