Search

Quickly remove __c from fields in custom forms

In my personal opinion, I see no value in the __o and __c suffix conventions that overlay/best practice mode have brought to the AR System as it relates to fully custom forms. There are many other areas within Developer Studio and within the Objects themselves at an API level that allow us to know if we are working on unmodified, overlaid or custom objects so making it visible to the administrators by way of the database name is annoying. While __c is important when adding custom fields to forms ‘owned’ by BMC (to avoid possible name collisions in future versions), it doesn’t serve much value on custom forms which is the point behind this utility.

There is an active BMC Communities idea for making this a toggle in dev studio (or possibly out-right removing it) here, please vote if you feel the same way as me:

https://communities.bmc.com/ideas/6043

Now in order to work around this for now in situations where you have many fields you want to strip the __c from (like the example in the idea above with large custom join forms), here is a sample Java API method that will do this for you, simply pass it in the name of the form and it will do it’s magic.

private static void cleanDBNameForFields(String targetForm) {
    // Allow us to work on Custom & Overlay Objects vs Base Objects
    ctx.setBaseOverlayFlag(false);
    ctx.setOverlayFlag(true);
    ctx.setOverlayGroup(String.valueOf(Constants.AR_GROUP_ID_ADMINISTRATOR));
    ctx.setDesignOverlayGroup(String.valueOf(Constants.AR_GROUP_ID_ADMINISTRATOR));
    List<Field> fieldsToClean = new ArrayList<Field>();
    try {
        // Get list of all the fields on the targetForm
        List<Fields> fields = ctx.getListFieldObjects(targetForm);
        for (Field field : fields) {
            String fieldName = field.getName();
            // Find field that have __c
            if (fieldName.endsWith("__c")) {
                // Chop off last 3 characters to get rid of __c and then add it to our list of Fields to update
                field.setNewName(fieldName.substring(0, fieldName.length() - 3));
                fieldsToClean.add(field);
            }
        }
        // Update all the fields in the same transaction
        ctx.setMultipleFields(fieldsToClean);
        System.out.println("   " + fieldsToClean.size() + " fields have been cleaned successfully on " + targetForm);
    } catch (ARException e) {
        System.out.println(e.getMessage());
    }
}

The code is simple enough, we simply get a list of all the fields on the target form, find the ones that have __c at the end, strip it out and then add the updated Field object to a new list of Fields. Once we have our list of fields, we simply feed it to setMultipleFields() so that it commits them all in a single transaction like Developer Studio does when you make changes to forms. This method is very fast, on my system, a join form with ~720 fields that contained __c took only 1.5 seconds to ‘clean’.

I’ve attached below a full program that can be run from the command line and it’s source that makes use of the example above, just feed it your connection info on the command line as well as the target form and it will give you back hours in your day 🙂

Here is a sample run with results shown:

CleanFieldDBNames

CleanFieldDBNames

Icon

CleanFieldDBNames - Source 5.96 KB 360 downloads

...
Icon

CleanFieldDBNames - Utility 7.34 MB 406 downloads

...