- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 01:26 PM - edited 11-10-2024 06:45 PM
Hi everyone,
I have created around 6-7 custom system properties according to my business requirement. Now I am creating one Scripted rest api and I have to check whether these properties exist or not in this system.
How can check whether the system properties exist or not with Scripted rest api.
cc; @Ankur Bawiskar
Thanks in advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 12:18 AM
Hello @Abhijit Das7,
Thanks for clarifying your query. I was under the impression that if any of the property is present, then the expected output needs to be true.
Now that your requirement is to have output as true when all 6 property exists, then you can consider using condition gr.getRowCount() == 6
var propertiesArray = ["propertyName1", "propertyName2", "propertyName3", "propertyName4", "propertyName5", "propertyName6" ]
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", "IN", propertiesArray);
gr.query();
var result = gr.getRowCount() == 6;
gs.info("testing sys property" + result);
Please mark this response as correct and/or helpful if it assisted you with your question 😊
Regards,
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2024 08:44 PM - edited 11-10-2024 09:21 PM
Hello @Abhijit Das7 ,
To create scripted rest API for your use case, you can create a POST resource with below script
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body.data;
var propertiesArray = requestBody.properties;
response.setContentType('application/json');
if (!propertiesArray)
response.setError(new sn_ws_err.BadRequestError(gs.getMessage('Missing required parameters')));
else {
try {
var result = {};
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", "IN", propertiesArray);
gr.query();
if (gr.hasNext()) {
result.propertyExists = true;
} else {
result.propertyExists = false;
}
response.setBody(result);
response.setStatus(200);
} catch (err) {
gs.error(JSON.stringify(err));
response.setBody([]);
response.setStatus(200);
}
}
})(request, response);
{
"properties": ["propertyName1", "propertyName2", "propertyName3", "propertyName4", "propertyName5"]
}
The output would be
{
"result": {
"propertyExists": true
}
}
OR
{
"result": {
"propertyExists": false
}
}
Please mark this response as correct and/or helpful if it assisted you with your question 😊
Regards,
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 12:02 AM - edited 11-11-2024 12:08 AM
Hi @Aman Kumar11 ,
I tried your method, but even one of the properties are missing still it returns as true. In my scenario if all 6 properties are present then it should return as true.
I have added part of my code,
var propertiesArray = ["propertyName1", "propertyName2", "propertyName3", "propertyName4", "propertyName5", "propertyName6" ]
var result ;
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", "IN", propertiesArray);
gr.query();
if (gr.hasNext()) {
result = true;
} else {
result = false;
}
gs.info("testing sys property" + result);
I have edited code according to my requirement. Issue is that I have deleted system property 6 in my PDI still I am getting result as true. If all properties are present in system, then the result should be true or else false.
cc: @Ankur Bawiskar . This is my business requirement. I have to check in Scripted rest api whether these 6 properties exist or not in system if not then result should be false or else true
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 12:18 AM
Hello @Abhijit Das7,
Thanks for clarifying your query. I was under the impression that if any of the property is present, then the expected output needs to be true.
Now that your requirement is to have output as true when all 6 property exists, then you can consider using condition gr.getRowCount() == 6
var propertiesArray = ["propertyName1", "propertyName2", "propertyName3", "propertyName4", "propertyName5", "propertyName6" ]
var gr = new GlideRecord("sys_properties");
gr.addQuery("name", "IN", propertiesArray);
gr.query();
var result = gr.getRowCount() == 6;
gs.info("testing sys property" + result);
Please mark this response as correct and/or helpful if it assisted you with your question 😊
Regards,
Aman Kumar