Script Needed to find the user is present or not
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 06:01 AM - edited 01-10-2025 06:05 AM
I am writing a script in Scripted rest API in my test instance
1. From my Prod instance request via get sending the user name(Sahana Asma) as request to my test instance
2. From my test instance on the the scripted rest api via get method need to check on sys_user table that user is available or not . If user not available send a response back that user unavailable
Someone please give me script ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2025 06:30 AM - edited 01-10-2025 06:39 AM
Hello @Saib1 ,
You can follow the below script: -
var ID = request.pathParams.sys_id;
var user= new GlideRecord("sys_user");
user.addQuery("sys_id",ID);
user.query();
if(user.next()){
var arr = [];
var obj = {};
obj.id = user.user_name;
obj.first_name= user.first_name;
obj.last_name = user.name;
arr.push(obj);
response.setStatus(200);
response.setBody(arr);
} else {
response.setStatus(500); //Set the status code- internal server error
response.setBody({
error: {
message: 'User id is not found.',
detail: 'The record could not foud into the table. Check system logs for more detail.'
},
status: 'failure'
})
}
}
Please mark my answer as accepted solution and also give thumbs up, if it helps you.
Regards,
Abhishek Thakur