Check for existence of system property

Abhijit Das7
Tera Expert

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. 

1 ACCEPTED SOLUTION

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 

View solution in original post

7 REPLIES 7

Aman Kumar11
ServiceNow Employee
ServiceNow Employee

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);
 
This can then be tried using Rest API explorer with below Request body 
{
"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

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

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