Business Rule Help needed!!

Snow Angel
Tera Expert

Can someone help me fix this BR so it shows the attachment file ext.

Currently its creating (required) prefix by picking up the value from a field, and creating a new file name as prefix + hyphen + file name. But for some reason not picking up the file extension.

 

Requirement : The total name character limit is 100 i.e prefix + filename.ext 

Business Rule:

(function executeRule(current, previous /*null when async*/ ) {
var auditGR = new GlideRecord(current.table_name);
auditGR.get(current.table_sys_id);
if (auditGR.u_pbc_id) {
current.file_name = auditGR.u_pbc_id + " - " + current.file_name;
}
})(current, previous);

 

This is what it shows currently in the activity, its missing the file ext.

SnowAngel_0-1721920855529.png

 

1 ACCEPTED SOLUTION

@Snow Angel 
try this:

 

(function executeRule(current, previous /*null when async*/ ) {
    var auditGR = newGlideRecord(current.table_name);
    auditGR.get(current.table_sys_id);
    if (auditGR.u_pbc_id) {
        var extensionIndex = current.file_name.lastIndexOf(".");
        var extension = current.file_name.substring(extensionIndex);
        var filename = auditGR.u_pbc_id + " - " + current.file_name;
        if (filename.length > 100) {
            filename = filename.substring(0, 100 - extension.length) + extension;
        }
        current.file_name = filename;
    }
})(current, previous);

View solution in original post

12 REPLIES 12

Satishkumar B
Giga Sage
Giga Sage

Hi @Snow Angel 
business rule you provided is modifying the file name but not appending the file extension. To ensure that the file extension is included, you can update the code to capture and append the extension separately.

 

(function executeRule(current, previous /*null when async*/) {
    var auditGR = new GlideRecord(current.table_name);
    auditGR.get(current.table_sys_id);

    if (auditGR.u_pbc_id) {
        // Get the current file name and extension
        var fileName = current.file_name;
        var fileExtension = '';

        // Extract the file extension
        if (fileName.lastIndexOf('.') !== -1) {
            fileExtension = fileName.substring(fileName.lastIndexOf('.'));
            fileName = fileName.substring(0, fileName.lastIndexOf('.'));
        }

        // Create the new file name with prefix and extension
        current.file_name = auditGR.u_pbc_id + ' - ' + fileName + fileExtension;

        // Ensure the total length is within the character limit
        if (current.file_name.length > 100) {
            var maxFileNameLength = 100 - auditGR.u_pbc_id.length - fileExtension.length - 3; // 3 for " - "
            fileName = fileName.substring(0, maxFileNameLength);
            current.file_name = auditGR.u_pbc_id + ' - ' + fileName + fileExtension;
        }
    }
})(current, previous);

 

Thank You!! Happy learning

……………………………………………………………………………………………………

Please Mark it helpful 👍and Accept Solution✔️!! If this helps you!!

Snow Angel
Tera Expert

@Satishkumar B 

Satish,

I have updated the BR as above but still same result, no file extension is shown.

Can you debug your code by adding some logs and see if each are getting executed.

Changed current.table_sys_id to current.sys_id to fetch the current record correctly.
Replaced current.table_name with current.getTableName() to dynamically retrieve the name of the table the current record belongs to.
Make sure the field names like u_pbc_id and file_name are correct and exist on the table