Notifications

404 views

How to set dictionary attributes quickly for all fields in a table or set of tables



Introduction


Dictionary attributes alter the behavior of the table or element that the dictionary record describes. Administrators can add or modify dictionary attributes.

Adding an Attribute


To add an attribute to a table or field, you generally navigate to the System Dictionary record for the Dictionary entry, and add the attribute to the Attributes field. Attributes are comma-separated; if attributes already exist on a dictionary record, add a comma, with no spaces, before adding a new attribute.

This method can take a while, especially if you're adding an attribute for many tables and many fields.  The JavaScript provided in this article enables you to specify the table name and the attribute string, and set the attribute for all columns in that table. This should help speed up situations where you want to disable text indexing for tables but cannot disable it at the collection level because it's inherited from a parent table.

For more information, see the product documentation topic Dictionary attributes.

JavaScript


var vTableName = 'cmdb_ci_printer';
var vAttribute = 'no_text_index=true';
var vDryRun = false; // set this to false when you're ready to do the change
 
// Do Not Modify below this line
 
bulkUpdateTableAttributes(vTableName, vAttribute, vDryRun);
 
function bulkUpdateTableAttributes(pTableName, pAttribute, pDryRun) {
    var gr = new GlideRecord('sys_dictionary');
    gr.addEncodedQuery('name=' + pTableName + '^element!=NULL');
    gr.query();
    var bUpdate = false;
    while (gr.next()) {
        var vAttributes = gr.attributes;
        var nLength;
        if (vAttributes) {
            nLength = vAttributes.length;
        } else {
            nLength = 0;
        }
        if (0 == nLength) {
            // There are no other attributes, just set it
            gr.attributes = pAttribute;
            bUpdate = true;
        } else {
            // Attributes is not empty, check to see if we have it set already:
            var n = vAttributes.indexOf(pAttribute);
            if (-1 == n) {
                // The attribute is not set, so append it:
                gr.attributes = vAttributes + ',' + pAttribute;
                bUpdate = true;
            } else {
                // The attribute is set; do nothing
            }
        }
        if (!pDryRun) {
            if (bUpdate) {
                gs.print('Updating record sys_dictionary.do?sys_id=' + gr.sys_id + ' to ' + gr.attributes);
                gr.setWorkflow(false);
                gr.update();
            }
        }
        bUpdate = false;
    }
}

 

Article Information

Last Updated:2018-01-04 23:18:27
Published:2018-01-05