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 

Hello Satish,

I really appreciate your prompt help. I've made the suggested changes, the ext. shows but now its missing the required prefix i.e the value from the "u_pbc_id" field followed by a hyphen.

What can we do here so the prefix also shows up along with file name and its extension.

SnowAngel_0-1721923778991.png

 

Hi @Snow Angel 
can you please share me your latest code. also what is the field type of your prefix field?

@Satishkumar B 

Below is the latest code.

(function executeRule(current, previous /*null when async*/ ) {
    var auditGR = new GlideRecord(current.getTableName());
    auditGR.get(current.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 - 4; // 4 for " - "
            fileName = fileName.substring(0, maxFileNameLength);
            current.file_name = auditGR.u_pbc_id + ' - ' + fileName + fileExtension;
        }
    }
})(current, previous);

@Snow Angel what is the field type of your prefix field?

@Satishkumar B 

its a string filed, manually entered, the format is xxx-xxx for eg OIG-001-Filename.ext