GET Mandatory Fields from REST API

lexicalunit
Giga Expert

I need a way to get the list of mandatory fields for an incident so that I can create an incident on behalf of a user. The user will NOT be a System Administrator in this case so the user will not have access to sys_dictionary and can therefore NOT be expected to be able to make a query like the following:

 

curl -XGET "https://${INSTANCE}/api/now/table/sys_dictionary?sysparm_query=name%3Dincident&sysparm_fields=column_label%2Cmandatory%2Cinternal_type" \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    -H "Accept: application/json"

 

For System Administrators this query will return data which enumerates all the fields for an incident and true/false if they are mandatory.

 

What other way is there within the ServiceNow suite of REST APIs (or any other kind of APIs available to a typical user level client_credentials) that can be used to fetch down the list of fields and if any of them are mandatory or not?

9 REPLIES 9

Runjay Patel
Giga Sage

Hi @lexicalunit ,

 

You can write a script to get the mandatory fields in script include and return as a Json.

getMandatoryFields: function() {
        var fields = [];
        var gr = new GlideRecord('incident');
        gr.initialize();
        
        var elements = gr.getElements();
        for (var i = 0; i < elements.size(); i++) {
            var element = elements.get(i);
            if (element.isMandatory()) {
                fields.push(element.getName());
            }
        }
        return fields;
    },

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

I don't have access to the ServiceNow website, I only have a user's client_token which I can use to make REST API requests. If it can't be done with a normal access level client_token from a user, then I can't do it.

Hi @lexicalunit ,

 

you can ask ServiceNow developer to make scripted rest api and provide you the new end points to get the fields.

and if you don’t wanna do then the user which you are using for authentication should have read access to dictionary table.

 

Accept the solution if it helped.

I can't ask hundreds of ServiceNow users who may not be admins to create scripts and hope they do it right. Not viable.