How to get the length of the string field auto populated on another field and shown in list view without saving the form?

tyagisu
Mega Expert

Hi,

I need the list of managed document records where the length of the filename equals exactly 100 characters. So, I created a field "Length of file" on Revision table and captured the length of the file name in this field which is working fine as I used onload Client script. But I don't want to open each record and save it in order to appear on the list view.

I tried using fix script and before query business rule but none of them is working as expected.

Here is the code I used for Fix script

var gr = new GlideRecord("dms_document_revision");
var qry = 'attachment.file_nameISNOTEMPTY';
gr.addEncodedQuery(qry);
gr.query();
while (gr.next()) {
var field = gr.getDisplayValue('file_name').length;
if(field > 10)
{
  gr.setValue("u_length_of_file", field);
}
gr.updateMultiple();
}

find_real_file.png

1 ACCEPTED SOLUTION

Try this...

var gr = new GlideRecord("dms_document_revision");
gr.addEncodedQuery('attachment.file_nameISNOTEMPTY');
gr.query();
while (gr.next()) {
    var fieldLength = gr.attachment.getValue('file_name').length;
    if (fieldLength > 10) {
        gr.u_length_of_file = fieldLength;
    }
    gr.setWorkflow(false); // Do not run business rules
    gr.autoSysFields(false); // Do not update system fields
    gr.update();
}

View solution in original post

15 REPLIES 15

It's probably not working for those where the attachment file name is less than 10 characters, right?  It also won't include anything without an attachment file name.  Those are the only conditions excluded.  Check your data to make sure it's meeting the conditions specified.

I know it's weird but I checked the data. I added the filter condition on the table so that the documents with the attachment file name would show up only. Also, I changed the length condition to 5 instead of 10.

Okay, run this from the 'Scripts -> Background' module and let me know what output you get.  When running this script are you getting the correct counts?  If it isn't printing out 'Long field length' for one of the records can you update that field manually?

var gr = new GlideRecord("dms_document_revision");
gr.addEncodedQuery('attachment.file_nameISNOTEMPTY');
gr.query();
gs.print('Number of records to check length: ' + gr.getRowCount());
while (gr.next()) {
    var fieldLength = gr.attachment.getValue('file_name').length;
gs.print('Checking: ' + fieldLength);
    if (fieldLength > 10) {
gs.print('Long field length');
        gr.u_length_of_file = fieldLength;
        gr.setWorkflow(false); // Do not run business rules
        gr.autoSysFields(false); // Do not update system fields
        //gr.update();
    }
}

The output that came up is HUGE!!!

So the total records are 54687 where attachment file name is not empty, out of which 34678 are not getting updated with field Length.

 

And how many of those have a field length less than 10?  Does that number match the number that aren't being updated?  Are there any of those where the field length is greater than 10 that you are able to update manually?